shrek-detection / shrek-detection.py
jamescalam's picture
Update shrek-detection.py
4f1d9ee verified
import datasets
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {Shrek Detection Dataset},
author={Aurelio AI},
year={2024}
}
"""
_DESCRIPTION = """\
Demo dataset for testing Shrek detection capabilities in images.
"""
_HOMEPAGE = "https://huggingface.co/datasets/aurelio-ai/shrek-detection"
_LICENSE = ""
_REPO = "https://huggingface.co/datasets/aurelio-ai/shrek-detection"
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(),
'is_shrek': datasets.Value("bool")
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
# iterator for train set
images_archive = dl_manager.download(f"{_REPO}/resolve/main/images.tgz")
image_iters = dl_manager.iter_archive(images_archive)
# iterator for test set
test_images_archive = dl_manager.download(f"{_REPO}/resolve/main/test_images.tgz")
test_image_iters = dl_manager.iter_archive(test_images_archive)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"images": image_iters
}
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"images": test_image_iters
}
),
]
def _generate_examples(self, images):
""" This function returns the examples in the raw (text) form."""
for idx, (filepath, image) in enumerate(images):
filename = filepath.split('/')[-1][:-4]
cls = filename[0] if filename[0] in ["0", "1"] else None
if cls:
filename = filename[1:]
description = filename.replace('_', ' ').replace('-', ' ')
yield idx, {
"image": {"path": filepath, "bytes": image.read()},
"text": description,
"is_shrek": True if cls == "1" else False
}