stpete2 commited on
Commit
9cd9530
·
verified ·
1 Parent(s): ba6e567

Update h5_to_db.py

Browse files
Files changed (1) hide show
  1. h5_to_db.py +42 -7
h5_to_db.py CHANGED
@@ -69,18 +69,39 @@ def create_camera(db, image_path, camera_model):
69
  return db.add_camera(model, width, height, param_arr)
70
 
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
- import glob
78
  all_images = {}
79
  for ext in ['.jpg', '.jpeg', '.png', '.JPG', '.JPEG', '.PNG']:
80
  for img_file in glob.glob(os.path.join(image_path, f'*{ext}')):
81
  base_name = os.path.splitext(os.path.basename(img_file))[0]
82
  all_images[base_name] = img_file
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  for filename in tqdm(list(keypoint_f.keys())):
85
  keypoints = keypoint_f[filename][()]
86
 
@@ -92,22 +113,35 @@ def add_keypoints(db, h5_path, image_path, img_ext, camera_model, single_camera=
92
 
93
  # カメラは1回だけ作成(single_camera=Trueの場合)
94
  if camera_id is None:
95
- from PIL import Image
96
  img = Image.open(path)
97
  width, height = img.size
98
 
99
  focal_length = float(max(width, height))
100
  cx = float(width / 2.0)
101
  cy = float(height / 2.0)
102
- k = 0.0
103
 
104
- print(f"カメラ作成: f={focal_length}, cx={cx}, cy={cy}, k={k}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  camera_id = db.add_camera(
107
- model=2, # SIMPLE_RADIAL
108
  width=width,
109
  height=height,
110
- params=np.array([focal_length, cx, cy, k], dtype=np.float64)
111
  )
112
 
113
  image_id = db.add_image(fname_with_ext, camera_id)
@@ -115,7 +149,8 @@ def add_keypoints(db, h5_path, image_path, img_ext, camera_model, single_camera=
115
  db.add_keypoints(image_id, keypoints)
116
 
117
  return fname_to_id
118
-
 
119
 
120
  def add_matches(db, h5_path, fname_to_id):
121
  """
 
69
  return db.add_camera(model, width, height, param_arr)
70
 
71
 
72
+
73
  def add_keypoints(db, h5_path, image_path, img_ext, camera_model, single_camera=True):
74
+ import h5py
75
+ import numpy as np
76
+ import os
77
+ import glob
78
+ from tqdm import tqdm
79
+ from PIL import Image
80
+
81
  keypoint_f = h5py.File(os.path.join(h5_path, 'keypoints.h5'), 'r')
82
  camera_id = None
83
  fname_to_id = {}
84
 
 
85
  all_images = {}
86
  for ext in ['.jpg', '.jpeg', '.png', '.JPG', '.JPEG', '.PNG']:
87
  for img_file in glob.glob(os.path.join(image_path, f'*{ext}')):
88
  base_name = os.path.splitext(os.path.basename(img_file))[0]
89
  all_images[base_name] = img_file
90
 
91
+ # ★★★ カメラモデルのマッピング ★★★
92
+ camera_model_ids = {
93
+ 'SIMPLE_PINHOLE': 0,
94
+ 'PINHOLE': 1,
95
+ 'SIMPLE_RADIAL': 2,
96
+ 'RADIAL': 3,
97
+ 'OPENCV': 4,
98
+ }
99
+
100
+ if camera_model not in camera_model_ids:
101
+ raise ValueError(f"Unknown camera model: {camera_model}")
102
+
103
+ model_id = camera_model_ids[camera_model]
104
+
105
  for filename in tqdm(list(keypoint_f.keys())):
106
  keypoints = keypoint_f[filename][()]
107
 
 
113
 
114
  # カメラは1回だけ作成(single_camera=Trueの場合)
115
  if camera_id is None:
 
116
  img = Image.open(path)
117
  width, height = img.size
118
 
119
  focal_length = float(max(width, height))
120
  cx = float(width / 2.0)
121
  cy = float(height / 2.0)
 
122
 
123
+ # ★★★ カメラモデルに応じてパラメータを設定 ★★★
124
+ if camera_model == 'PINHOLE':
125
+ # PINHOLE: [fx, fy, cx, cy]
126
+ params = np.array([focal_length, focal_length, cx, cy], dtype=np.float64)
127
+ print(f"カメラ作成: PINHOLE, f={focal_length}, cx={cx}, cy={cy}")
128
+ elif camera_model == 'SIMPLE_PINHOLE':
129
+ # SIMPLE_PINHOLE: [f, cx, cy]
130
+ params = np.array([focal_length, cx, cy], dtype=np.float64)
131
+ print(f"カメラ作成: SIMPLE_PINHOLE, f={focal_length}, cx={cx}, cy={cy}")
132
+ elif camera_model == 'SIMPLE_RADIAL':
133
+ # SIMPLE_RADIAL: [f, cx, cy, k]
134
+ k = 0.0
135
+ params = np.array([focal_length, cx, cy, k], dtype=np.float64)
136
+ print(f"カメラ作成: SIMPLE_RADIAL, f={focal_length}, cx={cx}, cy={cy}, k={k}")
137
+ else:
138
+ raise ValueError(f"Unsupported camera model: {camera_model}")
139
 
140
  camera_id = db.add_camera(
141
+ model=model_id, # ★★★ 引数に基づいて設定 ★★★
142
  width=width,
143
  height=height,
144
+ params=params
145
  )
146
 
147
  image_id = db.add_image(fname_with_ext, camera_id)
 
149
  db.add_keypoints(image_id, keypoints)
150
 
151
  return fname_to_id
152
+
153
+
154
 
155
  def add_matches(db, h5_path, fname_to_id):
156
  """