|
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 |
|
|