因为我采用colmap重建相机位姿,所以我认为可以直接使用重建位姿的colmap模型,因此修改了run.py的代码。但当我这样做时,从fuse.ply.vis文件中读取信息的代码会报错,请问其中的原因是什么?或者,从fuse.ply.vis文件中读取信息的代码的作用是什么,能否提供注释?谢谢!
这是修改后的run.py主要代码
`for scene_id in ['214']:
source = f'/data8T/ydf/manhattan_sdf/data/tmp/{scene_id}/' # TODO: modify this to your path
target = f'/data8T/ydf/manhattan_sdf/data/{scene_id}'
os.makedirs(f'{target}/images', exist_ok=True)
os.makedirs(f'{target}/pose', exist_ok=True)
os.makedirs(f'{target}/depth_patchmatch', exist_ok=True)
if not os.path.exists(f'{source}/images/10.jpg'):
sortFile(source)
colmap_path = "colmap"
with open(f'{source}/colmap_output.txt', 'a') as f:
feature_extractor_args = [
colmap_path, 'feature_extractor',
'--database_path', os.path.join(source, "database.db"),
'--image_path', os.path.join(source, "images"),
'--ImageReader.single_camera', '1',
# '--ImageReader.mask_path', os.path.join(basedir, args.masks),
]
feat_output = (subprocess.check_output(feature_extractor_args, universal_newlines=True))
f.writelines(feat_output)
print('Features extracted')
exhaustive_matcher_args = [
colmap_path, 'exhaustive_matcher',
'--database_path', os.path.join(source, "database.db"),
]
exh_output = (subprocess.check_output(exhaustive_matcher_args, universal_newlines=True))
f.writelines(exh_output)
print('Exhaustive matched')
p = os.path.join(source, 'sparse')
if not os.path.exists(p):
os.makedirs(p)
mapper_args = [
colmap_path, 'hierarchical_mapper',
'--database_path', os.path.join(source, "database.db"),
'--image_path', os.path.join(source, "images"),
# '--ImageReader.mask_path', os.path.join(basedir, args.masks),
'--output_path', os.path.join(source, 'sparse'),
]
map_output = (subprocess.check_output(mapper_args, universal_newlines=True))
f.writelines(map_output)
print('Sparse map created')
os.makedirs(f'{source}/dense/', exist_ok=True)
danse_args = [
colmap_path, 'image_undistorter',
'--image_path', os.path.join(source, "images"),
# '--ImageReader.mask_path', os.path.join(basedir, args.masks),
'--input_path', os.path.join(source, 'sparse', '0'),
'--output_path', os.path.join(source, 'dense'),
'--output_type', 'COLMAP',
]
danse_output = (subprocess.check_output(danse_args, universal_newlines=True))
f.writelines(danse_output)
print('Danse map created')
patch_args = [
colmap_path, 'patch_match_stereo',
'--workspace_path', os.path.join(source, 'dense'),
'--workspace_format', 'COLMAP',
'--PatchMatchStereo.cache_size', '64',
]
patch_output = (subprocess.check_output(patch_args, universal_newlines=True))
f.writelines(patch_output)
print('Patch match stereo succeed')
stereo_args = [
colmap_path, 'stereo_fusion',
'--workspace_path', os.path.join(source, 'dense'),
'--workspace_format', 'COLMAP',
'--input_type', 'geometric',
'--output_path', os.path.join(source, 'dense', 'fused.ply'),
'--StereoFusion.cache_size', '64',
]
stereo_output = (subprocess.check_output(stereo_args, universal_newlines=True))
f.writelines(stereo_output)
print('Stereo fusion succeed')
load_save_pose(source)
npy2one(source)
id_list = os.listdir(f'{source}/images')
id_list = [id[:-4] for id in id_list if id.endswith('0.jpg')]
id_list.sort(key=lambda _: int(_))
pose_dict = dict()
for id in id_list:
pose_dict[id] = np.loadtxt(source + f'pose/{id}.txt')
id_list = [id for id in id_list if not np.isinf(pose_dict[id]).any()]
id_list.sort()
translation_list = []
for id in id_list:
translation_list.append(pose_dict[id][None, :3, 3])
translation_list = np.concatenate(translation_list)
translation_center = (translation_list.max(axis=0) + translation_list.min(axis=0)) / 2
translation_list -= translation_center
max_cam_norm = np.linalg.norm(translation_list, axis=1).max()
scale = (scale_radius / max_cam_norm / 1.1)
for id in id_list:
pose_dict[id][:3, 3] -= translation_center
pose_dict[id][:3, 3] *= scale
with open(f'{source}/offset.txt', 'w') as f:
f.write(f'{translation_center}')
with open(f'{source}/scale.txt', 'w') as f:
f.write(f'{scale}')
os.system(f'cp {source}/intrinsic.txt {target}/intrinsic.txt')
for id in tqdm(id_list):
color = cv2.imread(f'{source}/images/{id}.jpg')
color = cv2.resize(color, (width, height))
cv2.imwrite(f'{target}/images/{id}.png', color)
np.savetxt(f'{target}/pose/{id}.txt', pose_dict[id])
intrinsic = np.loadtxt(f'{target}/intrinsic.txt')
images_bin_path = f'{source}/sparse/0/images.bin'
images = read_images_binary(images_bin_path)
names = [_[1].name for _ in images.items()]
shape = (height, width)
ply_vis_path = f'{source}/dense/fused.ply.vis'
assert os.path.exists(ply_vis_path)
masks = [np.zeros(shape, dtype=np.uint8) for name in names]
load_point_vis(ply_vis_path, masks)
for name, mask in tqdm(zip(names, masks)):
depth_bin_path = f'{source}/dense/stereo/depth_maps/{name}.geometric.bin'
if not os.path.exists(depth_bin_path):
continue
depth_fname = depth_bin_path
depth = read_array(depth_fname)
depth[mask == 0] = 0
np.save(f'{target}/depth_patchmatch/{name[:-4]}.npy', depth)`
这是报错信息
Traceback (most recent call last): File "/home/ydf/manhattan_sdf/docs/run_colmap/run.py", line 179, in <module> load_point_vis(ply_vis_path, masks) File "/home/ydf/manhattan_sdf/docs/run_colmap/run.py", line 33, in load_point_vis idx, u, v = struct.unpack('<III', f.read(4 * 3)) struct.error: unpack requires a buffer of 12 bytes
其中的原因在于,按照你们提供的代码要求读取文件时,文件大小不匹配,导致不能读到更多信息