Update new_dataset_script.py
Browse files- new_dataset_script.py +58 -52
new_dataset_script.py
CHANGED
@@ -18,7 +18,8 @@
|
|
18 |
import csv
|
19 |
import json
|
20 |
import os
|
21 |
-
|
|
|
22 |
import datasets
|
23 |
|
24 |
|
@@ -129,55 +130,60 @@ class NewDataset(datasets.GeneratorBasedBuilder):
|
|
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 |
-
image_path = os.path.join(data_dir, f"{image_id}.jpg")
|
162 |
-
label_path = os.path.join(data_dir, f"{image_id}.txt")
|
163 |
-
|
164 |
-
with Image.open(image_path) as img:
|
165 |
-
pics_array = np.array(img)
|
166 |
-
width, height = img.size
|
167 |
-
|
168 |
-
species_row = species_info.loc[species_info['FileName'] == file_name]
|
169 |
-
species = species_row['Species'].values[0] if not species_row.empty else None
|
170 |
-
scientific_name = species_row['ScientificName'].values[0] if not species_row.empty else None
|
171 |
-
|
172 |
-
annotations = self._parse_yolo_labels(label_path, width, height)
|
173 |
-
|
174 |
-
# Create a structured object that includes the image and its metadata
|
175 |
-
img_with_metadata_and_annotations = {
|
176 |
-
"image_id": image_id,
|
177 |
-
"species": species,
|
178 |
-
"scientific_name": scientific_name,
|
179 |
-
"image": img, # Assuming you want to keep the PIL Image object; otherwise use pics_array
|
180 |
-
"annotations": annotations
|
181 |
}
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
import csv
|
19 |
import json
|
20 |
import os
|
21 |
+
from PIL import Image
|
22 |
+
import numpy as np
|
23 |
import datasets
|
24 |
|
25 |
|
|
|
130 |
},
|
131 |
)]
|
132 |
|
133 |
+
|
134 |
+
def save_metadata_as_json(image_id, annotations, species, scientific_name, json_path):
|
135 |
+
metadata = {
|
136 |
+
"image_id": image_id,
|
137 |
+
"species": species,
|
138 |
+
"scientific_name": scientific_name,
|
139 |
+
"annotations": annotations
|
140 |
+
}
|
141 |
+
with open(json_path, 'w') as json_file:
|
142 |
+
json.dump(metadata, json_file)
|
143 |
+
|
144 |
+
def _parse_yolo_labels(self, label_path, width, height):
|
145 |
+
annotations = []
|
146 |
+
with open(label_path, 'r') as file:
|
147 |
+
yolo_data = file.readlines()
|
148 |
+
|
149 |
+
for line in yolo_data:
|
150 |
+
class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
|
151 |
+
x_min = (x_center_rel - width_rel / 2) * width
|
152 |
+
y_min = (y_center_rel - height_rel / 2) * height
|
153 |
+
x_max = (x_center_rel + width_rel / 2) * width
|
154 |
+
y_max = (y_center_rel + height_rel / 2) * height
|
155 |
+
annotations.append({
|
156 |
+
"category_id": int(class_id),
|
157 |
+
"bounding_box": {
|
158 |
+
"x_min": x_min,
|
159 |
+
"y_min": y_min,
|
160 |
+
"x_max": x_max,
|
161 |
+
"y_max": y_max
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
}
|
163 |
+
})
|
164 |
+
return annotations
|
165 |
+
|
166 |
+
def _generate_examples(self, filepaths, species_info, data_dir, split):
|
167 |
+
"""Yields examples as (key, example) tuples."""
|
168 |
+
for file_name in filepaths:
|
169 |
+
image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
|
170 |
+
image_path = os.path.join(data_dir, f"{image_id}.jpg")
|
171 |
+
label_path = os.path.join(data_dir, f"{image_id}.txt")
|
172 |
+
json_path = os.path.join(data_dir, f"{image_id}.json") # JSON file path
|
173 |
+
|
174 |
+
with Image.open(image_path) as img:
|
175 |
+
width, height = img.size
|
176 |
+
|
177 |
+
species_row = species_info.loc[species_info['FileName'] == file_name]
|
178 |
+
species = species_row['Species'].values[0] if not species_row.empty else None
|
179 |
+
scientific_name = species_row['ScientificName'].values[0] if not species_row.empty else None
|
180 |
+
|
181 |
+
annotations = self._parse_yolo_labels(label_path, width, height)
|
182 |
+
|
183 |
+
# Save metadata to JSON
|
184 |
+
save_metadata_as_json(image_id, annotations, species, scientific_name, json_path)
|
185 |
+
|
186 |
+
yield image_id, {
|
187 |
+
"image": img, # Return the PIL image
|
188 |
+
"metadata_json": json_path # Return the path to the JSON file
|
189 |
+
}
|