gaussian-splatting/convert.py

112 lines
4.4 KiB
Python
Raw Normal View History

#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
# xvdp removed magick, it is 3x slower than single threaded PIL for resizing
2023-07-04 08:00:48 +00:00
import os
import logging
2023-07-04 08:00:48 +00:00
from argparse import ArgumentParser
import shutil
from PIL import Image
2023-07-04 08:00:48 +00:00
# This Python script is based on the shell converter script provided in the MipNerF 360 repository.
parser = ArgumentParser("Colmap converter")
parser.add_argument("--no_gpu", action='store_true')
2023-07-10 15:58:17 +00:00
parser.add_argument("--skip_matching", action='store_true')
2023-07-04 08:00:48 +00:00
parser.add_argument("--source_path", "-s", required=True, type=str)
parser.add_argument("--camera", default="OPENCV", type=str)
parser.add_argument("--colmap_executable", default="", type=str)
parser.add_argument("--resize", action="store_true")
parser.add_argument("--magick_executable", default="", type=str)
args = parser.parse_args()
colmap_command = '"{}"'.format(args.colmap_executable) if len(args.colmap_executable) > 0 else "colmap"
2023-07-04 08:00:48 +00:00
use_gpu = 1 if not args.no_gpu else 0
2023-07-10 15:58:17 +00:00
if not args.skip_matching:
os.makedirs(args.source_path + "/distorted/sparse", exist_ok=True)
2023-07-04 08:00:48 +00:00
2023-07-10 15:58:17 +00:00
## Feature extraction
feat_extracton_cmd = colmap_command + " feature_extractor "\
2023-07-10 15:58:17 +00:00
"--database_path " + args.source_path + "/distorted/database.db \
--image_path " + args.source_path + "/input \
--ImageReader.single_camera 1 \
--ImageReader.camera_model " + args.camera + " \
--SiftExtraction.use_gpu " + str(use_gpu)
exit_code = os.system(feat_extracton_cmd)
if exit_code != 0:
logging.error(f"Feature extraction failed with code {exit_code}. Exiting.")
exit(exit_code)
2023-07-04 08:00:48 +00:00
2023-07-10 15:58:17 +00:00
## Feature matching
feat_matching_cmd = colmap_command + " exhaustive_matcher \
2023-07-10 15:58:17 +00:00
--database_path " + args.source_path + "/distorted/database.db \
--SiftMatching.use_gpu " + str(use_gpu)
exit_code = os.system(feat_matching_cmd)
if exit_code != 0:
logging.error(f"Feature matching failed with code {exit_code}. Exiting.")
exit(exit_code)
2023-07-04 08:00:48 +00:00
2023-07-10 15:58:17 +00:00
### Bundle adjustment
# The default Mapper tolerance is unnecessarily large,
# decreasing it speeds up bundle adjustment steps.
mapper_cmd = (colmap_command + " mapper \
2023-07-10 15:58:17 +00:00
--database_path " + args.source_path + "/distorted/database.db \
--image_path " + args.source_path + "/input \
--output_path " + args.source_path + "/distorted/sparse \
--Mapper.ba_global_function_tolerance=0.000001")
exit_code = os.system(mapper_cmd)
if exit_code != 0:
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
exit(exit_code)
2023-07-04 08:00:48 +00:00
### Image undistortion
## We need to undistort our images into ideal pinhole intrinsics.
img_undist_cmd = (colmap_command + " image_undistorter \
2023-07-04 08:00:48 +00:00
--image_path " + args.source_path + "/input \
--input_path " + args.source_path + "/distorted/sparse/0 \
--output_path " + args.source_path + "\
--output_type COLMAP")
exit_code = os.system(img_undist_cmd)
if exit_code != 0:
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
exit(exit_code)
2023-07-04 08:00:48 +00:00
files = os.listdir(args.source_path + "/sparse")
os.makedirs(args.source_path + "/sparse/0", exist_ok=True)
# Copy each file from the source directory to the destination directory
for file in files:
if file == '0':
continue
source_file = os.path.join(args.source_path, "sparse", file)
destination_file = os.path.join(args.source_path, "sparse", "0", file)
shutil.move(source_file, destination_file)
if args.resize:
2023-07-04 08:00:48 +00:00
print("Copying and resizing...")
# Resize images.
for div in [2,4,8]:
os.makedirs(args.source_path + f"/images_{div}", exist_ok=True)
2023-07-04 08:00:48 +00:00
# Get the list of files in the source directory
files = os.listdir(args.source_path + "/images")
# Copy each file from the source directory to the destination directory
for j, file in enumerate(files):
2023-07-04 08:00:48 +00:00
source_file = os.path.join(args.source_path, "images", file)
im = Image.open(source_file)
logging.info(f"processing image [{j}/{len(files)}] {source_file}")
for div in [2,4,8]:
destination_file = os.path.join(args.source_path, f"images_{div}", file)
im.resize([round(i/div) for i in im.size], Image.BICUBIC).save(destination_file)
2023-07-04 08:00:48 +00:00
print("Done.")