forked from mapillary/OpenSfM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
265 lines (223 loc) · 8.17 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# pyre-unsafe
import argparse
import json
import typing as t
from collections import OrderedDict, defaultdict
from os import PathLike
from pathlib import Path
from typing import Union
import numpy as np
from annotation_gui_gcp.lib import GUI
from annotation_gui_gcp.lib.gcp_manager import GroundControlPointManager
from annotation_gui_gcp.lib.image_manager import ImageManager
from flask import Flask
from opensfm import dataset, io
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("dataset", help="dataset")
parser.add_argument(
"--group-by-reconstruction",
action="store_true",
help="If set, the UI will show one window per reconstruction, "
"otherwise, it will use sequences as specified by 'sequence-file'",
)
parser.add_argument(
"--strict-missing",
action="store_true",
)
parser.add_argument(
"--min-images-in-reconstruction",
type=int,
default=50,
help="Reconstructions with fewer images than this value will not be "
"displayed in the UI",
)
parser.add_argument(
"--sequence-file",
help="dict-of-image-keys JSON file describing each sequence. "
"Format: {'sequence_key': ['im_key_1', 'im_key_2', ...], ...}",
default="sequence_database.json",
)
parser.add_argument(
"--cad",
type=str,
help="Specify a directory containing CAD files in FBX format",
default=None,
)
parser.add_argument(
"--port",
type=int,
default=5000,
)
return parser
def file_sanity_check(root, seq_dict, fname) -> t.Set[str]:
# Images available under ./images for a sanity check
available_images = {p.name for p in (root / "images").iterdir()}
keys_in_seq_dict = {im_key for seq_keys in seq_dict.values() for im_key in seq_keys}
images_not_in_seq_file = available_images - keys_in_seq_dict
if len(images_not_in_seq_file) > 0:
print(f"{len(images_not_in_seq_file)} images not in {fname}")
n_missing = len(keys_in_seq_dict - available_images)
if n_missing > 0:
print(f"There are {n_missing} images from {fname} missing in {(root/'images')}")
return available_images
def load_rig_assignments(root: Path) -> t.Dict[str, t.List[str]]:
"""
Returns a dict mapping every shot to all the other corresponding shots in the rig
"""
p_json = root / "rig_assignments.json"
if not p_json.exists():
return {}
output = {}
with open(p_json) as f:
assignments: t.Dict[str, t.List[t.Tuple[str, str]]] = json.load(f)
for shot_group in assignments.values():
group_shot_ids = [s[0] for s in shot_group]
for shot_id, _ in shot_group:
output[shot_id] = group_shot_ids
return output
def load_sequence_database_from_file(
root: Path,
fname: Union["PathLike[str]", str] = "sequence_database.json",
skip_missing: bool = False,
):
"""
Simply loads a sequence file and returns it.
This doesn't require an existing SfM reconstruction
"""
root = Path(root)
p_json = root / fname
if not p_json.exists():
return None
with open(p_json) as f:
seq_dict = OrderedDict(io.json_load(f))
available_images = file_sanity_check(root, seq_dict, fname)
for skey in seq_dict:
available_image_keys = []
for k in seq_dict[skey]:
if k in available_images:
available_image_keys.append(k)
elif not skip_missing:
raise FileNotFoundError(f"{k} not found")
seq_dict[skey] = available_image_keys
empty_seqs = [skey for skey in seq_dict if not seq_dict[skey]]
for skey in empty_seqs:
del seq_dict[skey]
return seq_dict
def load_shots_from_reconstructions(path, min_ims) -> t.List[t.List[str]]:
data = dataset.DataSet(path)
reconstructions = data.load_reconstruction()
# Replace sequence keys for those in sequence_database.json
n_recs = len(reconstructions)
if len(reconstructions) > 2:
reconstructions = [
r
for ix_r, r in enumerate(reconstructions)
if len(r.shots) >= min_ims or ix_r < 2
]
if len(reconstructions) < n_recs:
print(
"Kept {}/{} reconstructions (min images: {})".format(
len(reconstructions),
n_recs,
min_ims,
)
)
output = []
for rec in reconstructions:
shots = sorted(
rec.shots.values(), key=lambda x: (x.metadata.capture_time.value, x.id)
)
output.append([shot.id for shot in shots])
return output
def group_by_reconstruction(
args, groups_from_sequence_database
) -> t.Dict[str, t.List[str]]:
all_recs_shots = load_shots_from_reconstructions(
args.dataset, min_ims=args.min_images_in_reconstruction
)
map_key_to_skey = {}
if groups_from_sequence_database:
for skey, keys in groups_from_sequence_database.items():
for k in keys:
map_key_to_skey[k] = skey
groups = defaultdict(list)
for ix_rec, rec_shots in enumerate(all_recs_shots):
for key in rec_shots:
if key in map_key_to_skey:
group_key = f"REC#{ix_rec}_{map_key_to_skey[key]}"
else:
group_key = f"REC#{ix_rec}"
groups[group_key].append(key)
return groups
def group_images(args) -> t.Dict[str, t.List[str]]:
"""
Groups the images to be shown in different windows/views
If group_by_reconstruction is set, each reconstruction will have its own view
If there is a sequence_database file, each sequence will have its own view
If group_by_reconstruction is set and there is a sequence_database file, the views
will be split by sequence and also by reconstruction. For example, if there is a camera
rig (4 sequences) that is reconstructed into two disjoint reconstructions, you end up
with 8 views.
"""
groups_from_sequence_database = load_sequence_database_from_file(
args.dataset,
args.sequence_file,
skip_missing=not args.strict_missing,
)
if args.group_by_reconstruction:
return group_by_reconstruction(args, groups_from_sequence_database)
else:
# We only group by sequence key
if groups_from_sequence_database is None:
print(
f"No sequence database file at {args.sequence_file}"
" and --group-by-reconstruction is disabled. Quitting"
)
exit()
return groups_from_sequence_database
def find_suitable_cad_paths(path_cad_files: Path, path_dataset, n_paths: int = 6):
if path_cad_files is None:
return []
def latlon_from_meta(path_cad) -> t.Tuple[float, float]:
path_meta = path_cad.with_suffix(".json")
with open(path_meta) as f:
meta = json.load(f)
return meta["center"]["latitude"], meta["center"]["longitude"]
# Returns the top N cad models sorted by distance to the dataset
path_cad_files = Path(path_cad_files)
cad_files = list(
set(
list(path_cad_files.glob("**/*.FBX"))
+ list(path_cad_files.glob("**/*.fbx"))
)
)
ref = dataset.DataSet(path_dataset).load_reference()
ref_latlon = np.array([ref.lat, ref.lon])
cad_latlons = np.array([latlon_from_meta(path) for path in cad_files])
distances = np.linalg.norm(cad_latlons - ref_latlon, axis=1)
ixs_sort = np.argsort(distances)[:n_paths]
return [cad_files[i] for i in ixs_sort]
def init_ui() -> t.Tuple[Flask, argparse.Namespace]:
app = Flask(__name__)
parser = get_parser()
args = parser.parse_args()
path = args.dataset
rig_groups = load_rig_assignments(Path(args.dataset))
groups = group_images(args)
image_manager = ImageManager(
groups,
path,
)
gcp_manager = GroundControlPointManager(path)
GUI.Gui(
app,
gcp_manager,
image_manager,
rig_groups,
find_suitable_cad_paths(args.cad, path, 1),
)
return app, args
if __name__ == "__main__":
app, args = init_ui()
app.run(host="::", port=args.port)