| |
| |
| |
| |
| |
| |
| |
| |
| ''' |
| Dataset class for loading and processing grasp data from a WebDataset. |
| |
| Installation: |
| pip install trimesh==4.5.3 objaverse==0.1.7 meshcat==0.0.12 webdataset==0.2.111 |
| |
| ''' |
| import os |
| import json |
| import time |
| import webdataset as wds |
| import glob |
| from pathlib import Path |
| from tqdm import tqdm |
| from typing import Dict, Optional |
|
|
| def load_uuid_list(uuid_list_path: str): |
| if not os.path.exists(uuid_list_path): |
| raise FileNotFoundError(f"UUID list file not found: {uuid_list_path}") |
| if uuid_list_path.endswith(".json"): |
| with open(uuid_list_path, 'r') as f: |
| uuids = json.load(f) |
| if type(uuids) == list: |
| return uuids |
| elif type(uuids) == dict: |
| return list(uuids.keys()) |
| else: |
| raise ValueError(f"UUID list is not a list or dict: {uuids}") |
| elif uuid_list_path.endswith(".txt"): |
| with open(uuid_list_path, 'r') as f: |
| uuids = [line.strip() for line in f.readlines()] |
| else: |
| raise ValueError(f"Unsupported file format: {uuid_list_path}") |
| return uuids |
|
|
| class GraspWebDatasetReader: |
| """Class to efficiently read grasps data using a pre-loaded index.""" |
| |
| def __init__(self, dataset_path: str): |
| """ |
| Initialize the reader with dataset path and load the index. |
| |
| Args: |
| dataset_path (str): Path to directory containing WebDataset shards |
| """ |
| self.dataset_path = dataset_path |
| self.shards_dir = self.dataset_path |
| |
| |
| index_path = os.path.join(self.shards_dir, "uuid_index.json") |
| with open(index_path, 'r') as f: |
| self.uuid_index = json.load(f) |
| |
| |
| self.shard_datasets = {} |
|
|
| def read_grasps_by_uuid(self, object_uuid: str) -> Optional[Dict]: |
| """ |
| Read grasps data for a specific object UUID using the index. |
| |
| Args: |
| object_uuid (str): UUID of the object to retrieve |
| |
| Returns: |
| Optional[Dict]: Dictionary containing the grasps data if found, None otherwise |
| """ |
| if object_uuid not in self.uuid_index: |
| return None |
| |
| shard_idx = self.uuid_index[object_uuid] |
| |
| |
| if shard_idx not in self.shard_datasets: |
| shard_path = f"{self.shards_dir}/shard_{shard_idx:03d}.tar" |
| self.shard_datasets[shard_idx] = wds.WebDataset(shard_path) |
| |
| dataset = self.shard_datasets[shard_idx] |
| |
| |
| for sample in dataset: |
| if sample["__key__"] == object_uuid: |
| return json.loads(sample["grasps.json"]) |
| |
| return None |