XintongHe commited on
Commit
173fe72
1 Parent(s): 21becc9

Update Populus_Stomatal_Images_Datasets.py

Browse files
Files changed (1) hide show
  1. Populus_Stomatal_Images_Datasets.py +62 -17
Populus_Stomatal_Images_Datasets.py CHANGED
@@ -111,10 +111,12 @@ class NewDataset(datasets.GeneratorBasedBuilder):
111
 
112
  def _split_generators(self, dl_manager):
113
 
114
- data_files = dl_manager.download_and_extract({
115
  "csv": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.csv",
116
- "zip": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.zip"
 
117
  })
 
118
 
119
  species_info = pd.read_csv(data_files["csv"])
120
  extracted_images_path = os.path.join(data_files["zip"], "Labeled Stomatal Images")
@@ -128,7 +130,8 @@ class NewDataset(datasets.GeneratorBasedBuilder):
128
  gen_kwargs={
129
  "filepaths": all_image_filenames,
130
  "species_info": species_info,
131
- "data_dir": extracted_images_path
 
132
  },
133
  )]
134
 
@@ -157,42 +160,84 @@ class NewDataset(datasets.GeneratorBasedBuilder):
157
  })
158
  return annotations
159
 
160
- def _generate_examples(self, filepaths, species_info, data_dir):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  """Yields examples as (key, example) tuples."""
 
 
 
 
162
  for file_name in filepaths:
163
  image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
164
  image_path = os.path.join(data_dir, f"{image_id}.jpg")
165
- label_path = os.path.join(data_dir, f"{image_id}.txt")
166
-
167
  # Find the corresponding row in the CSV for the current image
168
  species_row = species_info.loc[species_info['FileName'] == image_id]
169
  if not species_row.empty:
170
  species = species_row['Species'].values[0]
171
  scientific_name = species_row['ScientificName'].values[0]
172
- width = species_row['Witdh'].values[0]
173
- height = species_row['Heigth'].values[0]
174
  else:
175
  # Default values if not found
176
  species = None
177
  scientific_name = None
178
  width = 1024 # Default value
179
  height = 768 # Default value
180
-
181
  pics_array = None
182
  with Image.open(image_path) as img:
183
- pics_array = np.array(img)# Convert the PIL image to a numpy array and then to a list
184
- # print(pics_array.shape)
185
-
186
- annotations = self._parse_yolo_labels(label_path, width, height)
187
-
188
  # Yield the dataset example
189
  yield image_id, {
190
  "image_id": image_id,
191
  "species": species,
192
  "scientific_name": scientific_name,
193
- "pics_array": pics_array, # Should be a list for JSON serializability
194
  "image_resolution": {"width": width, "height": height},
195
  "annotations": annotations
196
  }
197
-
198
-
 
111
 
112
  def _split_generators(self, dl_manager):
113
 
114
+ data_files = dl_manager.download_and_extract({
115
  "csv": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.csv",
116
+ "zip": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.zip",
117
+ "annotations_json": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/annotations.json"
118
  })
119
+
120
 
121
  species_info = pd.read_csv(data_files["csv"])
122
  extracted_images_path = os.path.join(data_files["zip"], "Labeled Stomatal Images")
 
130
  gen_kwargs={
131
  "filepaths": all_image_filenames,
132
  "species_info": species_info,
133
+ "data_dir": extracted_images_path
134
+ "annotations_file": data_files["annotations_json"]
135
  },
136
  )]
137
 
 
160
  })
161
  return annotations
162
 
163
+ # def _generate_examples(self, filepaths, species_info, data_dir):
164
+ # """Yields examples as (key, example) tuples."""
165
+ # for file_name in filepaths:
166
+ # image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
167
+ # image_path = os.path.join(data_dir, f"{image_id}.jpg")
168
+ # label_path = os.path.join(data_dir, f"{image_id}.txt")
169
+
170
+ # # Find the corresponding row in the CSV for the current image
171
+ # species_row = species_info.loc[species_info['FileName'] == image_id]
172
+ # if not species_row.empty:
173
+ # species = species_row['Species'].values[0]
174
+ # scientific_name = species_row['ScientificName'].values[0]
175
+ # width = species_row['Witdh'].values[0]
176
+ # height = species_row['Heigth'].values[0]
177
+ # else:
178
+ # # Default values if not found
179
+ # species = None
180
+ # scientific_name = None
181
+ # width = 1024 # Default value
182
+ # height = 768 # Default value
183
+
184
+ # pics_array = None
185
+ # with Image.open(image_path) as img:
186
+ # pics_array = np.array(img)# Convert the PIL image to a numpy array and then to a list
187
+ # # print(pics_array.shape)
188
+
189
+ # annotations = self._parse_yolo_labels(label_path, width, height)
190
+
191
+ # # Yield the dataset example
192
+ # yield image_id, {
193
+ # "image_id": image_id,
194
+ # "species": species,
195
+ # "scientific_name": scientific_name,
196
+ # "pics_array": pics_array, # Should be a list for JSON serializability
197
+ # "image_resolution": {"width": width, "height": height},
198
+ # "annotations": annotations
199
+ # }
200
+
201
+
202
+
203
+
204
+ def _generate_examples(self, filepaths, species_info, data_dir, annotations_file):
205
  """Yields examples as (key, example) tuples."""
206
+ # Load annotations from JSON file
207
+ with open(annotations_file, 'r') as file:
208
+ annotations_dict = json.load(file)
209
+
210
  for file_name in filepaths:
211
  image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
212
  image_path = os.path.join(data_dir, f"{image_id}.jpg")
213
+
 
214
  # Find the corresponding row in the CSV for the current image
215
  species_row = species_info.loc[species_info['FileName'] == image_id]
216
  if not species_row.empty:
217
  species = species_row['Species'].values[0]
218
  scientific_name = species_row['ScientificName'].values[0]
219
+ width = species_row['Width'].values[0] # Corrected field name from 'Witdh'
220
+ height = species_row['Height'].values[0] # Corrected field name from 'Heigth'
221
  else:
222
  # Default values if not found
223
  species = None
224
  scientific_name = None
225
  width = 1024 # Default value
226
  height = 768 # Default value
227
+
228
  pics_array = None
229
  with Image.open(image_path) as img:
230
+ pics_array = np.array(img) # Convert the PIL image to a numpy array
231
+
232
+ # Retrieve annotations for the current image from the dictionary
233
+ annotations = annotations_dict.get(image_id, [])
234
+
235
  # Yield the dataset example
236
  yield image_id, {
237
  "image_id": image_id,
238
  "species": species,
239
  "scientific_name": scientific_name,
240
+ "pics_array": pics_array.tolist(), # Convert numpy array to list for JSON serializability
241
  "image_resolution": {"width": width, "height": height},
242
  "annotations": annotations
243
  }