Debian Codegraph @

mrcal-from-cahvor

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
12 r'''Converts a .cahvor-formatted camera model to the .cameramodel file format
13
14 SYNOPSIS
15
16 $ mrcal-from-cahvor model1.cahvor model2.cahvor
17 Wrote model1.cameramodel
18 Wrote model2.cameramodel
19
20 File formats supported by mrcal are described at
21 https://mrcal.secretsauce.net/cameramodels.html#cameramodel-file-formats
22
23 This tool converts the given model(s) to the cameramodel file format. No changes
24 to the content are made; this is purely a format converter (the
25 mrcal-convert-lensmodel tool fits different lens models instead). Model
26 filenames are given on the commandline. Output is written to the same directory,
27 with the same filename, but with a .cameramodel extension.
28
29 If the model is omitted or given as "-", the input is read from standard input,
30 and the output is written to standard output
31
32 '''
33
34 import sys
35 import argparse
36 import re
37 import os
38
39 def parse_args():
40
41 parser = \
42 argparse.ArgumentParser(description = __doc__,
43 formatter_class=argparse.RawDescriptionHelpFormatter)
44
45 parser.add_argument('--force', '-f',
46 action='store_true',
47 default=False,
48 help='''By default existing files are not overwritten. Pass --force to overwrite them
49 without complaint''')
50 parser.add_argument('--outdir',
51 required=False,
52 type=lambda d: d if os.path.isdir(d) else \
53 parser.error("--outdir requires an existing directory as the arg, but got '{}'".format(d)),
54 help='''Directory to write the output models into. If omitted, we write the output
55 models to the same directory as the input models''')
56 parser.add_argument('model',
57 default=['-'],
58 nargs='*',
59 type=str,
60 help='''Input camera model''')
61
62 return parser.parse_args()
63
64 args = parse_args()
65
66 # arg-parsing is done before the imports so that --help works without building
67 # stuff, so that I can generate the manpages and README
68
69 Nstdin = sum(1 for m in args.model if m=='-')
70 if Nstdin > 1:
71 print(f"At most one model can be read from standard input ('-'), but I got {Nstdin}", file=sys.stderr)
72 sys.exit(1)
73
74 import mrcal
75
76 for model in args.model:
77 if model == '-':
78 try:
79 m = mrcal.cameramodel(model)
80 except KeyboardInterrupt:
81 sys.exit(1)
82 except Exception as e:
83 print(e, file=sys.stderr)
84 sys.exit(1)
85 m.write(sys.stdout)
86 else:
87 base,extension = os.path.splitext(model)
88 if extension.lower() == '.cameramodel':
89 print("Input file is already in the cameramodel format (judging from the filename). Doing nothing",
90 file=sys.stderr)
91 sys.exit(0)
92
93 if args.outdir is not None:
94 base = args.outdir + '/' + os.path.split(base)[1]
95 filename_out = base + '.cameramodel'
96 if not args.force and os.path.exists(filename_out):
97 print(f"Target model '{filename_out}' already exists. Doing nothing with this model. Pass -f to overwrite",
98 file=sys.stderr)
99 else:
100 m = mrcal.cameramodel(model)
101 m.write(filename_out)
102 print("Wrote " + filename_out)