stpete2 commited on
Commit
c66a1b9
·
verified ·
1 Parent(s): 28469be

Update h5_to_db.py

Browse files
Files changed (1) hide show
  1. h5_to_db.py +30 -10
h5_to_db.py CHANGED
@@ -71,25 +71,45 @@ def create_camera(db, image_path, camera_model):
71
 
72
  def add_keypoints(db, h5_path, image_path, img_ext, camera_model, single_camera = True):
73
  keypoint_f = h5py.File(os.path.join(h5_path, 'keypoints.h5'), 'r')
74
-
75
  camera_id = None
76
  fname_to_id = {}
 
 
 
 
 
 
 
 
 
77
  for filename in tqdm(list(keypoint_f.keys())):
78
  keypoints = keypoint_f[filename][()]
79
-
80
- fname_with_ext = filename + img_ext
81
- path = os.path.join(image_path, fname_with_ext)
82
- if not os.path.isfile(path):
83
- raise IOError(f'Invalid image path {path}')
84
-
 
 
85
  if camera_id is None or not single_camera:
86
- camera_id = create_camera(db, path, camera_model)
 
 
 
 
 
 
 
 
 
 
87
  image_id = db.add_image(fname_with_ext, camera_id)
88
  fname_to_id[filename] = image_id
89
-
90
  db.add_keypoints(image_id, keypoints)
91
-
92
  return fname_to_id
 
93
 
94
  def add_matches(db, h5_path, fname_to_id):
95
  match_file = h5py.File(os.path.join(h5_path, 'matches.h5'), 'r')
 
71
 
72
  def add_keypoints(db, h5_path, image_path, img_ext, camera_model, single_camera = True):
73
  keypoint_f = h5py.File(os.path.join(h5_path, 'keypoints.h5'), 'r')
 
74
  camera_id = None
75
  fname_to_id = {}
76
+
77
+ # 画像ディレクトリ内の全ファイルを取得
78
+ import glob
79
+ all_images = {}
80
+ for ext in ['.jpg', '.jpeg', '.png', '.JPG', '.JPEG', '.PNG']:
81
+ for img_file in glob.glob(os.path.join(image_path, f'*{ext}')):
82
+ base_name = os.path.splitext(os.path.basename(img_file))[0]
83
+ all_images[base_name] = img_file
84
+
85
  for filename in tqdm(list(keypoint_f.keys())):
86
  keypoints = keypoint_f[filename][()]
87
+
88
+ # 拡張子なしのファイル名で検索
89
+ if filename not in all_images:
90
+ raise IOError(f'Image not found for key: {filename}')
91
+
92
+ path = all_images[filename] # 実際のフルパス
93
+ fname_with_ext = os.path.basename(path) # 拡張子付きファイル名
94
+
95
  if camera_id is None or not single_camera:
96
+ from PIL import Image
97
+ img = Image.open(path)
98
+ width, height = img.size
99
+
100
+ camera_id = db.add_camera(
101
+ model=2, # SIMPLE_RADIAL
102
+ width=width,
103
+ height=height,
104
+ params=np.array([0.0, 0.0, 0.0, 0.0]) # COLMAPが最適化
105
+ )
106
+
107
  image_id = db.add_image(fname_with_ext, camera_id)
108
  fname_to_id[filename] = image_id
 
109
  db.add_keypoints(image_id, keypoints)
110
+
111
  return fname_to_id
112
+
113
 
114
  def add_matches(db, h5_path, fname_to_id):
115
  match_file = h5py.File(os.path.join(h5_path, 'matches.h5'), 'r')