XintongHe commited on
Commit
d3c5791
1 Parent(s): 0d03b37

Update new_dataset_script.py

Browse files
Files changed (1) hide show
  1. new_dataset_script.py +65 -82
new_dataset_script.py CHANGED
@@ -79,40 +79,33 @@ class NewDataset(datasets.GeneratorBasedBuilder):
79
  DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
80
 
81
  def _info(self):
82
- # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
83
- if self.config.name == "first_domain": # This is the name of the configuration selected in BUILDER_CONFIGS above
84
- features = datasets.Features(
85
- {
86
- "sentence": datasets.Value("string"),
87
- "option1": datasets.Value("string"),
88
- "answer": datasets.Value("string")
89
- # These are the features of your dataset like images, labels ...
90
- }
91
- )
92
- else: # This is an example to show how to have different features for "first_domain" and "second_domain"
93
- features = datasets.Features(
94
- {
95
- "sentence": datasets.Value("string"),
96
- "option2": datasets.Value("string"),
97
- "second_domain_answer": datasets.Value("string")
98
- # These are the features of your dataset like images, labels ...
99
- }
100
- )
101
- return datasets.DatasetInfo(
102
- # This is the description that will appear on the datasets page.
103
- description=_DESCRIPTION,
104
- # This defines the different columns of the dataset and their types
105
- features=features, # Here we define them above because they are different between the two configurations
106
- # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
107
- # specify them. They'll be used if as_supervised=True in builder.as_dataset.
108
- # supervised_keys=("sentence", "label"),
109
- # Homepage of the dataset for documentation
110
- homepage=_HOMEPAGE,
111
- # License for the dataset if available
112
- license=_LICENSE,
113
- # Citation for the dataset
114
- citation=_CITATION,
115
- )
116
 
117
  def _split_generators(self, dl_manager):
118
  # Download and extract the dataset using Hugging Face's datasets library
@@ -173,64 +166,54 @@ class NewDataset(datasets.GeneratorBasedBuilder):
173
  ),
174
  ]
175
 
176
- import os
177
- from PIL import Image
178
- import numpy as np
179
- import pandas as pd
180
 
181
  # ... other necessary imports and class definitions
182
-
183
- def _generate_examples(self, filepaths, species_info, data_dir, split):
184
- """Yields examples as (key, example) tuples."""
185
- for file_name in filepaths:
186
- # Extract the base name without the file extension for image_id
187
- image_id = os.path.splitext(file_name)[0]
188
-
189
- # Construct the full image file path and label file path
190
- image_path = os.path.join(data_dir, f"{image_id}.jpg")
191
- label_path = os.path.join(data_dir, f"{image_id}.txt")
192
-
193
- # Open image and convert to array
194
- with Image.open(image_path) as img:
195
- pics_array = np.array(img)
196
- width, height = img.size
197
-
198
- # Extract species and scientific name from CSV file
199
- species_row = species_info.loc[species_info['FileName'] == file_name]
200
- species = species_row['Species'].values[0] if not species_row.empty else None
201
- scientific_name = species_row['ScientificName'].values[0] if not species_row.empty else None
202
-
203
- # Parse YOLO annotations
204
- annotations = self._parse_yolo_labels(label_path, width, height)
205
-
206
- # Yield the final structured example
207
- yield image_id, {
208
- "image_id": image_id,
209
- "species": species,
210
- "scientific_name": scientific_name,
211
- "pics_array": pics_array,
212
- "image_resolution": {"width": width, "height": height},
213
- "annotations": annotations
214
- }
215
-
216
- def _parse_yolo_labels(self, label_path, width, height):
217
  annotations = []
218
  with open(label_path, 'r') as file:
219
  yolo_data = file.readlines()
220
 
221
  for line in yolo_data:
222
  class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
223
- x_center_abs = x_center_rel * width
224
- y_center_abs = y_center_rel * height
225
- bbox_width = width_rel * width
226
- bbox_height = height_rel * height
227
  annotations.append({
228
  "category_id": int(class_id),
229
  "bounding_box": {
230
- "x_center": x_center_abs,
231
- "y_center": y_center_abs,
232
- "width": bbox_width,
233
- "height": bbox_height
234
  }
235
  })
236
- return annotations
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
80
 
81
  def _info(self):
82
+ features = datasets.Features({
83
+ "image_id": datasets.Value("string"),
84
+ "species": datasets.Value("string"),
85
+ "scientific_name": datasets.Value("string"),
86
+ "pics_array": datasets.Array3D(dtype="uint8", shape=(3, 768, 1024)), # Assuming images are RGB with shape 768x1024
87
+ "image_resolution": {
88
+ "width": datasets.Value("int32"),
89
+ "height": datasets.Value("int32"),
90
+ },
91
+ "annotations": datasets.Sequence({
92
+ "category_id": datasets.Value("int32"),
93
+ "bounding_box": {
94
+ "x_min": datasets.Value("float32"),
95
+ "y_min": datasets.Value("float32"),
96
+ "x_max": datasets.Value("float32"),
97
+ "y_max": datasets.Value("float32"),
98
+ },
99
+ }),
100
+ })
101
+ return datasets.DatasetInfo(
102
+ description=_DESCRIPTION,
103
+ features=features, # Here we define them because they are different between the two configurations
104
+ homepage=_HOMEPAGE,
105
+ license=_LICENSE,
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
 
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
+
201
+ with Image.open(image_path) as img:
202
+ pics_array = np.array(img)
203
+ width, height = img.size
204
+
205
+ species_row = species_info.loc[species_info['FileName'] == file_name]
206
+ species = species_row['Species'].values[0] if not species_row.empty else None
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
+ }
219
+