|
"""Dataset class for image dataset.""" |
|
|
|
import datasets |
|
import os |
|
from datasets.tasks import ImageClassification |
|
|
|
_URL = "http://https://huggingface.co/datasets/Racso777/AncientMortar/tree/main/metadata" |
|
|
|
_HOMEPAGE = "http://https://huggingface.co/datasets/Racso777/AncientMortar" |
|
|
|
_DESCRIPTION = ( |
|
"This dataset consists of test dataset of ancient mortar and with only obsidian images as zip file in it" |
|
) |
|
|
|
_NAMES = [ |
|
"Obsidian-1to2mm", |
|
|
|
] |
|
|
|
|
|
class AncientMortarConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for COCO cats image.""" |
|
|
|
def __init__( |
|
self, |
|
data_url, |
|
url, |
|
task_templates=None, |
|
**kwargs, |
|
): |
|
super(AncientMortarConfig, self).__init__( |
|
version=datasets.Version("1.9.0", ""), **kwargs |
|
) |
|
self.data_url = data_url |
|
self.url = url |
|
self.task_templates = task_templates |
|
|
|
class AncientMortar(datasets.GeneratorBasedBuilder): |
|
|
|
BUILDER_CONFIGS = [ |
|
AncientMortarConfig( |
|
name="image", |
|
url="", |
|
data_url="", |
|
) |
|
] |
|
|
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"image": datasets.Image(), |
|
"label": datasets.ClassLabel(), |
|
} |
|
), |
|
supervised_keys=("image", "label"), |
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
task_templates=[ImageClassification(image_column="image", label_column="label")], |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
|
|
|
|
archive_path = dl_manager.download_and_extract(_URLS) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={"archive_path": archive_path}, |
|
), |
|
] |
|
def _generate_examples(self, images, metadata_path): |
|
"""Generate images and labels for splits.""" |
|
with open(metadata_path, encoding="utf-8") as f: |
|
files_to_keep = set(f.read().split("\n")) |
|
for file_path, file_obj in images: |
|
if file_path.startswith(_IMAGES_DIR): |
|
if file_path[len(_IMAGES_DIR) : -len(".bmp")] in files_to_keep: |
|
label = file_path.split("/")[2] |
|
yield file_path, { |
|
"image": {"path": file_path, "bytes": file_obj.read()}, |
|
"label": label, |
|
} |