XintongHe commited on
Commit
e000a58
1 Parent(s): 80d0a83

Update Populus_Stomatal_Images_Datasets.py

Browse files
Files changed (1) hide show
  1. Populus_Stomatal_Images_Datasets.py +60 -56
Populus_Stomatal_Images_Datasets.py CHANGED
@@ -85,7 +85,8 @@ class NewDataset(datasets.GeneratorBasedBuilder):
85
  "image_id": datasets.Value("string"),
86
  "species": datasets.Value("string"),
87
  "scientific_name": datasets.Value("string"),
88
- "pics_array": datasets.Image(),
 
89
  # datasets.Array3D(dtype="uint8", shape=(3,768, 1024)), # Assuming images are RGB with shape 768x1024
90
  "image_resolution": {
91
  "width": datasets.Value("int32"),
@@ -160,78 +161,34 @@ class NewDataset(datasets.GeneratorBasedBuilder):
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['Witdh'].values[0] # Corrected field name from 'Witdh'
220
- height = species_row['Heigth'].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,
@@ -239,6 +196,53 @@ class NewDataset(datasets.GeneratorBasedBuilder):
239
  "scientific_name": scientific_name,
240
  #"pics_array": pics_array.tolist(), # Convert numpy array to list for JSON serializability
241
  "image_path": image_path,
 
242
  "image_resolution": {"width": width, "height": height},
243
  "annotations": annotations
244
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  "image_id": datasets.Value("string"),
86
  "species": datasets.Value("string"),
87
  "scientific_name": datasets.Value("string"),
88
+ "image_path": datasets.Value("string"),
89
+ "image": datasets.Image(),
90
  # datasets.Array3D(dtype="uint8", shape=(3,768, 1024)), # Assuming images are RGB with shape 768x1024
91
  "image_resolution": {
92
  "width": datasets.Value("int32"),
 
161
  })
162
  return annotations
163
 
164
+ def _generate_examples(self, filepaths, species_info, data_dir):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  """Yields examples as (key, example) tuples."""
 
 
 
 
166
  for file_name in filepaths:
167
  image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
168
  image_path = os.path.join(data_dir, f"{image_id}.jpg")
169
+ label_path = os.path.join(data_dir, f"{image_id}.txt")
170
+ img = Image.open(image_path)
171
  # Find the corresponding row in the CSV for the current image
172
  species_row = species_info.loc[species_info['FileName'] == image_id]
173
  if not species_row.empty:
174
  species = species_row['Species'].values[0]
175
  scientific_name = species_row['ScientificName'].values[0]
176
+ width = species_row['Witdh'].values[0]
177
+ height = species_row['Heigth'].values[0]
178
  else:
179
  # Default values if not found
180
  species = None
181
  scientific_name = None
182
  width = 1024 # Default value
183
  height = 768 # Default value
184
+
185
  # pics_array = None
186
  # with Image.open(image_path) as img:
187
+ # pics_array = np.array(img)# Convert the PIL image to a numpy array and then to a list
188
+ # # print(pics_array.shape)
189
+
190
+ annotations = self._parse_yolo_labels(label_path, width, height)
191
+
192
  # Yield the dataset example
193
  yield image_id, {
194
  "image_id": image_id,
 
196
  "scientific_name": scientific_name,
197
  #"pics_array": pics_array.tolist(), # Convert numpy array to list for JSON serializability
198
  "image_path": image_path,
199
+ "image": img,
200
  "image_resolution": {"width": width, "height": height},
201
  "annotations": annotations
202
  }
203
+
204
+
205
+
206
+
207
+ # def _generate_examples(self, filepaths, species_info, data_dir, annotations_file):
208
+ # """Yields examples as (key, example) tuples."""
209
+ # # Load annotations from JSON file
210
+ # with open(annotations_file, 'r') as file:
211
+ # annotations_dict = json.load(file)
212
+
213
+ # for file_name in filepaths:
214
+ # image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
215
+ # image_path = os.path.join(data_dir, f"{image_id}.jpg")
216
+
217
+ # # Find the corresponding row in the CSV for the current image
218
+ # species_row = species_info.loc[species_info['FileName'] == image_id]
219
+ # if not species_row.empty:
220
+ # species = species_row['Species'].values[0]
221
+ # scientific_name = species_row['ScientificName'].values[0]
222
+ # width = species_row['Witdh'].values[0] # Corrected field name from 'Witdh'
223
+ # height = species_row['Heigth'].values[0] # Corrected field name from 'Heigth'
224
+ # else:
225
+ # # Default values if not found
226
+ # species = None
227
+ # scientific_name = None
228
+ # width = 1024 # Default value
229
+ # height = 768 # Default value
230
+
231
+ # # pics_array = None
232
+ # # with Image.open(image_path) as img:
233
+ # # pics_array = np.array(img) # Convert the PIL image to a numpy array
234
+
235
+ # # Retrieve annotations for the current image from the dictionary
236
+ # annotations = annotations_dict.get(image_id, [])
237
+
238
+ # # Yield the dataset example
239
+ # yield image_id, {
240
+ # "image_id": image_id,
241
+ # "species": species,
242
+ # "scientific_name": scientific_name,
243
+ # #"pics_array": pics_array.tolist(), # Convert numpy array to list for JSON serializability
244
+ # "image_path": image_path,
245
+ # "image": img,
246
+ # "image_resolution": {"width": width, "height": height},
247
+ # "annotations": annotations
248
+ # }