Upload convert.py with huggingface_hub
Browse files- convert.py +124 -0
convert.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Copyright (C) 2023, Inria
|
| 3 |
+
# GRAPHDECO research group, https://team.inria.fr/graphdeco
|
| 4 |
+
# All rights reserved.
|
| 5 |
+
#
|
| 6 |
+
# This software is free for non-commercial, research and evaluation use
|
| 7 |
+
# under the terms of the LICENSE.md file.
|
| 8 |
+
#
|
| 9 |
+
# For inquiries contact george.drettakis@inria.fr
|
| 10 |
+
#
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import logging
|
| 14 |
+
from argparse import ArgumentParser
|
| 15 |
+
import shutil
|
| 16 |
+
|
| 17 |
+
# This Python script is based on the shell converter script provided in the MipNerF 360 repository.
|
| 18 |
+
parser = ArgumentParser("Colmap converter")
|
| 19 |
+
parser.add_argument("--no_gpu", action='store_true')
|
| 20 |
+
parser.add_argument("--skip_matching", action='store_true')
|
| 21 |
+
parser.add_argument("--source_path", "-s", required=True, type=str)
|
| 22 |
+
parser.add_argument("--camera", default="OPENCV", type=str)
|
| 23 |
+
parser.add_argument("--colmap_executable", default="", type=str)
|
| 24 |
+
parser.add_argument("--resize", action="store_true")
|
| 25 |
+
parser.add_argument("--magick_executable", default="", type=str)
|
| 26 |
+
args = parser.parse_args()
|
| 27 |
+
colmap_command = '"{}"'.format(args.colmap_executable) if len(args.colmap_executable) > 0 else "colmap"
|
| 28 |
+
magick_command = '"{}"'.format(args.magick_executable) if len(args.magick_executable) > 0 else "magick"
|
| 29 |
+
use_gpu = 1 if not args.no_gpu else 0
|
| 30 |
+
|
| 31 |
+
if not args.skip_matching:
|
| 32 |
+
os.makedirs(args.source_path + "/distorted/sparse", exist_ok=True)
|
| 33 |
+
|
| 34 |
+
## Feature extraction
|
| 35 |
+
feat_extracton_cmd = colmap_command + " feature_extractor "\
|
| 36 |
+
"--database_path " + args.source_path + "/distorted/database.db \
|
| 37 |
+
--image_path " + args.source_path + "/input \
|
| 38 |
+
--ImageReader.single_camera 1 \
|
| 39 |
+
--ImageReader.camera_model " + args.camera + " \
|
| 40 |
+
--SiftExtraction.use_gpu " + str(use_gpu)
|
| 41 |
+
exit_code = os.system(feat_extracton_cmd)
|
| 42 |
+
if exit_code != 0:
|
| 43 |
+
logging.error(f"Feature extraction failed with code {exit_code}. Exiting.")
|
| 44 |
+
exit(exit_code)
|
| 45 |
+
|
| 46 |
+
## Feature matching
|
| 47 |
+
feat_matching_cmd = colmap_command + " exhaustive_matcher \
|
| 48 |
+
--database_path " + args.source_path + "/distorted/database.db \
|
| 49 |
+
--SiftMatching.use_gpu " + str(use_gpu)
|
| 50 |
+
exit_code = os.system(feat_matching_cmd)
|
| 51 |
+
if exit_code != 0:
|
| 52 |
+
logging.error(f"Feature matching failed with code {exit_code}. Exiting.")
|
| 53 |
+
exit(exit_code)
|
| 54 |
+
|
| 55 |
+
### Bundle adjustment
|
| 56 |
+
# The default Mapper tolerance is unnecessarily large,
|
| 57 |
+
# decreasing it speeds up bundle adjustment steps.
|
| 58 |
+
mapper_cmd = (colmap_command + " mapper \
|
| 59 |
+
--database_path " + args.source_path + "/distorted/database.db \
|
| 60 |
+
--image_path " + args.source_path + "/input \
|
| 61 |
+
--output_path " + args.source_path + "/distorted/sparse \
|
| 62 |
+
--Mapper.ba_global_function_tolerance=0.000001")
|
| 63 |
+
exit_code = os.system(mapper_cmd)
|
| 64 |
+
if exit_code != 0:
|
| 65 |
+
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
|
| 66 |
+
exit(exit_code)
|
| 67 |
+
|
| 68 |
+
### Image undistortion
|
| 69 |
+
## We need to undistort our images into ideal pinhole intrinsics.
|
| 70 |
+
img_undist_cmd = (colmap_command + " image_undistorter \
|
| 71 |
+
--image_path " + args.source_path + "/input \
|
| 72 |
+
--input_path " + args.source_path + "/distorted/sparse/0 \
|
| 73 |
+
--output_path " + args.source_path + "\
|
| 74 |
+
--output_type COLMAP")
|
| 75 |
+
exit_code = os.system(img_undist_cmd)
|
| 76 |
+
if exit_code != 0:
|
| 77 |
+
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
|
| 78 |
+
exit(exit_code)
|
| 79 |
+
|
| 80 |
+
files = os.listdir(args.source_path + "/sparse")
|
| 81 |
+
os.makedirs(args.source_path + "/sparse/0", exist_ok=True)
|
| 82 |
+
# Copy each file from the source directory to the destination directory
|
| 83 |
+
for file in files:
|
| 84 |
+
if file == '0':
|
| 85 |
+
continue
|
| 86 |
+
source_file = os.path.join(args.source_path, "sparse", file)
|
| 87 |
+
destination_file = os.path.join(args.source_path, "sparse", "0", file)
|
| 88 |
+
shutil.move(source_file, destination_file)
|
| 89 |
+
|
| 90 |
+
if(args.resize):
|
| 91 |
+
print("Copying and resizing...")
|
| 92 |
+
|
| 93 |
+
# Resize images.
|
| 94 |
+
os.makedirs(args.source_path + "/images_2", exist_ok=True)
|
| 95 |
+
os.makedirs(args.source_path + "/images_4", exist_ok=True)
|
| 96 |
+
os.makedirs(args.source_path + "/images_8", exist_ok=True)
|
| 97 |
+
# Get the list of files in the source directory
|
| 98 |
+
files = os.listdir(args.source_path + "/images")
|
| 99 |
+
# Copy each file from the source directory to the destination directory
|
| 100 |
+
for file in files:
|
| 101 |
+
source_file = os.path.join(args.source_path, "images", file)
|
| 102 |
+
|
| 103 |
+
destination_file = os.path.join(args.source_path, "images_2", file)
|
| 104 |
+
shutil.copy2(source_file, destination_file)
|
| 105 |
+
exit_code = os.system(magick_command + " mogrify -resize 50% " + destination_file)
|
| 106 |
+
if exit_code != 0:
|
| 107 |
+
logging.error(f"50% resize failed with code {exit_code}. Exiting.")
|
| 108 |
+
exit(exit_code)
|
| 109 |
+
|
| 110 |
+
destination_file = os.path.join(args.source_path, "images_4", file)
|
| 111 |
+
shutil.copy2(source_file, destination_file)
|
| 112 |
+
exit_code = os.system(magick_command + " mogrify -resize 25% " + destination_file)
|
| 113 |
+
if exit_code != 0:
|
| 114 |
+
logging.error(f"25% resize failed with code {exit_code}. Exiting.")
|
| 115 |
+
exit(exit_code)
|
| 116 |
+
|
| 117 |
+
destination_file = os.path.join(args.source_path, "images_8", file)
|
| 118 |
+
shutil.copy2(source_file, destination_file)
|
| 119 |
+
exit_code = os.system(magick_command + " mogrify -resize 12.5% " + destination_file)
|
| 120 |
+
if exit_code != 0:
|
| 121 |
+
logging.error(f"12.5% resize failed with code {exit_code}. Exiting.")
|
| 122 |
+
exit(exit_code)
|
| 123 |
+
|
| 124 |
+
print("Done.")
|