Update new_dataset_script.py
Browse files- new_dataset_script.py +65 -82
new_dataset_script.py
CHANGED
|
@@ -79,40 +79,33 @@ class NewDataset(datasets.GeneratorBasedBuilder):
|
|
| 79 |
DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
| 80 |
|
| 81 |
def _info(self):
|
| 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 |
-
# Homepage of the dataset for documentation
|
| 110 |
-
homepage=_HOMEPAGE,
|
| 111 |
-
# License for the dataset if available
|
| 112 |
-
license=_LICENSE,
|
| 113 |
-
# Citation for the dataset
|
| 114 |
-
citation=_CITATION,
|
| 115 |
-
)
|
| 116 |
|
| 117 |
def _split_generators(self, dl_manager):
|
| 118 |
# Download and extract the dataset using Hugging Face's datasets library
|
|
@@ -173,64 +166,54 @@ class NewDataset(datasets.GeneratorBasedBuilder):
|
|
| 173 |
),
|
| 174 |
]
|
| 175 |
|
| 176 |
-
|
| 177 |
-
from PIL import Image
|
| 178 |
-
import numpy as np
|
| 179 |
-
import pandas as pd
|
| 180 |
|
| 181 |
# ... other necessary imports and class definitions
|
| 182 |
-
|
| 183 |
-
def _generate_examples(self, filepaths, species_info, data_dir, split):
|
| 184 |
-
"""Yields examples as (key, example) tuples."""
|
| 185 |
-
for file_name in filepaths:
|
| 186 |
-
# Extract the base name without the file extension for image_id
|
| 187 |
-
image_id = os.path.splitext(file_name)[0]
|
| 188 |
-
|
| 189 |
-
# Construct the full image file path and label file path
|
| 190 |
-
image_path = os.path.join(data_dir, f"{image_id}.jpg")
|
| 191 |
-
label_path = os.path.join(data_dir, f"{image_id}.txt")
|
| 192 |
-
|
| 193 |
-
# Open image and convert to array
|
| 194 |
-
with Image.open(image_path) as img:
|
| 195 |
-
pics_array = np.array(img)
|
| 196 |
-
width, height = img.size
|
| 197 |
-
|
| 198 |
-
# Extract species and scientific name from CSV file
|
| 199 |
-
species_row = species_info.loc[species_info['FileName'] == file_name]
|
| 200 |
-
species = species_row['Species'].values[0] if not species_row.empty else None
|
| 201 |
-
scientific_name = species_row['ScientificName'].values[0] if not species_row.empty else None
|
| 202 |
-
|
| 203 |
-
# Parse YOLO annotations
|
| 204 |
-
annotations = self._parse_yolo_labels(label_path, width, height)
|
| 205 |
-
|
| 206 |
-
# Yield the final structured example
|
| 207 |
-
yield image_id, {
|
| 208 |
-
"image_id": image_id,
|
| 209 |
-
"species": species,
|
| 210 |
-
"scientific_name": scientific_name,
|
| 211 |
-
"pics_array": pics_array,
|
| 212 |
-
"image_resolution": {"width": width, "height": height},
|
| 213 |
-
"annotations": annotations
|
| 214 |
-
}
|
| 215 |
-
|
| 216 |
-
def _parse_yolo_labels(self, label_path, width, height):
|
| 217 |
annotations = []
|
| 218 |
with open(label_path, 'r') as file:
|
| 219 |
yolo_data = file.readlines()
|
| 220 |
|
| 221 |
for line in yolo_data:
|
| 222 |
class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
annotations.append({
|
| 228 |
"category_id": int(class_id),
|
| 229 |
"bounding_box": {
|
| 230 |
-
"
|
| 231 |
-
"
|
| 232 |
-
"
|
| 233 |
-
"
|
| 234 |
}
|
| 235 |
})
|
| 236 |
-
return annotations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
| 80 |
|
| 81 |
def _info(self):
|
| 82 |
+
features = datasets.Features({
|
| 83 |
+
"image_id": datasets.Value("string"),
|
| 84 |
+
"species": datasets.Value("string"),
|
| 85 |
+
"scientific_name": datasets.Value("string"),
|
| 86 |
+
"pics_array": datasets.Array3D(dtype="uint8", shape=(3, 768, 1024)), # Assuming images are RGB with shape 768x1024
|
| 87 |
+
"image_resolution": {
|
| 88 |
+
"width": datasets.Value("int32"),
|
| 89 |
+
"height": datasets.Value("int32"),
|
| 90 |
+
},
|
| 91 |
+
"annotations": datasets.Sequence({
|
| 92 |
+
"category_id": datasets.Value("int32"),
|
| 93 |
+
"bounding_box": {
|
| 94 |
+
"x_min": datasets.Value("float32"),
|
| 95 |
+
"y_min": datasets.Value("float32"),
|
| 96 |
+
"x_max": datasets.Value("float32"),
|
| 97 |
+
"y_max": datasets.Value("float32"),
|
| 98 |
+
},
|
| 99 |
+
}),
|
| 100 |
+
})
|
| 101 |
+
return datasets.DatasetInfo(
|
| 102 |
+
description=_DESCRIPTION,
|
| 103 |
+
features=features, # Here we define them because they are different between the two configurations
|
| 104 |
+
homepage=_HOMEPAGE,
|
| 105 |
+
license=_LICENSE,
|
| 106 |
+
citation=_CITATION,
|
| 107 |
+
)
|
| 108 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
def _split_generators(self, dl_manager):
|
| 111 |
# Download and extract the dataset using Hugging Face's datasets library
|
|
|
|
| 166 |
),
|
| 167 |
]
|
| 168 |
|
| 169 |
+
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
# ... other necessary imports and class definitions
|
| 172 |
+
def _parse_yolo_labels(self, label_path, width, height):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
annotations = []
|
| 174 |
with open(label_path, 'r') as file:
|
| 175 |
yolo_data = file.readlines()
|
| 176 |
|
| 177 |
for line in yolo_data:
|
| 178 |
class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
|
| 179 |
+
x_min = (x_center_rel - width_rel / 2) * width
|
| 180 |
+
y_min = (y_center_rel - height_rel / 2) * height
|
| 181 |
+
x_max = (x_center_rel + width_rel / 2) * width
|
| 182 |
+
y_max = (y_center_rel + height_rel / 2) * height
|
| 183 |
annotations.append({
|
| 184 |
"category_id": int(class_id),
|
| 185 |
"bounding_box": {
|
| 186 |
+
"x_min": x_min,
|
| 187 |
+
"y_min": y_min,
|
| 188 |
+
"x_max": x_max,
|
| 189 |
+
"y_max": y_max
|
| 190 |
}
|
| 191 |
})
|
| 192 |
+
return annotations
|
| 193 |
+
|
| 194 |
+
def _generate_examples(self, filepaths, species_info, data_dir, split):
|
| 195 |
+
"""Yields examples as (key, example) tuples."""
|
| 196 |
+
for file_name in filepaths:
|
| 197 |
+
image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
|
| 198 |
+
image_path = os.path.join(data_dir, f"{image_id}.jpg")
|
| 199 |
+
label_path = os.path.join(data_dir, f"{image_id}.txt")
|
| 200 |
+
|
| 201 |
+
with Image.open(image_path) as img:
|
| 202 |
+
pics_array = np.array(img)
|
| 203 |
+
width, height = img.size
|
| 204 |
+
|
| 205 |
+
species_row = species_info.loc[species_info['FileName'] == file_name]
|
| 206 |
+
species = species_row['Species'].values[0] if not species_row.empty else None
|
| 207 |
+
scientific_name = species_row['ScientificName'].values[0] if not species_row.empty else None
|
| 208 |
+
|
| 209 |
+
annotations = self._parse_yolo_labels(label_path, width, height)
|
| 210 |
+
|
| 211 |
+
yield image_id, {
|
| 212 |
+
"image_id": image_id,
|
| 213 |
+
"species": species,
|
| 214 |
+
"scientific_name": scientific_name,
|
| 215 |
+
"pics_array": pics_array,
|
| 216 |
+
"image_resolution": {"width": width, "height": height},
|
| 217 |
+
"annotations": annotations
|
| 218 |
+
}
|
| 219 |
+
|