-
Notifications
You must be signed in to change notification settings - Fork 30
/
pose_estimation.py
148 lines (134 loc) · 5.09 KB
/
pose_estimation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import pickle
from pathlib import Path
import numpy as np
import poselib
import pycolmap
from siclib.models.extractor import VP
from .models.extractor import GeoCalib
from .utils.image import load_image
# flake8: noqa
# mypy: ignore-errors
class AbsolutePoseEstimator:
default_opts = {
"ransac": "poselib_gravity", # pycolmap, poselib, poselib_gravity
"refinement": "pycolmap_gravity", # pycolmap, pycolmap_gravity, none
"gravity_weight": 50000,
"max_reproj_error": 48.0,
"loss_function_scale": 1.0,
"use_vp": False,
"max_uncertainty": 10.0 / 180.0 * 3.1415, # radians
"cache_path": "../../outputs/inloc/calib.pickle",
}
def __init__(self, pose_opts=None):
pose_opts = {} if pose_opts is None else pose_opts
self.opts = {**self.default_opts, **pose_opts}
self.device = "cuda"
if self.opts["use_vp"]:
self.calib = VP().to(self.device)
self.cache_path = str(self.opts["cache_path"]).replace(".pickle", "_vp.pickle")
else:
self.calib = GeoCalib().to(self.device)
self.cache_path = str(self.opts["cache_path"])
# self.read_cache()
self.cache = {}
def read_cache(self):
print(f"Reading cache from {self.cache_path} ({Path(self.cache_path).exists()})")
if not Path(self.cache_path).exists():
self.cache = {}
return
with open(self.cache_path, "rb") as handle:
self.cache = pickle.load(handle)
def write_cache(self):
with open(self.cache_path, "wb") as handle:
pickle.dump(self.cache, handle, protocol=pickle.HIGHEST_PROTOCOL)
def __call__(self, query_path, p2d, p3d, camera_dict):
focal_length = pycolmap.Camera(camera_dict).mean_focal_length()
if query_path in self.cache:
calib = self.cache[query_path]
else:
calib = self.calib.calibrate(
load_image(query_path).to(self.device), priors={"f": focal_length}
)
calib = {k: v[0].detach().cpu().numpy() for k, v in calib.items()}
self.cache[query_path] = calib
# self.write_cache()
if self.opts["ransac"] == "pycolmap":
ret = pycolmap.absolute_pose_estimation(
p2d, p3d, camera_dict, self.opts["max_reproj_error"] # , do_refine=False
)
elif self.opts["ransac"] == "poselib":
M, ret = poselib.estimate_absolute_pose(
p2d,
p3d,
camera_dict,
ransac_opt={"max_reproj_error": self.opts["max_reproj_error"]},
)
ret["success"] = M is not None
ret["qvec"] = M.q
ret["tvec"] = M.t
elif self.opts["ransac"] == "poselib_gravity":
g_q = calib["gravity"].vec3d
g_qu = calib.get("gravity_uncertainty", self.opts["max_uncertainty"])
M, ret = poselib.estimate_absolute_pose_gravity(
p2d,
p3d,
camera_dict,
g_q,
g_qu * 2 * 180 / 3.1415, # convert to scalar
ransac_opt={"max_reproj_error": self.opts["max_reproj_error"]},
)
ret["success"] = M is not None
ret["qvec"] = M.q
ret["tvec"] = M.t
else:
raise NotImplementedError(self.opts["ransac"])
r_opts = {
"refine_focal_length": False,
"refine_extra_params": False,
"print_summary": False,
"loss_function_scale": self.opts["loss_function_scale"],
}
if self.opts["refinement"] == "pycolmap_gravity":
g_q = calib["gravity"].vec3d
g_qu = calib.get("gravity_uncertainty", self.opts["max_uncertainty"])
if g_qu <= self.opts["max_uncertainty"]:
g_gt = np.array([0, 0, 1]) # world frame
ret_ref = pycolmap.pose_refinement_gravity(
ret["tvec"],
ret["qvec"],
p2d,
p3d,
ret["inliers"],
camera_dict,
g_q,
g_gt,
self.opts["gravity_weight"],
r_opts,
)
else:
ret_ref = pycolmap.pose_refinement(
ret["tvec"],
ret["qvec"],
p2d,
p3d,
ret["inliers"],
camera_dict,
r_opts,
)
elif self.opts["refinement"] == "pycolmap":
ret_ref = pycolmap.pose_refinement(
ret["tvec"],
ret["qvec"],
p2d,
p3d,
ret["inliers"],
camera_dict,
r_opts,
)
elif self.opts["refinement"] == "none":
ret_ref = {}
else:
raise NotImplementedError(self.opts["refinement"])
ret = {**ret, **ret_ref}
ret["camera_dict"] = camera_dict
return ret, calib