XintongHe commited on
Commit
4f297f5
1 Parent(s): c91a438

Update Populus_Stomatal_Images_Datasets.py

Browse files
Files changed (1) hide show
  1. Populus_Stomatal_Images_Datasets.py +28 -33
Populus_Stomatal_Images_Datasets.py CHANGED
@@ -109,38 +109,27 @@ class NewDataset(datasets.GeneratorBasedBuilder):
109
  )
110
 
111
  def _split_generators(self, dl_manager):
112
- # Download and extract the data files
113
  data_files = dl_manager.download_and_extract({
114
  "csv": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.csv",
115
- "zip_config": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images_config.zip"
116
  })
117
 
118
- # The path to the CSV file
119
- csv_path = data_files["csv"]
120
-
121
- # The path to the folder where the config ZIP was extracted
122
- extracted_config_path = data_files["zip_config"]
123
-
124
- # Read the CSV file to get the species information
125
- species_info = pd.read_csv(csv_path)
126
-
127
- # Get the list of image filenames from the CSV that are part of the config
128
- image_filenames_config = species_info['FileName'].apply(lambda x: x + '.jpg').tolist()
129
 
130
- # Filter the list to include only the images present in the config directory
131
- image_filenames = [f for f in image_filenames_config if os.path.exists(os.path.join(extracted_config_path, f))]
132
-
133
- # Use the filtered list of filenames to create the dataset split
134
- return [
135
- datasets.SplitGenerator(
136
- name=datasets.Split.TRAIN,
137
- gen_kwargs={
138
- "filepaths": image_filenames,
139
- "species_info": species_info,
140
- "data_dir": extracted_config_path
141
- },
142
- )
143
- ]
144
 
145
 
146
 
@@ -168,6 +157,7 @@ class NewDataset(datasets.GeneratorBasedBuilder):
168
  return annotations
169
 
170
  def _generate_examples(self, filepaths, species_info, data_dir):
 
171
  for file_name in filepaths:
172
  image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
173
  image_path = os.path.join(data_dir, f"{image_id}.jpg")
@@ -178,15 +168,19 @@ class NewDataset(datasets.GeneratorBasedBuilder):
178
  if not species_row.empty:
179
  species = species_row['Species'].values[0]
180
  scientific_name = species_row['ScientificName'].values[0]
181
- width = species_row['Width'].values[0]
182
- height = species_row['Height'].values[0]
183
  else:
184
- # If the species info is not found, continue to the next file
185
- continue
186
-
 
 
 
 
187
  with Image.open(image_path) as img:
188
  pics_array = np.array(img).tolist() # Convert the PIL image to a numpy array and then to a list
189
-
190
  annotations = self._parse_yolo_labels(label_path, width, height)
191
 
192
  # Yield the dataset example
@@ -199,3 +193,4 @@ class NewDataset(datasets.GeneratorBasedBuilder):
199
  "annotations": annotations
200
  }
201
 
 
 
109
  )
110
 
111
  def _split_generators(self, dl_manager):
112
+ # Only download data, no need to split
113
  data_files = dl_manager.download_and_extract({
114
  "csv": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.csv",
115
+ "zip": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.zip"
116
  })
117
 
118
+ species_info = pd.read_csv(data_files["csv"])
119
+ extracted_images_path = os.path.join(data_files["zip"], "Labeled Stomatal Images")
120
+
121
+ # Get all image filenames
122
+ all_image_filenames = species_info['FileName'].apply(lambda x: x + '.jpg').tolist()
 
 
 
 
 
 
123
 
124
+ # No longer need to randomize and split the dataset
125
+ return [datasets.SplitGenerator(
126
+ name=datasets.Split.TRAIN,
127
+ gen_kwargs={
128
+ "filepaths": all_image_filenames,
129
+ "species_info": species_info,
130
+ "data_dir": extracted_images_path
131
+ },
132
+ )]
 
 
 
 
 
133
 
134
 
135
 
 
157
  return annotations
158
 
159
  def _generate_examples(self, filepaths, species_info, data_dir):
160
+ """Yields examples as (key, example) tuples."""
161
  for file_name in filepaths:
162
  image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
163
  image_path = os.path.join(data_dir, f"{image_id}.jpg")
 
168
  if not species_row.empty:
169
  species = species_row['Species'].values[0]
170
  scientific_name = species_row['ScientificName'].values[0]
171
+ width = species_row['Witdh'].values[0]
172
+ height = species_row['Heigth'].values[0]
173
  else:
174
+ # Default values if not found
175
+ species = None
176
+ scientific_name = None
177
+ width = 1024 # Default value
178
+ height = 768 # Default value
179
+
180
+ pics_array = None
181
  with Image.open(image_path) as img:
182
  pics_array = np.array(img).tolist() # Convert the PIL image to a numpy array and then to a list
183
+
184
  annotations = self._parse_yolo_labels(label_path, width, height)
185
 
186
  # Yield the dataset example
 
193
  "annotations": annotations
194
  }
195
 
196
+