stackmix_hkr_large / stackmix_hkr_large.py
nastyboget's picture
Rename stackmix_hkr.py to stackmix_hkr_large.py
de3dfe5
import os
import pandas as pd
import zipfile
import datasets
from datasets import load_dataset
class StackmixHKRLarge(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
features=datasets.Features(
{
"path": datasets.Value("string"),
"name": datasets.Value("string"),
"text": datasets.Value("string"),
"image": datasets.Image()
}
)
)
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 = {
"path": image_path,
"name": image_name,
"text": df["text"][image_name],
"image": image_path
}
yield image_name, example