|
| 1 | +""" |
| 2 | +Modify Spectacular AI data JSONL files |
| 3 | +
|
| 4 | +Input JSONL in read from stdin an output written to stdout |
| 5 | +""" |
| 6 | +import json |
| 7 | +import sys |
| 8 | + |
| 9 | +def modify_jsonl_sorted(in_f, out_f, func): |
| 10 | + lines = [] |
| 11 | + for line in in_f: |
| 12 | + d = func(json.loads(line)) |
| 13 | + if d is not None: |
| 14 | + lines.append(d) |
| 15 | + for l in sorted(lines, key=lambda d: d.get('time', -1e100)): |
| 16 | + out_f.write(json.dumps(l)+'\n') |
| 17 | + |
| 18 | +def modify_jsonl_stream(in_f, out_f, func): |
| 19 | + for line in in_f: |
| 20 | + d = func(json.loads(line)) |
| 21 | + if d is not None: |
| 22 | + out_f.write(json.dumps(d)+'\n') |
| 23 | + return out_f |
| 24 | + |
| 25 | +def shift_imu_time(data, delta_t): |
| 26 | + if 'sensor' in data: |
| 27 | + data['time'] += delta_t |
| 28 | + return data |
| 29 | + |
| 30 | +def shift_frame_times(data, delta_t): |
| 31 | + if 'frames' in data: |
| 32 | + data['time'] += delta_t |
| 33 | + for cam in data['frames']: |
| 34 | + if 'time' in cam: cam['time'] += delta_t |
| 35 | + return data |
| 36 | + |
| 37 | +def drop_zero_imu_samples(data): |
| 38 | + if 'sensor' in data and all([v == 0.0 for v in data['sensor']['values']]): return None |
| 39 | + return data |
| 40 | + |
| 41 | +if __name__ == '__main__': |
| 42 | + import argparse |
| 43 | + p = argparse.ArgumentParser(__doc__) |
| 44 | + subs = p.add_subparsers(dest='command') |
| 45 | + |
| 46 | + sub_shift_imu = subs.add_parser('shift_imu_time', help='shift IMU timestamps by given delta_t') |
| 47 | + sub_shift_imu.add_argument('delta_t', help='timestamp shift in seconds', type=float) |
| 48 | + |
| 49 | + sub_shift_frames = subs.add_parser('shift_frame_times', help='shift frame timestamps by given delta_t') |
| 50 | + sub_shift_frames.add_argument('delta_t', help='timestamp shift in seconds', type=float) |
| 51 | + |
| 52 | + sub_drop_zero_imus = subs.add_parser('drop_zero_imu_samples', help='remove all-zero IMU samples') |
| 53 | + |
| 54 | + p.add_argument('-s', '--sort', action='store_true', help='sort the resulting file based on timestamp') |
| 55 | + |
| 56 | + args = p.parse_args() |
| 57 | + f_in = sys.stdin |
| 58 | + f_out = sys.stdout |
| 59 | + |
| 60 | + if args.command == 'shift_imu_time': |
| 61 | + func = lambda x: shift_imu_time(x, args.delta_t) |
| 62 | + elif args.command == 'shift_frame_times': |
| 63 | + func = lambda x: shift_frame_times(x, args.delta_t) |
| 64 | + elif args.command == 'drop_zero_imu_samples': |
| 65 | + func = drop_zero_imu_samples |
| 66 | + else: |
| 67 | + assert(False) |
| 68 | + |
| 69 | + if args.sort: |
| 70 | + modify_jsonl_sorted(f_in, f_out, func) |
| 71 | + else: |
| 72 | + modify_jsonl_stream(f_in, f_out, func) |
0 commit comments