scripts: update RTM iterator with touch-window timestamp picker
Browse files- scripts/make_parquet_v2.py +36 -6
scripts/make_parquet_v2.py
CHANGED
|
@@ -189,8 +189,12 @@ def iter_tactile_tracking():
|
|
| 189 |
|
| 190 |
def iter_real_tactile_mnist():
|
| 191 |
"""RTM seq-320x240: parquet with 'sensor_video' list-of-struct{bytes,path}.
|
| 192 |
-
Each row
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
"""
|
| 195 |
import cv2
|
| 196 |
root = f"{BASE_DATA}/markerless/RealTactileMNIST"
|
|
@@ -206,6 +210,9 @@ def iter_real_tactile_mnist():
|
|
| 206 |
label = cols.get("label", [None]*n)[i]
|
| 207 |
obj_id = cols.get("object_id", [None]*n)[i]
|
| 208 |
videos = cols["sensor_video"][i] or []
|
|
|
|
|
|
|
|
|
|
| 209 |
for tj, vid_struct in enumerate(videos):
|
| 210 |
if not vid_struct: continue
|
| 211 |
vid_bytes = vid_struct.get("bytes") if isinstance(vid_struct, dict) else None
|
|
@@ -222,9 +229,32 @@ def iter_real_tactile_mnist():
|
|
| 222 |
try: os.remove(tmpf)
|
| 223 |
except: pass
|
| 224 |
if not frames: continue
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
"source": "real_tactile_mnist",
|
| 229 |
"markered": False,
|
| 230 |
"capture": f"r{round_id}_t{tj}",
|
|
@@ -232,7 +262,7 @@ def iter_real_tactile_mnist():
|
|
| 232 |
"obj_name": f"digit_{label}",
|
| 233 |
"digit_class": int(label) if label is not None else None,
|
| 234 |
"episode": str(obj_id) if obj_id is not None else None,
|
| 235 |
-
"frame_idx":
|
| 236 |
}
|
| 237 |
|
| 238 |
|
|
|
|
| 189 |
|
| 190 |
def iter_real_tactile_mnist():
|
| 191 |
"""RTM seq-320x240: parquet with 'sensor_video' list-of-struct{bytes,path}.
|
| 192 |
+
Each row is one 'round' = one digit touched ~256 times. Each touch video
|
| 193 |
+
is 60-75 frames long but the actual contact window is only ~6 frames
|
| 194 |
+
(typically near the end of the video). We pick the frame at the *middle
|
| 195 |
+
of the touch window* using `touch_start_time_rel` / `touch_end_time_rel`
|
| 196 |
+
plus the per-frame `time_stamp_rel_seq`. As a fallback when timestamps
|
| 197 |
+
are missing or stale, we pick the frame with highest central std.
|
| 198 |
"""
|
| 199 |
import cv2
|
| 200 |
root = f"{BASE_DATA}/markerless/RealTactileMNIST"
|
|
|
|
| 210 |
label = cols.get("label", [None]*n)[i]
|
| 211 |
obj_id = cols.get("object_id", [None]*n)[i]
|
| 212 |
videos = cols["sensor_video"][i] or []
|
| 213 |
+
ts_seq = cols.get("time_stamp_rel_seq", [None]*n)[i] or []
|
| 214 |
+
t_start = cols.get("touch_start_time_rel", [None]*n)[i] or []
|
| 215 |
+
t_end = cols.get("touch_end_time_rel", [None]*n)[i] or []
|
| 216 |
for tj, vid_struct in enumerate(videos):
|
| 217 |
if not vid_struct: continue
|
| 218 |
vid_bytes = vid_struct.get("bytes") if isinstance(vid_struct, dict) else None
|
|
|
|
| 229 |
try: os.remove(tmpf)
|
| 230 |
except: pass
|
| 231 |
if not frames: continue
|
| 232 |
+
|
| 233 |
+
pick_idx = None
|
| 234 |
+
# Try timestamp-based pick first
|
| 235 |
+
try:
|
| 236 |
+
ts = ts_seq[tj] if tj < len(ts_seq) else None
|
| 237 |
+
ts0 = t_start[tj] if tj < len(t_start) else None
|
| 238 |
+
ts1 = t_end[tj] if tj < len(t_end) else None
|
| 239 |
+
if ts is not None and ts0 is not None and ts1 is not None \
|
| 240 |
+
and len(ts) == len(frames):
|
| 241 |
+
t_mid = (ts0 + (ts1 - ts0) / 2)
|
| 242 |
+
diffs = [abs((t - t_mid).total_seconds()) for t in ts]
|
| 243 |
+
pick_idx = int(np.argmin(diffs))
|
| 244 |
+
except Exception:
|
| 245 |
+
pick_idx = None
|
| 246 |
+
|
| 247 |
+
# Fallback: frame with max central-region std
|
| 248 |
+
if pick_idx is None:
|
| 249 |
+
stds = []
|
| 250 |
+
for fr in frames:
|
| 251 |
+
g = np.asarray(Image.fromarray(fr).convert("L"),
|
| 252 |
+
dtype=np.float32)
|
| 253 |
+
h, w = g.shape
|
| 254 |
+
stds.append(float(g[h//4:3*h//4, w//4:3*w//4].std()))
|
| 255 |
+
pick_idx = int(np.argmax(stds))
|
| 256 |
+
|
| 257 |
+
yield frames[pick_idx], {
|
| 258 |
"source": "real_tactile_mnist",
|
| 259 |
"markered": False,
|
| 260 |
"capture": f"r{round_id}_t{tj}",
|
|
|
|
| 262 |
"obj_name": f"digit_{label}",
|
| 263 |
"digit_class": int(label) if label is not None else None,
|
| 264 |
"episode": str(obj_id) if obj_id is not None else None,
|
| 265 |
+
"frame_idx": pick_idx,
|
| 266 |
}
|
| 267 |
|
| 268 |
|