greenbot-dataset / tools /timing.py
jlblancoc's picture
Calibration v2, figures, tools and dataset card
2a86f7e verified
Raw History Blame Contribute Delete
2.64 kB
"""Per-topic timing (bag time vs header stamp) and standstill detection, in one sequential pass.
Usage: timing.py <bag> <out.npz>
"""
import sys
from collections import defaultdict
from pathlib import Path
import cv2
import numpy as np
from rosbags.rosbag1 import Reader
BAG = Path(sys.argv[1])
TOPICS = ("/ouster/points", "/Velodyne1_SCAN", "/stereo_camera/left/image_raw", "/ouster/imu")
bt = defaultdict(list)
ht = defaultdict(list)
motion = []
prev = None
k_img = 0
with Reader(BAG) as r:
cons = [c for c in r.connections if c.topic in TOPICS]
for c, t, raw in r.messages(connections=cons):
# ROS 1 Header: uint32 seq, uint32 sec, uint32 nsec, ...
sec, nsec = np.frombuffer(raw[4:12], "<u4")
bt[c.topic].append(t * 1e-9)
ht[c.topic].append(sec + nsec * 1e-9)
if c.topic.endswith("image_raw"):
if k_img % 10 == 0:
# Image: header(seq,sec,nsec,frame_id), height, width, encoding, is_bigendian, step, data
n = int(np.frombuffer(raw[12:16], "<u4")[0])
o = 16 + n
h, w = np.frombuffer(raw[o:o + 8], "<u4")
o += 8
n = int(np.frombuffer(raw[o:o + 4], "<u4")[0])
o += 4 + n + 1 + 4 + 4
img = np.frombuffer(raw[o:o + h * w], np.uint8).reshape(h, w)
g = cv2.resize(img, (128, 96), interpolation=cv2.INTER_AREA).astype(np.float32)
motion.append((t * 1e-9, np.nan if prev is None else float(np.mean(np.abs(g - prev)))))
prev = g
k_img += 1
res = {}
for tp in TOPICS:
if not bt[tp]:
print(f"{tp:32s} not present")
continue
b = np.array(bt[tp])
h = np.array(ht[tp])
key = tp.strip("/").replace("/", "_")
res[key + "_bag"] = b
res[key + "_hdr"] = h
d = b - h
dh = np.diff(h)
print(f"{tp:32s} n={len(b)} bag-hdr: median {np.median(d):.4f} min {d.min():.4f} p99 {np.percentile(d, 99):.4f}; "
f"hdr period median {np.median(dh) * 1e3:.2f} ms, max gap {dh.max() * 1e3:.0f} ms, nonmonotonic {np.sum(dh <= 0)}")
w = ((b - b[0]) // 30).astype(int)
env = np.array([[np.mean(b[w == i]) - b[0], np.min(d[w == i])] for i in np.unique(w)])
p = np.polyfit(env[:, 0], env[:, 1], 1)
print(f" lower-envelope drift {p[0] * 1e6:+.1f} ppm, residual std {np.std(env[:, 1] - np.polyval(p, env[:, 0])) * 1e3:.2f} ms")
res["img_motion"] = np.array(motion).reshape(-1, 2)
np.savez(sys.argv[2], **res)
if len(motion) > 1:
print("image motion quantiles [1,5,10,50,90]%:", np.nanpercentile(res["img_motion"][:, 1], [1, 5, 10, 50, 90]).round(2))