|
import datasets |
|
import json |
|
import os |
|
|
|
_CITATION = """\ |
|
@InProceedings{huggingface:dataset, |
|
title = {Small image-text set}, |
|
author={James Briggs}, |
|
year={2022} |
|
} |
|
""" |
|
|
|
_DESCRIPTION = """\ |
|
Demo dataset for testing or showing image-text capabilities. |
|
""" |
|
_HOMEPAGE = "https://huggingface.co/datasets/ppp121386/Image-demo" |
|
|
|
_LICENSE = "" |
|
|
|
_REPO_URL = "https://huggingface.co/datasets/ppp121386/Image-demo/resolve/main/images.tar.gz" |
|
_CAPTION_URL = "https://huggingface.co/datasets/ppp121386/Image-demo/resolve/main/caption.json" |
|
|
|
|
|
|
|
class ImageSet(datasets.GeneratorBasedBuilder): |
|
"""Small sample of image-text pairs""" |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
'text': datasets.Value("string"), |
|
'image': datasets.Image(), |
|
} |
|
), |
|
supervised_keys=None, |
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
images_archive = dl_manager.download(_REPO_URL) |
|
image_iters = dl_manager.iter_archive(images_archive) |
|
filepath = dl_manager.download_and_extract(_CAPTION_URL) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"images": image_iters, |
|
"filepath": filepath |
|
} |
|
), |
|
] |
|
|
|
def _generate_examples(self, images, filepath): |
|
""" This function returns the examples in the raw (text) form.""" |
|
_CAPTION = json.load(open(filepath, 'r')) |
|
for idx, (imgpath, image) in enumerate(images): |
|
|
|
|
|
yield idx, { |
|
"image": {"path": imgpath, "bytes": image.read()}, |
|
"text": _CAPTION[idx], |
|
} |