Debian Codegraph @

mrcal-show-residuals-board-observation

raw
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
4 # Government sponsorship acknowledged. All rights reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10
11 r'''Visualize calibration residuals for one or more observations of a board
12
13 SYNOPSIS
14
15 $ mrcal-show-residuals-board-observation
16 --from-worst --explore left.cameramodel 0-2
17
18 ... a plot pops up showing the 3 worst-fitting chessboard observations in this
19 ... solve. And a REPL opens up to allow further study
20
21 The residuals come from the optimization inputs stored in the cameramodel file.
22 A cameramodel that's missing this data cannot be used with this tool.
23
24 To plot the residuals on top of the image used in the calibration the image
25 paths are loaded from the optimization inputs. The paths are used directly,
26 relative to the current directory. If the paths are unavailable or if the image
27 cannot be read, the plot is made without the underlying image.
28
29 '''
30
31
32 import sys
33 import argparse
34 import re
35 import os
36
37 def parse_args():
38
39 parser = \
40 argparse.ArgumentParser(description = __doc__,
41 formatter_class=argparse.RawDescriptionHelpFormatter)
42
43 parser.add_argument('--title',
44 type=str,
45 default = None,
46 help='''Title string for the plot. Overrides the default
47 title. Exclusive with --extratitle''')
48 parser.add_argument('--extratitle',
49 type=str,
50 default = None,
51 help='''Additional string for the plot to append to the
52 default title. Exclusive with --title''')
53
54 parser.add_argument('--vectorscale',
55 type = float,
56 default = 1.0,
57 help='''Scale all the vectors by this factor. Useful to improve legibility if the
58 vectors are too small to see''')
59
60 parser.add_argument('--circlescale',
61 type = float,
62 default = 1.0,
63 help='''Scale all the plotted circles by this factor. Useful to improve legibility if
64 the vectors are too big or too small''')
65
66 parser.add_argument('--cbmax',
67 type=float,
68 help='''Maximum range of the colorbar. If omitted, we
69 autoscale''')
70
71 parser.add_argument('--from-worst',
72 action = 'store_true',
73 help='''If given, the requested observations index from
74 the worst-fitting observations to the best-fitting
75 (observation 0 is the worst-fitting observation).
76 Exclusive with --from-glob. By default we index the
77 observations in the order they appear in the solve''')
78
79 parser.add_argument('--from-glob',
80 action = 'store_true',
81 help='''If given, the requested observations are
82 specified as glob(s) matching the image filenames.
83 Exclusive with --from-worst''')
84
85 parser.add_argument('--image-path-prefix',
86 help='''If given, we prepend the given prefix to the image paths. Exclusive with
87 --image-directory''')
88
89 parser.add_argument('--image-directory',
90 help='''If given, we extract the filenames from the image paths in the solve, and use
91 the given directory to find those filenames. Exclusive
92 with --image-path-prefix''')
93
94 parser.add_argument('--hardcopy',
95 type=str,
96 help='''Write the output to disk, instead of making an
97 interactive plot. If given, only ONE observation may be
98 specified''')
99 parser.add_argument('--terminal',
100 type=str,
101 help=r'''gnuplotlib terminal. The default is good almost always, so most people don't
102 need this option''')
103 parser.add_argument('--set',
104 type=str,
105 action='append',
106 help='''Extra 'set' directives to gnuplotlib. Can be given multiple times''')
107 parser.add_argument('--unset',
108 type=str,
109 action='append',
110 help='''Extra 'unset' directives to gnuplotlib. Can be given multiple times''')
111 parser.add_argument('--explore',
112 action='store_true',
113 help='''If given, the tool drops into a REPL before exiting, to allow the user to
114 follow-up with more diagnostics''')
115
116 parser.add_argument('model',
117 type=str,
118 help=r'''Camera model that contains the optimization_inputs that describe the solve.
119 The displayed observations may come from ANY of the
120 cameras in the solve, not necessarily the one given by
121 this model''')
122
123 parser.add_argument('observations',
124 type=str,
125 nargs='+',
126 help=r'''The observations we're looking at. Unless
127 --from-glob, these are a list of integers A and/or A-B
128 ranges. By default these index the board observations in
129 the order they appear in the solve. If --from-worst,
130 then we index them from the worst-fitting to the
131 best-fitting instead. If --from-glob then we treat the
132 strings as globs to match against the image paths''')
133
134 args = parser.parse_args()
135
136 if args.image_path_prefix is not None and \
137 args.image_directory is not None:
138 print("--image-path-prefix and --image-directory are mutually exclusive",
139 file=sys.stderr)
140 sys.exit(1)
141
142 if args.from_worst and args.from_glob:
143 print("--from-worst and --from-glob are mutually exclusive",
144 file=sys.stderr)
145 sys.exit(1)
146
147 def list_ints_from_range_string(s):
148 try:
149 i = int(s)
150 if i < 0:
151 print(f"Observations should be a list of non-negative integers and/or A-B ranges. Invalid observation given: '{s}'",
152 file=sys.stderr)
153 sys.exit(1)
154 return [i]
155 except Exception as e:
156 pass
157
158 m = re.match("^([0-9]+)-([0-9]+)$", s)
159 if m is None:
160 print(f"Observations should be a list of non-negative integers and/or A-B ranges. Invalid observation given: '{s}'",
161 file=sys.stderr)
162 sys.exit(1)
163 try:
164 i0 = int(m.group(1))
165 i1 = int(m.group(2))
166 except Exception as e:
167 print(f"Observations should be a list of non-negative integers and/or A-B ranges. Invalid observation given: '{s}'",
168 file=sys.stderr)
169 sys.exit(1)
170 return list(range(i0,i1+1))
171
172 if not args.from_glob:
173 args.observations = [o for obs in args.observations for o in list_ints_from_range_string(obs)]
174
175 if args.hardcopy is not None:
176 if len(args.observations) != 1:
177 print(f"--hardcopy given, so exactly one observation should have been given. Instead got these observations: {args.observations}",
178 file=sys.stderr)
179 sys.exit(1)
180
181 if args.title is not None and \
182 args.extratitle is not None:
183 print("--title and --extratitle are exclusive", file=sys.stderr)
184 sys.exit(1)
185
186 return args
187
188 args = parse_args()
189
190 # arg-parsing is done before the imports so that --help works without building
191 # stuff, so that I can generate the manpages and README
192
193
194 import mrcal
195 import numpy as np
196 import numpysane as nps
197 import pprint
198
199
200 plotkwargs_extra = {}
201 if args.set is not None:
202 plotkwargs_extra['set'] = args.set
203 if args.unset is not None:
204 plotkwargs_extra['unset'] = args.unset
205
206 if args.title is not None:
207 plotkwargs_extra['title'] = args.title
208 if args.extratitle is not None:
209 plotkwargs_extra['extratitle'] = args.extratitle
210
211 try:
212 model = mrcal.cameramodel(args.model)
213 except Exception as e:
214 print(f"Couldn't load camera model '{args.model}': {e}", file=sys.stderr)
215 sys.exit(1)
216
217 optimization_inputs = model.optimization_inputs()
218 if optimization_inputs is None:
219 print(f"Camera model '{args.model}' does not contain optimization inputs. Residuals aren't available", file=sys.stderr)
220 sys.exit(1)
221
222 try:
223 paths = optimization_inputs['imagepaths']
224 except:
225 # older models don't have this data
226 paths = None
227
228
229 if args.from_glob:
230 if paths is None:
231 print("--from-glob requires image paths, but the given model doesn't have them",
232 file=sys.stderr)
233 sys.exit(1)
234
235 import fnmatch
236
237 def matching_path_indices(obs):
238 try:
239 r = re.compile( fnmatch.translate(obs) )
240 except:
241 print(f"Error translating, compiling glob '{obs}'",
242 file=sys.stderr)
243 sys.exit(1)
244
245 m = [i for i in range(len(paths)) if r.match(paths[i])]
246 if not m:
247 print(f"Glob '{obs}' did not match any images",
248 file=sys.stderr)
249 sys.exit(1)
250 return m
251
252 args.observations = [i for obs in args.observations for i in matching_path_indices(obs)]
253
254
255
256 x = mrcal.optimizer_callback(**optimization_inputs)[1]
257
258 # for --explore
259 indices_frame_camintrinsics_camextrinsics = optimization_inputs.get('indices_frame_camintrinsics_camextrinsics')
260 indices_point_camintrinsics_camextrinsics = optimization_inputs.get('indices_point_camintrinsics_camextrinsics')
261
262 if indices_frame_camintrinsics_camextrinsics is None:
263 print("ERROR: today this tool is primarily for chessboard solves", file=sys.stderr)
264 sys.exit(1)
265 if indices_point_camintrinsics_camextrinsics is not None:
266 print("WARNING: points present in this solve this tool looks at the chessboard observations only", file=sys.stderr)
267
268
269
270 # i_observations_sorted_from_worst is available even if I don't need it.
271 # --explore might want to look at it
272
273
274 # shape (Nobservations, object_height_n, object_width_n, 3)
275 observations = optimization_inputs['observations_board']
276 x_shape = observations.shape[:-1] + (2,)
277 # shape (Nobservations, object_height_n, object_width_n, 2)
278 x_reshaped = x[:np.prod(x_shape)].reshape(*x_shape)
279 # shape (Nobservations,)
280 err_per_observation = nps.norm2(nps.clump(x_reshaped, n=-3))
281 i_observations_sorted_from_worst = \
282 list(reversed(np.argsort(err_per_observation)))
283
284
285
286
287
288 def show(i_observation):
289 try:
290 mrcal.show_residuals_board_observation(optimization_inputs,
291 i_observation,
292 from_worst = args.from_worst,
293 i_observations_sorted_from_worst = i_observations_sorted_from_worst,
294 x = x,
295 paths = paths,
296 cbmax = args.cbmax,
297 image_path_prefix = args.image_path_prefix,
298 image_directory = args.image_directory,
299 circlescale = args.circlescale,
300 vectorscale = args.vectorscale,
301 hardcopy = args.hardcopy,
302 terminal = args.terminal,
303 **plotkwargs_extra)
304 except Exception as e:
305 print(f"Couldn't show_residuals_board_observation(): {e}",
306 file=sys.stderr)
307 sys.exit(1)
308
309
310 Nplots = len(args.observations)
311
312 if args.hardcopy:
313 for i in range(Nplots):
314 show(args.observations[i])
315 print(f"Wrote {args.hardcopy}")
316 pids = []
317
318 else:
319 plotkwargs_extra['wait'] = True
320 pids = [0] * Nplots
321 for i in range(Nplots):
322 pid = os.fork()
323 if pid == 0:
324 # child
325 # make the plot, and wait for it to be closed by the user
326 show(args.observations[i])
327 sys.exit()
328
329 # parent
330 pids[i] = pid
331
332
333 if args.explore:
334
335 explore_message = \
336 f'''We're exploring. The first plot being shown can be re-created with:
337
338 mrcal.show_residuals_board_observation(optimization_inputs,
339 {args.observations[0]},
340 from_worst = args.from_worst,
341 i_observations_sorted_from_worst = i_observations_sorted_from_worst,
342 x = x,
343 paths = paths,
344 cbmax = args.cbmax,
345 image_path_prefix = args.image_path_prefix,
346 image_directory = args.image_directory,
347 circlescale = args.circlescale,
348 vectorscale = args.vectorscale,
349 hardcopy = args.hardcopy,
350 terminal = args.terminal,
351 **{pprint.pformat(plotkwargs_extra)})
352
353 This is available via a shorthand function call show({args.observations[0]})
354
355
356 The first 10 worst-fitting observations (i_observations_sorted_from_worst[:10])
357
358 {i_observations_sorted_from_worst[:10]}
359
360
361 The corresponding 10 (iframe, icamintrinsics, icamextrinsics) tuples
362 (indices_frame_camintrinsics_camextrinsics[i_observations_sorted_from_worst[:10] ]):
363
364 {indices_frame_camintrinsics_camextrinsics[ i_observations_sorted_from_worst[:10] ]}
365
366 '''
367 if paths is not None:
368 explore_message += \
369 f'''The corresponding image paths (paths[i_observations_sorted_from_worst[:10]]):
370
371 {paths[i_observations_sorted_from_worst[:10]]}
372 '''
373
374 explore_message += \
375 r"""
376 The optimization inputs are available in the optimization_inputs dict
377 """
378
379 print(explore_message)
380 import IPython
381 IPython.embed()
382
383
384 for pid in pids:
385 os.waitpid(pid, 0)
386 sys.exit()