File size: 1,717 Bytes
15a4742 bf18a86 15a4742 29798e5 15a4742 29798e5 15a4742 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import os
import pandas as pd
import zipfile
import datasets
from datasets import load_dataset
class GanHKRLarge(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
features=datasets.Features(
{
"image": datasets.Image(),
"path": datasets.Value("string"),
"name": datasets.Value("string"),
"text": datasets.Value("string")
}
)
)
def _split_generators(self, dl_manager):
_URLS = {f"img{i:02d}": f"img/img-{i:02d}.zip" for i in range(10)}
_URLS["labels"] = "gt.txt"
data_paths = dl_manager.download_and_extract(_URLS)
image_paths = [data_paths[key] for key in data_paths if key.startswith("img")]
return [datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"image_paths": dl_manager.iter_files(image_paths),
"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,
"path": image_path,
"name": image_name,
"text": df["text"][image_name]
}
yield image_name, example
|