File size: 1,161 Bytes
05b8dea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""Monkeypatch lerobot's video decoder to read frames from the npy cache built by cache_videos.py.
Activated by importing this module (sitecustomize does it when ISR_MEMDECODE=1). Frame index = round(ts * fps)."""
import os, torch, numpy as np
_cache = {}; _fps = {}
def _load(path):
    p = str(path); a = _cache.get(p)
    if a is None:
        parts = p.split("/"); arm = parts[parts.index("datasets") + 1]; key, chunk, f = parts[-3], parts[-2], parts[-1][:-4]
        a = np.load(f"/workspace/isr/cache/{arm}/{key}/{chunk}_{f}.npy", mmap_mode="r"); _cache[p] = a
        import json; _fps[p] = json.load(open(f"/workspace/isr/datasets/{arm}/meta/info.json"))["fps"]
    return a
def decode_video_frames(video_path, timestamps, tolerance_s, backend=None, return_uint8=False, is_depth=False):
    a = _load(video_path); idx = np.clip(np.round(np.asarray(timestamps) * _fps[str(video_path)]).astype(int), 0, len(a) - 1)
    fr = torch.from_numpy(np.ascontiguousarray(a[idx])).permute(0, 3, 1, 2)   # [T,C,H,W] uint8
    return fr if return_uint8 else fr.float() / 255.0
import lerobot.datasets.dataset_reader as _r
_r.decode_video_frames = decode_video_frames