XintongHe commited on
Commit
afb6bc0
1 Parent(s): 22b8b85

Update new_dataset_script.py

Browse files
Files changed (1) hide show
  1. new_dataset_script.py +48 -83
new_dataset_script.py CHANGED
@@ -106,95 +106,58 @@ class NewDataset(datasets.GeneratorBasedBuilder):
106
  citation=_CITATION,
107
  )
108
 
109
-
110
  def _split_generators(self, dl_manager):
111
- # Download and extract the dataset using Hugging Face's datasets library
112
- data_files = dl_manager.download_and_extract({
113
- "csv": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/Labeled Stomatal Images.csv",
114
- "zip": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/Labeled Stomatal Images.zip"
115
- })
116
-
117
- # Load the CSV file containing species and scientific names
118
- species_info = pd.read_csv(data_files["csv"])
119
-
120
- # The directory 'Labeled Stomatal Images' is where the images and labels are stored after extraction
121
- extracted_images_path = os.path.join(data_files["zip"], "Labeled Stomatal Images")
122
-
123
- # Get the list of image filenames from the CSV
124
- all_image_filenames = species_info['FileName'].apply(lambda x: x + '.jpg').tolist()
125
-
126
- # Shuffle the list for random split
127
- random.seed(42) # Set a random seed for reproducibility
128
- random.shuffle(all_image_filenames)
129
-
130
- # Split the files into train/validation/test
131
- num_files = len(all_image_filenames)
132
- train_split_end = int(num_files * 0.7)
133
- val_split_end = train_split_end + int(num_files * 0.15)
134
-
135
- train_files = all_image_filenames[:train_split_end]
136
- val_files = all_image_filenames[train_split_end:val_split_end]
137
- test_files = all_image_filenames[val_split_end:]
138
-
139
- return [
140
- datasets.SplitGenerator(
141
  name=datasets.Split.TRAIN,
142
  gen_kwargs={
143
- "filepaths": train_files,
144
- "species_info": species_info,
145
- "data_dir": extracted_images_path,
146
- "split": "train",
147
- },
148
- ),
149
- datasets.SplitGenerator(
150
- name=datasets.Split.VALIDATION,
151
- gen_kwargs={
152
- "filepaths": val_files,
153
  "species_info": species_info,
154
  "data_dir": extracted_images_path,
155
- "split": "validation",
156
  },
157
- ),
158
- datasets.SplitGenerator(
159
- name=datasets.Split.TEST,
160
- gen_kwargs={
161
- "filepaths": test_files,
162
- "species_info": species_info,
163
- "data_dir": extracted_images_path,
164
- "split": "test",
165
- },
166
- ),
167
- ]
168
 
169
-
170
 
171
  # ... other necessary imports and class definitions
172
  def _parse_yolo_labels(self, label_path, width, height):
173
- annotations = []
174
- with open(label_path, 'r') as file:
175
- yolo_data = file.readlines()
176
-
177
- for line in yolo_data:
178
- class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
179
- x_min = (x_center_rel - width_rel / 2) * width
180
- y_min = (y_center_rel - height_rel / 2) * height
181
- x_max = (x_center_rel + width_rel / 2) * width
182
- y_max = (y_center_rel + height_rel / 2) * height
183
- annotations.append({
184
- "category_id": int(class_id),
185
- "bounding_box": {
186
- "x_min": x_min,
187
- "y_min": y_min,
188
- "x_max": x_max,
189
- "y_max": y_max
190
- }
191
- })
192
- return annotations
193
-
194
- def _generate_examples(self, filepaths, species_info, data_dir, split):
195
- """Yields examples as (key, example) tuples."""
196
  for file_name in filepaths:
197
- image_id = os.path.splitext(file_name)[0] # Extract the base name without the file extension
198
  image_path = os.path.join(data_dir, f"{image_id}.jpg")
199
  label_path = os.path.join(data_dir, f"{image_id}.txt")
200
 
@@ -207,12 +170,14 @@ class NewDataset(datasets.GeneratorBasedBuilder):
207
  scientific_name = species_row['ScientificName'].values[0] if not species_row.empty else None
208
 
209
  annotations = self._parse_yolo_labels(label_path, width, height)
210
-
211
- yield image_id, {
 
212
  "image_id": image_id,
213
  "species": species,
214
  "scientific_name": scientific_name,
215
- "pics_array": pics_array,
216
- "image_resolution": {"width": width, "height": height},
217
  "annotations": annotations
218
- }
 
 
 
106
  citation=_CITATION,
107
  )
108
 
 
109
  def _split_generators(self, dl_manager):
110
+ # Only download data, no need to split
111
+ data_files = dl_manager.download_and_extract({
112
+ "csv": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/Labeled Stomatal Images.csv",
113
+ "zip": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/Labeled Stomatal Images.zip"
114
+ })
115
+
116
+ species_info = pd.read_csv(data_files["csv"])
117
+ extracted_images_path = os.path.join(data_files["zip"], "Labeled Stomatal Images")
118
+
119
+ # Get all image filenames
120
+ all_image_filenames = species_info['FileName'].apply(lambda x: x + '.jpg').tolist()
121
+
122
+ # No longer need to randomize and split the dataset
123
+ return [datasets.SplitGenerator(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  name=datasets.Split.TRAIN,
125
  gen_kwargs={
126
+ "filepaths": all_image_filenames,
 
 
 
 
 
 
 
 
 
127
  "species_info": species_info,
128
  "data_dir": extracted_images_path,
 
129
  },
130
+ )]
 
 
 
 
 
 
 
 
 
 
131
 
132
+
133
 
134
  # ... other necessary imports and class definitions
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):
158
+ """Yields examples as (key, example) tuples."""
159
  for file_name in filepaths:
160
+ image_id = os.path.splitext(file_name)[0]
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
 
 
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
+ yield image_id, img_with_metadata_and_annotations