File size: 7,257 Bytes
d92f616 4953151 d92f616 4953151 d92f616 b066243 d92f616 c7a7861 8d102e2 c7a7861 8d3515f 19ebb8d 8d3515f 4927cbc c7a7861 8d3515f f8dd6b6 0a107d3 7320725 cede009 5805eb9 7320725 5805eb9 80c3f37 231bbd7 63f6c86 231bbd7 c7a7861 231bbd7 8d3515f 231bbd7 63f6c86 231bbd7 c7a7861 231bbd7 8d3515f 231bbd7 5fd226e 231bbd7 c7a7861 231bbd7 b066243 3f895a0 c7a7861 bc5162b c7a7861 3756ac8 2640785 5f31bbe 7e85ea9 c7a7861 8d3515f c7a7861 8d3515f 3f895a0 c7a7861 de8e778 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# 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
from PIL import Image
# 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": datasets.Value("string"),
"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/RDD_2020/resolve/main/train.zip',
"test1": "https://huggingface.co/datasets/ShixuanAn/RDD_2020/resolve/main/test1.zip",
"test2": "https://huggingface.co/datasets/ShixuanAn/RDD_2020/resolve/main/test2.zip"
}
downloaded_files = {
name: dl_manager.download_and_extract(url)
for name, url in urls_to_download.items()
}
# print( downloaded_files['train'])
# ls /root/.cache/huggingface/datasets/downloads/extracted/da17428e8597064d3819cfa1c73686f8876572f681a1b577a2fe8a68c885806a
#ls /root/.cache/huggingface/datasets/downloads/extracted/8391428f7cc3295c41551812c745861c0fa9ffe588cccef4cd4f618f92e6333f
# ls /root/.cache/huggingface/datasets/downloads/extracted/037a5dd551ee4ecb710b7ea376f03a5a3031aa9555aff200c7b758b066b1981d
# directory_path = '/root/.cache/huggingface/datasets/downloads/extracted/da17428e8597064d3819cfa1c73686f8876572f681a1b577a2fe8a68c885806a'
# files_and_directories = os.listdir(directory_path)
# print(files_and_directories)
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["test1"], "test1"),
"split": "test1",
}
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"filepath":os.path.join(downloaded_files["test2"], "test2"),
"split": "test2",
}
),
]
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"
# print(os.listdir(filepath))
# print(os.listdir(filepath))
# print(os.listdir(f"{filepath}/{country_dir}"))
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)
# Load the image as a PIL Image object
with Image.open(image_path) as img:
img.load() # This is important to load the image data
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 = []
# 将 PIL 图像对象添加到生成的字典中
yield image_id, {
"image_id": image_id,
"country": country_dir,
"type": split,
"image": img, # PIL 图像对象
"crack_type": crack_type,
"crack_coordinates": crack_coordinates,
} |