Absolute import and linting

This commit is contained in:
otthorn 2021-03-25 11:15:19 +01:00
parent 2f991920e6
commit f6debcb679

View file

@ -4,12 +4,13 @@ import typing
import numpy as np import numpy as np
from colmap_wrapper import run_colmap from nerf_homemade.poses.colmap_wrapper import run_colmap
import colmap_read_model as read_model import nerf_homemade.poses.colmap_read_model as read_model
FORMAT = "%(asctime)s %(levelname)s \t %(message)s" FORMAT = "%(asctime)s %(levelname)s \t %(message)s"
logging.basicConfig(format=FORMAT, level=logging.DEBUG) logging.basicConfig(format=FORMAT, level=logging.DEBUG)
def gen_poses(basedir: str, match_type: str = 'exhaustive') -> None: def gen_poses(basedir: str, match_type: str = 'exhaustive') -> None:
""" """
Geneate or retreive camera poses. Geneate or retreive camera poses.
@ -40,7 +41,8 @@ def gen_poses(basedir: str, match_type: str='exhaustive') -> None:
logging.info("Running COLMAP") logging.info("Running COLMAP")
run_colmap(basedir, match_type) run_colmap(basedir, match_type)
else: else:
logging.info("Files genreated by COLMAP found. Skipping running COLMAP.") logging.info(
"Files genreated by COLMAP found. Skipping running COLMAP.")
logging.debug("Loading COLMAP data") logging.debug("Loading COLMAP data")
poses, points_3d, perm = load_colmap_data(basedir) poses, points_3d, perm = load_colmap_data(basedir)
@ -48,6 +50,7 @@ def gen_poses(basedir: str, match_type: str='exhaustive') -> None:
logging.debug("Saving COLMAP data to npy") logging.debug("Saving COLMAP data to npy")
save_poses(basedir, poses, points_3d, perm) save_poses(basedir, poses, points_3d, perm)
def load_colmap_data(basedir: str) -> (np.ndarray, dict, np.ndarray): def load_colmap_data(basedir: str) -> (np.ndarray, dict, np.ndarray):
""" """
Load data from a COLMAP arborescence. Load data from a COLMAP arborescence.
@ -107,17 +110,20 @@ def load_colmap_data(basedir: str) -> (np.ndarray, dict, np.ndarray):
c2w_mats = np.linalg.inv(w2c_mats) c2w_mats = np.linalg.inv(w2c_mats)
poses = c2w_mats[:, :3, :4].transpose([1, 2, 0]) poses = c2w_mats[:, :3, :4].transpose([1, 2, 0])
poses = np.concatenate([poses, np.tile(hwf[..., np.newaxis], [1,1,poses.shape[-1]])], 1) poses = np.concatenate(
[poses, np.tile(hwf[..., np.newaxis], [1, 1, poses.shape[-1]])], 1)
# read 3d points data # read 3d points data
pts3d_file = os.path.join(basedir, "points3D.bin") pts3d_file = os.path.join(basedir, "points3D.bin")
pts3d = read_model.read_points3d_binary(pts3d_file) pts3d = read_model.read_points3d_binary(pts3d_file)
# must switch to [-u, r, -t] from [r, -u, t], NOT [r, u, -t] # must switch to [-u, r, -t] from [r, -u, t], NOT [r, u, -t]
poses = np.concatenate([poses[:, 1:2, :], poses[:, 0:1, :], -poses[:, 2:3, :], poses[:, 3:4, :], poses[:, 4:5, :]], 1) poses = np.concatenate([poses[:, 1:2, :], poses[:, 0:1, :], -
poses[:, 2:3, :], poses[:, 3:4, :], poses[:, 4:5, :]], 1)
return poses, pts3d, perm return poses, pts3d, perm
def save_poses(basedir, poses, pts3d, perm) -> None: def save_poses(basedir, poses, pts3d, perm) -> None:
""" """
Save the COLMAP data in a `.npy` format. Save the COLMAP data in a `.npy` format.
@ -142,7 +148,8 @@ def save_poses(basedir, poses, pts3d, perm) -> None:
cams = [0] * poses.shape[-1] cams = [0] * poses.shape[-1]
for ind in pts3d[k].image_ids: for ind in pts3d[k].image_ids:
if len(cams) < ind - 1: if len(cams) < ind - 1:
logging.error("The correct camera poses for current points cannot be accessed") logging.error(
"The correct camera poses for current points cannot be accessed")
return return
cams[ind - 1] = 1 cams[ind - 1] = 1
vis_arr.append(cams) vis_arr.append(cams)
@ -151,10 +158,11 @@ def save_poses(basedir, poses, pts3d, perm) -> None:
vis_arr = np.array(vis_arr) vis_arr = np.array(vis_arr)
logging.info(f"Points {pts_arr.shape} Visibility {vis_arr.shape}") logging.info(f"Points {pts_arr.shape} Visibility {vis_arr.shape}")
zvals = np.sum(-(pts_arr[:, np.newaxis, :].transpose([2,0,1]) - poses[:3, 3:4, :]) * poses[:3, 2:3, :], 0) zvals = np.sum(-(pts_arr[:, np.newaxis, :].transpose([2, 0, 1]
) - poses[:3, 3:4, :]) * poses[:3, 2:3, :], 0)
valid_z = zvals[vis_arr == 1] valid_z = zvals[vis_arr == 1]
logging.info(f"Depths stats - min: {valid_z.min()} max: {valid_z.max()} logging.info(
mean: {valid_z.mean()}") f"Depths stats - min: {valid_z.min()} max: {valid_z.max()} mean: {valid_z.mean()}")
save_arr = [] save_arr = []
for i in perm: for i in perm:
@ -163,7 +171,8 @@ def save_poses(basedir, poses, pts3d, perm) -> None:
zs = zs[vis == 1] zs = zs[vis == 1]
close_depth, inf_depth = np.percentile(zs, .1), np.percentile(zs, 99.9) close_depth, inf_depth = np.percentile(zs, .1), np.percentile(zs, 99.9)
save_arr.append(np.concatenate([poses[..., i].ravel(), np.array([close_depth, inf_depth])], 0)) save_arr.append(np.concatenate(
[poses[..., i].ravel(), np.array([close_depth, inf_depth])], 0))
save_arr = np.array(save_arr) save_arr = np.array(save_arr)
save_path = os.path.join(basedir, "poses_bounds.npy") save_path = os.path.join(basedir, "poses_bounds.npy")