Skip to content

Commit 78b414b

Browse files
committed
Implement set_first_to_second in modify-calibration.py
1 parent 56b82ad commit 78b414b

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

modify-calibration.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""
22
Modify Spectacular AI calibration JSON files
3+
4+
Input JSON in read from stdin an output written to stdout
35
"""
46
import numpy as np
57
import json
@@ -29,21 +31,47 @@ def set_imu_to_camera_matrix(calibration, imu_to_cam, second_camera=False):
2931

3032
return out
3133

34+
def set_first_to_second_matrix(calibration, first_to_second, second_camera=False):
35+
out = json.loads(json.dumps(calibration)) # deep copy
36+
assert(len(out['cameras']) == 2)
37+
38+
itoc1, itoc2 = [
39+
np.array(c['imuToCamera'])
40+
for c in calibration['cameras']
41+
]
42+
43+
if second_camera:
44+
second_to_first = np.linalg.inv(first_to_second)
45+
itoc1 = np.dot(second_to_first, itoc2)
46+
else:
47+
second_to_first = np.linalg.inv(first_to_second)
48+
itoc2 = np.dot(first_to_second, itoc1)
49+
50+
out['cameras'][0]['imuToCamera'] = itoc1.tolist()
51+
out['cameras'][1]['imuToCamera'] = itoc2.tolist()
52+
return out
53+
3254
if __name__ == '__main__':
3355
import argparse
3456
p = argparse.ArgumentParser(__doc__)
35-
subs = p.add_subparsers()
57+
p.add_argument('action', choices=['set_imu_to_camera', 'set_first_to_second'],
58+
help='set IMU to camera matrix or stereo extrinsic matrix, keeping the other intact')
3659

37-
set_imu_to_cam = subs.add_parser('set_imu_to_camera',
38-
help='set IMU to camera matrix, keeping stereo camera calibration intact')
39-
set_imu_to_cam.add_argument('imu_to_camera_matrix',
60+
p.add_argument('matrix',
4061
help='for example: [[1,0,0,0.1],[0,1,0,0.2],[0,0,1,0.3],[0,0,0,1]]')
41-
set_imu_to_cam.add_argument('--second', action='store_true',
42-
help='the given IMU-to-cam matrix is for the second camera')
4362

63+
p.add_argument('--second', action='store_true',
64+
help='the IMU-to-cam matrix (set or kept intact) concerns the second camera')
65+
4466
args = p.parse_args()
45-
if hasattr(args, 'imu_to_camera_matrix'):
46-
print(json.dumps(set_imu_to_camera_matrix(
47-
json.load(sys.stdin),
48-
json.loads(args.imu_to_camera_matrix),
49-
args.second), indent=2))
67+
68+
calib_in = json.load(sys.stdin)
69+
matrix = json.loads(args.matrix)
70+
71+
if args.action == 'set_imu_to_camera':
72+
calib_out = set_imu_to_camera_matrix(calib_in, matrix, args.second)
73+
74+
elif args.action == 'set_first_to_second':
75+
calib_out = set_first_to_second_matrix(calib_in, matrix, args.second)
76+
77+
print(json.dumps(calib_out, indent=2))

0 commit comments

Comments
 (0)