# TestNew.py import json import random from datasets import ( BuilderConfig, DatasetInfo, DownloadManager, GeneratorBasedBuilder, SplitGenerator, Split, Features, Image, Sequence, Value, ) from huggingface_hub import hf_hub_url _REPO_ID = "iamirulofficial/TestNew" # change if you ever fork the repo class ImageSubsetConfig(BuilderConfig): """BuilderConfig for full dataset vs. small random sample.""" def __init__(self, name, sample_size=None, **kwargs): super().__init__( name=name, version="1.0.1", description=kwargs.get("description", "") ) self.sample_size = sample_size class MyImageDataset(GeneratorBasedBuilder): """Images + 2‑D actuated_angle labels stored in metadata.json.""" BUILDER_CONFIGS = [ ImageSubsetConfig( name="full", sample_size=None, # all images description="Entire dataset (≈100 GB)" ), ImageSubsetConfig( name="small", sample_size=2, # tiny sample for quick tests description="Two random images" ), ] DEFAULT_CONFIG_NAME = "small" def _info(self) -> DatasetInfo: return DatasetInfo( description="Images with a 2‑D actuated_angle from metadata.json", features=Features( { "image": Image(), # PIL.Image will be returned "actuated_angle": Sequence(Value("int32")), # [angle0, angle1] } ), supervised_keys=None, ) # --------------------------------------------------------------------- # # Download phase # # --------------------------------------------------------------------- # def _split_generators(self, dl_manager: DownloadManager): # 1️⃣ Download metadata.json (tiny text file) meta_path = dl_manager.download( hf_hub_url(_REPO_ID, "metadata.json", repo_type="dataset") ) # 2️⃣ Decide which filenames we need with open(meta_path, encoding="utf-8") as f: metadata = json.load(f) # {"frame_000.png": {"0":…, …}, …} all_fnames = list(metadata) if self.config.sample_size: # small‑config branch random.seed(42) selected = sorted(random.sample(all_fnames, self.config.sample_size)) else: selected = sorted(all_fnames) # full dataset # 3️⃣ Build URLs → dl_manager.download() → local paths url_dict = { fname: hf_hub_url( _REPO_ID, f"images/{fname}", repo_type="dataset" ) for fname in selected } img_paths = dl_manager.download(url_dict) # same keys, but local files return [ SplitGenerator( name=Split.TRAIN, gen_kwargs={ "img_paths": img_paths, "metadata": metadata, }, ) ] # --------------------------------------------------------------------- # # Generate examples # # --------------------------------------------------------------------- # def _generate_examples(self, img_paths: dict, metadata: dict): """ Yields (key, example) where example = { "image": , "actuated_angle": [int, int] } """ for idx, (fname, local_path) in enumerate(img_paths.items()): meta = metadata.get(fname, {}) angles = [int(meta.get("0", 0)), int(meta.get("1", 0))] yield idx, {"image": local_path, "actuated_angle": angles}