|
1 | 1 | """ |
2 | 2 | Modify Spectacular AI calibration JSON files |
| 3 | +
|
| 4 | +Input JSON in read from stdin an output written to stdout |
3 | 5 | """ |
4 | 6 | import numpy as np |
5 | 7 | import json |
@@ -29,21 +31,47 @@ def set_imu_to_camera_matrix(calibration, imu_to_cam, second_camera=False): |
29 | 31 |
|
30 | 32 | return out |
31 | 33 |
|
| 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 | + |
32 | 54 | if __name__ == '__main__': |
33 | 55 | import argparse |
34 | 56 | 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') |
36 | 59 |
|
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', |
40 | 61 | 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') |
43 | 62 |
|
| 63 | + p.add_argument('--second', action='store_true', |
| 64 | + help='the IMU-to-cam matrix (set or kept intact) concerns the second camera') |
| 65 | + |
44 | 66 | 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