import os import pandas as pd import zipfile import datasets from datasets import load_dataset class SyntheticCyrillic(datasets.GeneratorBasedBuilder): def _info(self): return datasets.DatasetInfo( features=datasets.Features( { "image": datasets.Image(), "text": datasets.Value("string"), "path": datasets.Value("string"), "name": datasets.Value("string") } ) ) def _split_generators(self, dl_manager): _URLS = {"img": "img.zip", "labels": "gt.txt"} data_paths = dl_manager.download_and_extract(_URLS) return [datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "image_paths": dl_manager.iter_files(data_paths["img"]), "labels_path": data_paths["labels"] })] def _generate_examples(self, image_paths, labels_path): df = pd.read_csv(labels_path, sep=",", names=["path", "text"]) df["path"] = df.path.str[4:] df.set_index("path", inplace=True) for image_path in image_paths: image_name = os.path.basename(image_path) if image_name in df.index: example = { "image": image_path, "text": df["text"][image_name], "path": image_path, "name": image_name } yield image_name, example