import datasets import json import os _CITATION = '' _DESCRIPTION = '' _URL = 'https://huggingface.co/datasets/akumoth/ygo_card_img/' _IMAGES_DIR = 'imgs/' class ImageSet(datasets.GeneratorBasedBuilder): IMAGE_EXTENSION = ".png" def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "id": datasets.Value("int32"), 'name': datasets.Value('string'), 'japanese_name': datasets.Value('string'), 'img': datasets.Image(), 'card_type': datasets.Value('string'), 'attribute': datasets.Value('string'), 'archetypes': datasets.Value('string'), } ), supervised_keys=None, homepage='https://huggingface.co/datasets/akumoth/ygo_card_img', citation=_CITATION, ) def _split_generators(self, dl_manager): downloaded_files = dl_manager.download({ 'card_json': 'card_df.jsonl', 'images': 'card_imgs.zip' }) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ 'card_json': downloaded_files['card_json'], 'image_iter': dl_manager.iter_archive(downloaded_files['images']), } ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={ 'card_json': downloaded_files['card_json'], 'image_iter': dl_manager.iter_archive(downloaded_files['images']), } ), ] def _generate_examples(self, card_json, image_iter): examples = [json.loads(example_json) for example_json in open(card_json).readlines()] for img_path, img_obj in image_iter: if img_path.startswith(_IMAGES_DIR): cid = int(''.join(filter(str.isdigit, img_path))) examples[cid]['img'] = img_obj.read() id_ = examples[cid]['id'] yield id_, examples[cid]