stackmix_hkr_large / stackmix_hkr.py
nastyboget's picture
fix loading file
8fc2d4a
raw
history blame
1.98 kB
import os
import pandas as pd
import zipfile
import datasets
from datasets import load_dataset
class StackmixHKR(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(_URLS)
archive_paths = [data_paths[key] for key in data_paths if key.startswith("img")]
img_path = os.path.split(data_paths["img00"])[0]
for archive_path in archive_paths:
with zipfile.ZipFile(archive_path) as archive:
archive.extractall(img_path)
image_paths = [os.path.join(img_path, img_name) for img_name in os.listdir(img_path)]
return [datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"image_paths": 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