@@ -174,12 +174,45 @@ def fruit_detection_worldwide(self, dataset_name):
174
174
anno_data_all , label2id , output_json_file ,
175
175
output_img_path , general_info )
176
176
177
- def apple_detection_usa (self , dataset_name ):
177
+ def apple_detection_usa (self , dataset_name , fix = False ):
178
+ # Just a quick fix to clip over-sized bounding boxes.
179
+ if fix :
180
+ # Load in the annotations.
181
+ dataset_dir = os .path .join (self .data_original_dir , dataset_name )
182
+ with open (os .path .join (dataset_dir , 'annotations.json' ), 'r' ) as f :
183
+ annotations = json .load (f )
184
+
185
+ # Get the images and all of their heights/widths.
186
+ images = annotations ['images' ]
187
+ image_id_content_map = {}
188
+ for image in images :
189
+ image_id_content_map [image ['id' ]] = (image ['height' ], image ['width' ])
190
+
191
+ # Load all of the annotations.
192
+ new_annotations = []
193
+ for a in annotations ['annotations' ]:
194
+ new_a = a .copy ()
195
+ height , width = image_id_content_map [a ['image_id' ]]
196
+ (x , y , w , h ) = a ['bbox' ]
197
+ x1 , y1 , x2 , y2 = x , y , x + w , y + h
198
+ x1 = np .clip (x1 , 0 , width )
199
+ x2 = np .clip (x2 , 0 , width )
200
+ y1 = np .clip (y1 , 0 , height )
201
+ y2 = np .clip (y2 , 0 , height )
202
+ new_a ['bbox' ] = [int (i ) for i in [x1 , y1 , x2 - x1 , y2 - y1 ]]
203
+ new_annotations .append (new_a )
204
+
205
+ # Save the annotations.
206
+ annotations ['annotations' ] = new_annotations
207
+ with open (os .path .join (dataset_dir , 'annotations.json' ), 'w' ) as f :
208
+ json .dump (annotations , f )
209
+ return
210
+
178
211
# resize the dataset
179
212
resize = 1.0
180
213
181
214
# Read public_datasources.json to get class information
182
- category_info = self .data_sources [dataset_name ]['crop_types ' ]
215
+ category_info = self .data_sources [dataset_name ]['classes ' ]
183
216
labels_str = []
184
217
labels_ids = []
185
218
for info in category_info :
@@ -197,18 +230,16 @@ def apple_detection_usa(self, dataset_name):
197
230
198
231
# do tasks along folders
199
232
anno_data_all = []
200
- img_ids = []
201
- bbox_ids = []
202
233
for folder in plant_folders :
203
234
# Get image file and xml file
204
235
full_path = os .path .join (obj_Detection_data ,folder )
205
236
all_files = get_file_list (full_path )
206
237
anno_files = [x for x in all_files if "txt" in x ]
207
238
for anno_file in anno_files :
208
239
anno_line = []
209
- anno_path = os .path .join (full_path ,anno_file )
240
+ anno_path = os .path .join (full_path , anno_file )
210
241
# Opening annotation file
211
- anno_data = read_txt_file (anno_path ,delimiter = ',' )
242
+ anno_data = read_txt_file (anno_path , delimiter = ',' )[ 0 ]
212
243
213
244
for i , anno in enumerate (anno_data ):
214
245
new_anno = [os .path .join (dataset_dir , anno_data [i ][0 ])]
@@ -510,7 +541,24 @@ def _annotation_preprocess_fn(annotation_path, out_path):
510
541
annotation_preprocess_fn = _annotation_preprocess_fn
511
542
)
512
543
513
- def rice_seedling_segmentation (self , dataset_name ):
544
+ def rice_seedling_segmentation (self , dataset_name , fix = False ):
545
+ # Re-mapping labels to remove the `Background` class.
546
+ if fix :
547
+ data_dir = os .path .join (self .data_original_dir , dataset_name )
548
+ annotations = sorted ([
549
+ os .path .join (data_dir , 'annotations' , i )
550
+ for i in os .listdir (os .path .join (data_dir , 'annotations' ))])
551
+ os .makedirs (os .path .join (data_dir , 'new_annotations' ))
552
+
553
+ # Create the remap.
554
+ for annotation in tqdm (annotations ):
555
+ a = cv2 .imread (annotation )
556
+ a [a == 2 ] = 0
557
+ a [a == 3 ] = 2
558
+ cv2 .imwrite (os .path .join (
559
+ data_dir , 'new_annotations' , os .path .basename (annotation )), a )
560
+ return
561
+
514
562
# Get all of the relevant data.
515
563
data_dir = os .path .join (self .data_original_dir , dataset_name )
516
564
images = sorted (glob .glob (os .path .join (data_dir , 'image_*.jpg' )))
0 commit comments