Update Populus_Stomatal_Images_Datasets.py
Browse files
Populus_Stomatal_Images_Datasets.py
CHANGED
@@ -131,71 +131,62 @@ class NewDataset(datasets.GeneratorBasedBuilder):
|
|
131 |
},
|
132 |
)]
|
133 |
|
134 |
-
|
135 |
-
def save_metadata_as_json(image_id, annotations, species, scientific_name, json_path):
|
136 |
-
metadata = {
|
137 |
-
"image_id": image_id,
|
138 |
-
"species": species,
|
139 |
-
"scientific_name": scientific_name,
|
140 |
-
"annotations": annotations
|
141 |
-
}
|
142 |
-
with open(json_path, 'w') as json_file:
|
143 |
-
json.dump(metadata, json_file)
|
144 |
-
|
145 |
-
def _parse_yolo_labels(self, label_path, width, height):
|
146 |
-
annotations = []
|
147 |
-
with open(label_path, 'r') as file:
|
148 |
-
yolo_data = file.readlines()
|
149 |
-
|
150 |
-
for line in yolo_data:
|
151 |
-
class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
|
152 |
-
x_min = (x_center_rel - width_rel / 2) * width
|
153 |
-
y_min = (y_center_rel - height_rel / 2) * height
|
154 |
-
x_max = (x_center_rel + width_rel / 2) * width
|
155 |
-
y_max = (y_center_rel + height_rel / 2) * height
|
156 |
-
annotations.append({
|
157 |
-
"category_id": int(class_id),
|
158 |
-
"bounding_box": {
|
159 |
-
"x_min": x_min,
|
160 |
-
"y_min": y_min,
|
161 |
-
"x_max": x_max,
|
162 |
-
"y_max": y_max
|
163 |
-
}
|
164 |
-
})
|
165 |
-
return annotations
|
166 |
-
|
167 |
-
def _generate_examples(self, filepaths, species_info, data_dir, split):
|
168 |
-
"""Yields examples as (key, example) tuples."""
|
169 |
-
for file_name in filepaths:
|
170 |
-
image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
|
171 |
-
image_path = os.path.join(data_dir, f"{image_id}.jpg")
|
172 |
-
label_path = os.path.join(data_dir, f"{image_id}.txt")
|
173 |
-
|
174 |
-
# Find the corresponding row in the CSV for the current image
|
175 |
-
species_row = species_info.loc[species_info['FileName'] == image_id]
|
176 |
-
if not species_row.empty:
|
177 |
-
species = species_row['Species'].values[0]
|
178 |
-
scientific_name = species_row['ScientificName'].values[0]
|
179 |
-
width = species_row['Width'].values[0]
|
180 |
-
height = species_row['Height'].values[0]
|
181 |
-
else:
|
182 |
-
# Default values if not found
|
183 |
-
species = None
|
184 |
-
scientific_name = None
|
185 |
-
width = 1024 # or some default value
|
186 |
-
height = 768 # or some default value
|
187 |
-
|
188 |
-
with Image.open(image_path) as img:
|
189 |
-
pics_array = np.array(img) # Convert the PIL image to a numpy array
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
},
|
132 |
)]
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
+
def _parse_yolo_labels(self, label_path, width, height):
|
136 |
+
annotations = []
|
137 |
+
with open(label_path, 'r') as file:
|
138 |
+
yolo_data = file.readlines()
|
139 |
+
|
140 |
+
for line in yolo_data:
|
141 |
+
class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
|
142 |
+
x_min = (x_center_rel - width_rel / 2) * width
|
143 |
+
y_min = (y_center_rel - height_rel / 2) * height
|
144 |
+
x_max = (x_center_rel + width_rel / 2) * width
|
145 |
+
y_max = (y_center_rel + height_rel / 2) * height
|
146 |
+
annotations.append({
|
147 |
+
"category_id": int(class_id),
|
148 |
+
"bounding_box": {
|
149 |
+
"x_min": x_min,
|
150 |
+
"y_min": y_min,
|
151 |
+
"x_max": x_max,
|
152 |
+
"y_max": y_max
|
153 |
+
}
|
154 |
+
})
|
155 |
+
return annotations
|
156 |
+
|
157 |
+
def _generate_examples(self, filepaths, species_info, data_dir, split):
|
158 |
+
"""Yields examples as (key, example) tuples."""
|
159 |
+
for file_name in filepaths:
|
160 |
+
image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
|
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 |
+
# Find the corresponding row in the CSV for the current image
|
165 |
+
species_row = species_info.loc[species_info['FileName'] == image_id]
|
166 |
+
if not species_row.empty:
|
167 |
+
species = species_row['Species'].values[0]
|
168 |
+
scientific_name = species_row['ScientificName'].values[0]
|
169 |
+
width = species_row['Width'].values[0]
|
170 |
+
height = species_row['Height'].values[0]
|
171 |
+
else:
|
172 |
+
# Default values if not found
|
173 |
+
species = None
|
174 |
+
scientific_name = None
|
175 |
+
width = 1024 # Default value
|
176 |
+
height = 768 # Default value
|
177 |
+
|
178 |
+
pics_array = None
|
179 |
+
with Image.open(image_path) as img:
|
180 |
+
pics_array = np.array(img).tolist() # Convert the PIL image to a numpy array and then to a list
|
181 |
+
|
182 |
+
annotations = self._parse_yolo_labels(label_path, width, height)
|
183 |
+
|
184 |
+
# Yield the dataset example
|
185 |
+
yield image_id, {
|
186 |
+
"image_id": image_id,
|
187 |
+
"species": species,
|
188 |
+
"scientific_name": scientific_name,
|
189 |
+
"pics_array": pics_array, # Should be a list for JSON serializability
|
190 |
+
"image_resolution": {"width": width, "height": height},
|
191 |
+
"annotations": annotations
|
192 |
+
}
|