greenbot-dataset / tools /extract.py
jlblancoc's picture
Calibration v2, figures, tools and dataset card
2a86f7e verified
Raw History Blame Contribute Delete
2.67 kB
"""Extract calibration samples from a GREENBOT v1 bag into a pickle cache.
Usage: extract.py <bag> <timing.npz> <out.pkl> [n_moving]
"""
import pickle
import sys
from pathlib import Path
import numpy as np
from rosbags.highlevel import AnyReader
sys.path.insert(0, str(Path(__file__).parent))
from common import TOP_LEFT, TOP_OUSTER, TOP_RIGHT, TOP_VELO, bayer_to_gray, cloud_xyz, stamp # noqa: E402
BAG = Path(sys.argv[1])
tim = np.load(sys.argv[2])
OUT = Path(sys.argv[3])
N_MOVE = int(sys.argv[4]) if len(sys.argv) > 4 else 50
# Sample times (bag clock): evenly spaced + middle of every standstill episode.
mo = tim["img_motion"]
t_all = mo[:, 0]
d = mo[:, 1]
still = np.isfinite(d) & (d < 1.0)
episodes = []
for i in np.flatnonzero(still):
if not episodes or t_all[i] - episodes[-1][1] > 2:
episodes.append([t_all[i], t_all[i]])
else:
episodes[-1][1] = t_all[i]
t_still = [0.5 * (a + b) for a, b in episodes]
t_move = np.linspace(t_all[0] + 15, t_all[-1] - 15, N_MOVE)
times = [(t, True) for t in t_still] + [(t, False) for t in t_move]
print(f"{BAG.name}: {len(t_still)} standstill episodes, {len(t_move)} moving samples")
samples = []
with AnyReader([BAG]) as r:
con = {tp: [c for c in r.connections if c.topic == tp] for tp in (TOP_LEFT, TOP_RIGHT, TOP_OUSTER, TOP_VELO)}
def window(tp, T, a, b):
return [(t * 1e-9, r.deserialize(raw, c.msgtype)) for c, t, raw in
r.messages(connections=con[tp], start=int((T + a) * 1e9), stop=int((T + b) * 1e9))]
for T, is_still in times:
Ls = window(TOP_LEFT, T, -0.03, 0.03)
if not Ls:
continue
tbL, Lm = Ls[0]
Rs = [m for _, m in window(TOP_RIGHT, tbL, -0.1, 0.1) if abs(stamp(m) - stamp(Lm)) < 2e-3]
if not Rs:
continue
Os = window(TOP_OUSTER, T, -0.25, 0.35)
Vs = window(TOP_VELO, T, -0.25, 0.35)
if len(Os) < 3 or len(Vs) < 3:
continue
s = dict(still=is_still, t_bag=tbL, cam_hdr=stamp(Lm), L=bayer_to_gray(Lm), R=bayer_to_gray(Rs[0]), ouster=[], velo=[])
for tb, m in Os:
P, tt, amb, ring = cloud_xyz(m, extra=("t", "ambient", "ring"))
s["ouster"].append(dict(t_bag=tb, hdr=stamp(m), P=P.astype(np.float32), t=tt.astype(np.uint32),
amb=amb.astype(np.uint16), ring=ring.astype(np.uint16)))
for tb, m in Vs:
(P,) = cloud_xyz(m)
s["velo"].append(dict(t_bag=tb, hdr=stamp(m), P=P.astype(np.float32)))
samples.append(s)
print(f"{len(samples)} samples extracted")
pickle.dump(dict(bag=BAG.stem, samples=samples), open(OUT, "wb"), protocol=4)