From 33897d066138318c87fa33f70685927d4d5a4f4a Mon Sep 17 00:00:00 2001 From: Thomas Van Iseghem Date: Sat, 5 Nov 2022 15:56:42 +0100 Subject: [PATCH 1/8] Added num_threads argument preventing system freeze Without this, colmap will use up all available resources. This results in the system freezing due to no resources available. Adding this argument caps the resources and allows the system to function as normal. --- scripts/colmap2nerf.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/colmap2nerf.py b/scripts/colmap2nerf.py index b4f463e0a..9e6b54dcb 100755 --- a/scripts/colmap2nerf.py +++ b/scripts/colmap2nerf.py @@ -38,6 +38,7 @@ def parse_args(): parser.add_argument("--keep_colmap_coords", action="store_true", help="keep transforms.json in COLMAP's original frame of reference (this will avoid reorienting and repositioning the scene for preview and rendering)") parser.add_argument("--out", default="transforms.json", help="output path") parser.add_argument("--vocab_path", default="", help="vocabulary tree path") + parser.add_argument("--num_threads", default=-1, help="number of threads to use for colmap") args = parser.parse_args() return args @@ -85,7 +86,7 @@ def run_colmap(args): sys.exit(1) if os.path.exists(db): os.remove(db) - do_system(f"colmap feature_extractor --ImageReader.camera_model {args.colmap_camera_model} --ImageReader.camera_params \"{args.colmap_camera_params}\" --SiftExtraction.estimate_affine_shape=true --SiftExtraction.domain_size_pooling=true --ImageReader.single_camera 1 --database_path {db} --image_path {images}") + do_system(f"colmap feature_extractor --ImageReader.camera_model {args.colmap_camera_model} --ImageReader.camera_params \"{args.colmap_camera_params}\" --SiftExtraction.estimate_affine_shape=true --SiftExtraction.domain_size_pooling=true, --SiftExtraction.num_threads {args.num_threads} --ImageReader.single_camera 1 --database_path {db} --image_path {images}") match_cmd = f"colmap {args.colmap_matcher}_matcher --SiftMatching.guided_matching=true --database_path {db}" if args.vocab_path: match_cmd += f" --VocabTreeMatching.vocab_tree_path {args.vocab_path}" @@ -327,5 +328,6 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays f["transform_matrix"] = f["transform_matrix"].tolist() print(nframes,"frames") print(f"writing {OUT_PATH}") + with open(OUT_PATH, "w") as outfile: json.dump(out, outfile, indent=2) From a8b9c44286b1e71c1cf656d556e5dc879fc8b5e3 Mon Sep 17 00:00:00 2001 From: Thomas Van Iseghem Date: Sat, 5 Nov 2022 16:00:25 +0100 Subject: [PATCH 2/8] Automatically create transform.json file in specified path Before you had to specify the path with "transform.json" at the end, else the script went in error. Now "transform.json" will be appended to the path resulting in it being created without specifying it every time. --- scripts/colmap2nerf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/colmap2nerf.py b/scripts/colmap2nerf.py index 9e6b54dcb..152ac338f 100755 --- a/scripts/colmap2nerf.py +++ b/scripts/colmap2nerf.py @@ -36,7 +36,7 @@ def parse_args(): parser.add_argument("--aabb_scale", default=16, choices=["1","2","4","8","16"], help="large scene scale factor. 1=scene fits in unit cube; power of 2 up to 16") parser.add_argument("--skip_early", default=0, help="skip this many images from the start") parser.add_argument("--keep_colmap_coords", action="store_true", help="keep transforms.json in COLMAP's original frame of reference (this will avoid reorienting and repositioning the scene for preview and rendering)") - parser.add_argument("--out", default="transforms.json", help="output path") + parser.add_argument("--out", default="", help="output path") parser.add_argument("--vocab_path", default="", help="vocabulary tree path") parser.add_argument("--num_threads", default=-1, help="number of threads to use for colmap") args = parser.parse_args() @@ -166,7 +166,7 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays SKIP_EARLY = int(args.skip_early) IMAGE_FOLDER = args.images TEXT_FOLDER = args.text - OUT_PATH = args.out + OUT_PATH = args.out + "transforms.json" print(f"outputting to {OUT_PATH}...") with open(os.path.join(TEXT_FOLDER,"cameras.txt"), "r") as f: angle_x = math.pi / 2 From 2e84c012c4bfb95a77b3731bfbe4724f73cd9dc2 Mon Sep 17 00:00:00 2001 From: Thomas Van Iseghem Date: Sat, 5 Nov 2022 16:01:36 +0100 Subject: [PATCH 3/8] Don't add custom datasets to version control --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0b222e856..0437b7890 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ __pycache__ .DS_Store imgui.ini +data/nerf/custom/* From 2618a21a0efe9eb3c0de916161a4e371fd4f5170 Mon Sep 17 00:00:00 2001 From: Thomas Van Iseghem Date: Sat, 5 Nov 2022 16:04:22 +0100 Subject: [PATCH 4/8] Ignore all colmap output files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 0437b7890..86fce98ec 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ __pycache__ .DS_Store imgui.ini data/nerf/custom/* +*colmap_sparse +*colmap_text +*colmap.db From 314656c4b0408e347b24aece6d938fb005b215d5 Mon Sep 17 00:00:00 2001 From: Thomas Van Iseghem Date: Sat, 5 Nov 2022 17:10:33 +0100 Subject: [PATCH 5/8] Fixed transforms.json wrong file_path All path locations are handled to put the transforms.json in the right location and populate the frames-object with the right image file_path. Also wrapped them is a os.normpath to ensure cross platform functionallity. This might not be necesarry but is a good assurance. --- scripts/colmap2nerf.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/scripts/colmap2nerf.py b/scripts/colmap2nerf.py index 152ac338f..7dd9497c6 100755 --- a/scripts/colmap2nerf.py +++ b/scripts/colmap2nerf.py @@ -36,7 +36,6 @@ def parse_args(): parser.add_argument("--aabb_scale", default=16, choices=["1","2","4","8","16"], help="large scene scale factor. 1=scene fits in unit cube; power of 2 up to 16") parser.add_argument("--skip_early", default=0, help="skip this many images from the start") parser.add_argument("--keep_colmap_coords", action="store_true", help="keep transforms.json in COLMAP's original frame of reference (this will avoid reorienting and repositioning the scene for preview and rendering)") - parser.add_argument("--out", default="", help="output path") parser.add_argument("--vocab_path", default="", help="vocabulary tree path") parser.add_argument("--num_threads", default=-1, help="number of threads to use for colmap") args = parser.parse_args() @@ -158,15 +157,24 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays if __name__ == "__main__": args = parse_args() + + AABB_SCALE = int(args.aabb_scale) + SKIP_EARLY = int(args.skip_early) + IMAGE_FOLDER = os.path.normpath(args.images) + print(f"using images from {IMAGE_FOLDER}") + TEXT_FOLDER = os.path.normpath(args.text) + OUT_PATH = os.path.dirname(IMAGE_FOLDER) + "/transforms.json" + OUT_PATH = os.path.normpath(OUT_PATH) + + # Replace args with the normalized paths + args.images = IMAGE_FOLDER + args.text = TEXT_FOLDER + if args.video_in != "": run_ffmpeg(args) if args.run_colmap: run_colmap(args) - AABB_SCALE = int(args.aabb_scale) - SKIP_EARLY = int(args.skip_early) - IMAGE_FOLDER = args.images - TEXT_FOLDER = args.text - OUT_PATH = args.out + "transforms.json" + print(f"outputting to {OUT_PATH}...") with open(os.path.join(TEXT_FOLDER,"cameras.txt"), "r") as f: angle_x = math.pi / 2 @@ -253,10 +261,11 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays elems=line.split(" ") # 1-4 is quat, 5-7 is trans, 9ff is filename (9, if filename contains no spaces) #name = str(PurePosixPath(Path(IMAGE_FOLDER, elems[9]))) # why is this requireing a relitive path while using ^ + image_rel = os.path.relpath(IMAGE_FOLDER) name = str(f"./{image_rel}/{'_'.join(elems[9:])}") - b=sharpness(name) - print(name, "sharpness=",b) + b = sharpness(name) + print(name, "sharpness=", b) image_id = int(elems[0]) qvec = np.array(tuple(map(float, elems[1:5]))) tvec = np.array(tuple(map(float, elems[5:8]))) @@ -272,7 +281,10 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays up += c2w[0:3,1] - frame={"file_path":name,"sharpness":b,"transform_matrix": c2w} + local_file_path = str(f"./{os.path.basename(IMAGE_FOLDER)}/{'_'.join(elems[9:])}") + local_file_path = os.path.normpath(local_file_path) + + frame={"file_path":local_file_path,"sharpness":b,"transform_matrix": c2w} out["frames"].append(frame) nframes = len(out["frames"]) From c319b7f00c59971043192fafc8c9a2916d19d2ff Mon Sep 17 00:00:00 2001 From: Thomas Van Iseghem Date: Mon, 7 Nov 2022 09:02:55 +0100 Subject: [PATCH 6/8] [Fix] Video path loading also needed adjustment --- scripts/colmap2nerf.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/scripts/colmap2nerf.py b/scripts/colmap2nerf.py index 7dd9497c6..01f59910b 100755 --- a/scripts/colmap2nerf.py +++ b/scripts/colmap2nerf.py @@ -161,20 +161,27 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays AABB_SCALE = int(args.aabb_scale) SKIP_EARLY = int(args.skip_early) IMAGE_FOLDER = os.path.normpath(args.images) - print(f"using images from {IMAGE_FOLDER}") TEXT_FOLDER = os.path.normpath(args.text) - OUT_PATH = os.path.dirname(IMAGE_FOLDER) + "/transforms.json" - OUT_PATH = os.path.normpath(OUT_PATH) + VIDEO_FOLDER = os.path.dirname(args.video_in) # Replace args with the normalized paths args.images = IMAGE_FOLDER args.text = TEXT_FOLDER if args.video_in != "": + print("Extracting frames from video: " + args.video_in) + # The image folder is set here so that it can be accesed later on when building the transforms.json file + IMAGE_FOLDER = os.path.normpath(os.path.join(VIDEO_FOLDER, "images")) run_ffmpeg(args) + else: + print("Using images from", IMAGE_FOLDER) + if args.run_colmap: run_colmap(args) + OUT_PATH = os.path.dirname(IMAGE_FOLDER) + "/transforms.json" + OUT_PATH = os.path.normpath(OUT_PATH) + print(f"outputting to {OUT_PATH}...") with open(os.path.join(TEXT_FOLDER,"cameras.txt"), "r") as f: angle_x = math.pi / 2 From 72f81a1720af77e618be424453986a8d22cfde00 Mon Sep 17 00:00:00 2001 From: Thomas Van Iseghem Date: Sat, 19 Nov 2022 17:37:54 +0100 Subject: [PATCH 7/8] Transform.json file now has functional posixpath + optional out flag --- scripts/colmap2nerf.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/scripts/colmap2nerf.py b/scripts/colmap2nerf.py index 01f59910b..3e5b6db1b 100755 --- a/scripts/colmap2nerf.py +++ b/scripts/colmap2nerf.py @@ -37,6 +37,7 @@ def parse_args(): parser.add_argument("--skip_early", default=0, help="skip this many images from the start") parser.add_argument("--keep_colmap_coords", action="store_true", help="keep transforms.json in COLMAP's original frame of reference (this will avoid reorienting and repositioning the scene for preview and rendering)") parser.add_argument("--vocab_path", default="", help="vocabulary tree path") + parser.add_argument("--out", default="", help="output path") parser.add_argument("--num_threads", default=-1, help="number of threads to use for colmap") args = parser.parse_args() return args @@ -179,8 +180,9 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays if args.run_colmap: run_colmap(args) - OUT_PATH = os.path.dirname(IMAGE_FOLDER) + "/transforms.json" - OUT_PATH = os.path.normpath(OUT_PATH) + if(args.out == ""): + OUT_PATH = os.path.dirname(IMAGE_FOLDER) + "/transforms.json" + OUT_PATH = os.path.normpath(OUT_PATH) print(f"outputting to {OUT_PATH}...") with open(os.path.join(TEXT_FOLDER,"cameras.txt"), "r") as f: @@ -270,7 +272,7 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays # why is this requireing a relitive path while using ^ image_rel = os.path.relpath(IMAGE_FOLDER) - name = str(f"./{image_rel}/{'_'.join(elems[9:])}") + name = os.path.normpath(str(f"./{image_rel}/{'_'.join(elems[9:])}")) b = sharpness(name) print(name, "sharpness=", b) image_id = int(elems[0]) @@ -290,6 +292,9 @@ def closest_point_2_lines(oa, da, ob, db): # returns point closest to both rays local_file_path = str(f"./{os.path.basename(IMAGE_FOLDER)}/{'_'.join(elems[9:])}") local_file_path = os.path.normpath(local_file_path) + + #Convert it from OS native path to Posix path + local_file_path = str(PurePosixPath(Path(local_file_path))) frame={"file_path":local_file_path,"sharpness":b,"transform_matrix": c2w} out["frames"].append(frame) From b6591ebbec942798d148e5ef0c148f378b44792f Mon Sep 17 00:00:00 2001 From: Thomas Van Iseghem Date: Sat, 19 Nov 2022 17:38:52 +0100 Subject: [PATCH 8/8] Removed custom folder entry from gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 86fce98ec..af096317d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,7 +13,6 @@ __pycache__ .DS_Store imgui.ini -data/nerf/custom/* *colmap_sparse *colmap_text *colmap.db