Skip to content

Commit a719a09

Browse files
authored
Merge pull request #6 from playcanvas/select-largest-submodel
select largest submodel produced by colmap for splats generation
2 parents b2b2196 + 8732615 commit a719a09

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ python convert.py -s <location> --skip_matching [--resize] #If not resizing, Ima
475475
Path to the COLMAP executable (```.bat``` on Windows).
476476
#### --magick_executable
477477
Path to the ImageMagick executable.
478+
#### --generate_text_model
479+
Generate text model in sparse/0 folder
478480
</details>
479481
<br>
480482

convert.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
parser.add_argument("--resize", action="store_true")
3232
parser.add_argument("--magick_executable", default="", type=str)
3333
parser.add_argument("--masks_path", type=str)
34+
parser.add_argument("--generate_text_model", action="store_true")
3435
args = parser.parse_args()
3536
colmap_command = '"{}"'.format(args.colmap_executable) if len(args.colmap_executable) > 0 else "colmap"
3637
magick_command = '"{}"'.format(args.magick_executable) if len(args.magick_executable) > 0 else "magick"
@@ -75,21 +76,35 @@
7576
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
7677
exit(EXIT_FAIL)
7778

78-
### Convert model to text format so we can read cameras
79-
convert_cmd = (colmap_command + " model_converter \
80-
--input_path " + args.source_path + "/distorted/sparse/0 \
81-
--output_path " + args.source_path + "/distorted/sparse/0 \
82-
--output_type TXT")
83-
exit_code = os.system(convert_cmd)
84-
if exit_code != 0:
85-
logging.error(f"Convert failed with code {exit_code}. Exiting.")
86-
exit(EXIT_FAIL)
79+
80+
# select the largest submodel
81+
i = 0
82+
largest_size = 0
83+
index = 0
84+
85+
while True:
86+
path = args.source_path + "/distorted/sparse/" + str(i)
87+
if not os.path.exists(path):
88+
break
89+
90+
# check the file size of images.bin
91+
images_bin = path + "/images.bin"
92+
size = os.path.getsize(images_bin)
93+
if size > largest_size:
94+
largest_size = size
95+
index = i
96+
97+
i += 1
98+
99+
str_index = str(index)
100+
distorted_sparse_path = args.source_path + "/distorted/sparse/" + str_index
101+
87102

88103
### Image undistortion
89104
## We need to undistort our images into ideal pinhole intrinsics.
90105
img_undist_cmd = (colmap_command + " image_undistorter \
91106
--image_path " + args.source_path + "/input \
92-
--input_path " + args.source_path + "/distorted/sparse/0 \
107+
--input_path " + distorted_sparse_path + " \
93108
--output_path " + args.source_path + "\
94109
--output_type COLMAP")
95110

@@ -110,7 +125,7 @@ def remove_dir_if_exist(path):
110125
# We need to "hack" colmap to undistort segmentation maps modify paths
111126
# First convert model to text format
112127
model_converter_cmd = (colmap_command + " model_converter \
113-
--input_path " + args.source_path + "/distorted/sparse/0 \
128+
--input_path " + distorted_sparse_path + " \
114129
--output_path " + args.source_path + "/alpha_distorted_sparse_txt/ \
115130
--output_type TXT")
116131
exit_code = os.system(model_converter_cmd)
@@ -162,11 +177,23 @@ def concat_alpha(seg_path):
162177
Path(f'{args.source_path}/sparse').replace(f'{args.source_path}/sparse_src/')
163178
Path(f'{args.source_path}/alpha_undistorted_sparse/sparse').replace(f'{args.source_path}/sparse/')
164179

180+
if args.generate_text_model:
181+
### Convert model to text format so we can read cameras
182+
convert_cmd = (colmap_command + " model_converter \
183+
--input_path " + args.source_path + "/sparse" + " \
184+
--output_path " + args.source_path + "/sparse" + " \
185+
--output_type TXT")
186+
exit_code = os.system(convert_cmd)
187+
if exit_code != 0:
188+
logging.error(f"Convert failed with code {exit_code}. Exiting.")
189+
exit(exit_code)
190+
191+
# move all files from sparse into sparse/0, as train.py expects it
165192
files = os.listdir(args.source_path + "/sparse")
166193
os.makedirs(args.source_path + "/sparse/0", exist_ok=True)
167194
# Copy each file from the source directory to the destination directory
168195
for file in files:
169-
if file == '0':
196+
if file == "0":
170197
continue
171198
source_file = os.path.join(args.source_path, "sparse", file)
172199
destination_file = os.path.join(args.source_path, "sparse", "0", file)

0 commit comments

Comments
 (0)