convert.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import os
  2. from argparse import ArgumentParser
  3. import shutil
  4. # This Python script is based on the shell converter script provided in the MipNerF 360 repository.
  5. parser = ArgumentParser("Colmap converter")
  6. parser.add_argument("--no_gpu", action='store_true')
  7. parser.add_argument("--source_path", "-s", required=True, type=str)
  8. parser.add_argument("--camera", default="OPENCV", type=str)
  9. parser.add_argument("--colmap_executable", default="", type=str)
  10. parser.add_argument("--resize", action="store_true")
  11. parser.add_argument("--magick_executable", default="", type=str)
  12. args = parser.parse_args()
  13. colmap_command = '"{}"'.format(args.colmap_executable) if len(args.colmap_executable) > 0 else "colmap"
  14. magick_command = '"{}"'.format(args.magick_executable) if len(args.magick_executable) > 0 else "magick"
  15. use_gpu = 1 if not args.no_gpu else 0
  16. os.makedirs(args.source_path + "/distorted/sparse", exist_ok=True)
  17. ## Feature extraction
  18. os.system(colmap_command + " feature_extractor "\
  19. "--database_path " + args.source_path + "/distorted/database.db \
  20. --image_path " + args.source_path + "/input \
  21. --ImageReader.single_camera 1 \
  22. --ImageReader.camera_model " + args.camera + " \
  23. --SiftExtraction.use_gpu " + str(use_gpu))
  24. ## Feature matching
  25. os.system(colmap_command + " exhaustive_matcher \
  26. --database_path " + args.source_path + "/distorted/database.db \
  27. --SiftMatching.use_gpu " + str(use_gpu))
  28. ### Bundle adjustment
  29. # The default Mapper tolerance is unnecessarily large,
  30. # decreasing it speeds up bundle adjustment steps.
  31. os.system(colmap_command + " mapper \
  32. --database_path " + args.source_path + "/distorted/database.db \
  33. --image_path " + args.source_path + "/input \
  34. --output_path " + args.source_path + "/distorted/sparse \
  35. --Mapper.ba_global_function_tolerance=0.000001")
  36. ### Image undistortion
  37. ## We need to undistort our images into ideal pinhole intrinsics.
  38. os.system(colmap_command + " image_undistorter \
  39. --image_path " + args.source_path + "/input \
  40. --input_path " + args.source_path + "/distorted/sparse/0 \
  41. --output_path " + args.source_path + "\
  42. --output_type COLMAP")
  43. files = os.listdir(args.source_path + "/sparse")
  44. os.makedirs(args.source_path + "/sparse/0", exist_ok=True)
  45. # Copy each file from the source directory to the destination directory
  46. for file in files:
  47. if file == '0':
  48. continue
  49. source_file = os.path.join(args.source_path, "sparse", file)
  50. destination_file = os.path.join(args.source_path, "sparse", "0", file)
  51. shutil.move(source_file, destination_file)
  52. if(args.resize):
  53. print("Copying and resizing...")
  54. # Resize images.
  55. os.makedirs(args.source_path + "/images_2", exist_ok=True)
  56. os.makedirs(args.source_path + "/images_4", exist_ok=True)
  57. os.makedirs(args.source_path + "/images_8", exist_ok=True)
  58. # Get the list of files in the source directory
  59. files = os.listdir(args.source_path + "/images")
  60. # Copy each file from the source directory to the destination directory
  61. for file in files:
  62. source_file = os.path.join(args.source_path, "images", file)
  63. destination_file = os.path.join(args.source_path, "images_2", file)
  64. shutil.copy2(source_file, destination_file)
  65. os.system(magick_command + " mogrify -resize 50% " + destination_file)
  66. destination_file = os.path.join(args.source_path, "images_4", file)
  67. shutil.copy2(source_file, destination_file)
  68. os.system(magick_command + " mogrify -resize 25% " + destination_file)
  69. destination_file = os.path.join(args.source_path, "images_8", file)
  70. shutil.copy2(source_file, destination_file)
  71. os.system(magick_command + " mogrify -resize 12.5% " + destination_file)
  72. print("Done.")