RDD2020 / hugging_face.py
ShixuanAn's picture
Update hugging_face.py
3fcb2d5 verified
# TODO: Address all TODOs and remove all explanatory comments
"""TODO: Add a description here."""
import csv
import json
import os
from typing import List
import datasets
import logging
import xml.etree.ElementTree as ET
import os
# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {A great new dataset},
author={Shixuan An
},
year={2024}
}
"""
# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
"""
# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = ""
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""
# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
"""TODO: Short description of my dataset."""
VERSION = datasets.Version("1.1.0")
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features({
"image_id": datasets.Value("string"),
"country": datasets.Value("string"),
"type": datasets.Value("string"),
"image_resolution": datasets.Features({
"width": datasets.Value("int32"),
"height": datasets.Value("int32"),
"depth": datasets.Value("int32"),
}),
"image_path": datasets.Value("string"),
# "pics_array": datasets.Array3D(shape=(None, None, 3), dtype="uint8"),
"crack_type": datasets.Sequence(datasets.Value("string")),
"crack_coordinates": datasets.Sequence(datasets.Features({
"x_min": datasets.Value("int32"),
"x_max": datasets.Value("int32"),
"y_min": datasets.Value("int32"),
"y_max": datasets.Value("int32"),
})),
}),
homepage='https://data.mendeley.com/datasets/5ty2wb6gvg/1',
citation=_CITATION,
)
def _split_generators(self, dl_manager):
urls_to_download = {
"train": "https://huggingface.co/datasets/ShixuanAn/RDD2020/resolve/main/train.zip",
"test": "https://huggingface.co/datasets/ShixuanAn/RDD2020/resolve/main/test.zip",
"validation": "https://huggingface.co/datasets/ShixuanAn/RDD2020/resolve/main/validation.zip",
}
downloaded_files = {
name: dl_manager.download_and_extract(url)
for name, url in urls_to_download.items()
}
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath":os.path.join(downloaded_files["train"], "train"),
"split": "train",
}
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepath": os.path.join(downloaded_files["test"], "test"),
"split": "test",
}
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"filepath":os.path.join(downloaded_files["validation"], "validation"),
"split": "validation",
}
),
]
def _generate_examples(self, filepath, split):
# Iterate over each country directory
for country_dir in ['Czech', 'India', 'Japan']:
images_dir = f"{filepath}/{country_dir}/images"
annotations_dir = f"{filepath}/{country_dir}/annotations/xmls" if split == "train" else None
# Iterate over each image in the country's image directory
for image_file in os.listdir(images_dir):
if not image_file.endswith('.jpg'):
continue
image_id = f"{image_file.split('.')[0]}"
image_path = os.path.join(images_dir, image_file)
if annotations_dir:
annotation_file = image_id + '.xml'
annotation_path = os.path.join(annotations_dir, annotation_file)
if not os.path.exists(annotation_path):
continue
tree = ET.parse(annotation_path)
root = tree.getroot()
crack_type = []
crack_coordinates = []
for obj in root.findall('object'):
crack_type.append(obj.find('name').text)
bndbox = obj.find('bndbox')
coordinates = {
"x_min": int(bndbox.find('xmin').text),
"x_max": int(bndbox.find('xmax').text),
"y_min": int(bndbox.find('ymin').text),
"y_max": int(bndbox.find('ymax').text),
}
crack_coordinates.append(coordinates)
else:
crack_type = []
crack_coordinates = []
yield image_id, {
"image_id": image_id,
"country": country_dir,
"type": split,
"image_path": image_path,
"crack_type": crack_type,
"crack_coordinates": crack_coordinates,
}