@@ -35,7 +35,7 @@ def train_test_split(dataset_index, test_size=0.2):
35
35
"""Split dataset into train set and test set with test_size"""
36
36
test_size = min (max (0.0 , test_size ), 1.0 )
37
37
total_size = len (dataset_index )
38
- train_size = int (math . ceil (total_size * (1.0 - test_size )))
38
+ train_size = int (round (total_size * (1.0 - test_size )))
39
39
random .shuffle (dataset_index )
40
40
train_index = dataset_index [:train_size ]
41
41
test_index = dataset_index [train_size :]
@@ -169,22 +169,19 @@ def _update_id_map(self, label: str):
169
169
self ._label_list .append (label )
170
170
self ._label_id_map [label ] = len (self ._label_id_map )
171
171
172
- def _make_train_val_dir (self ):
172
+ def _make_train_val_dir (self , create_test_dir = False ):
173
173
self ._label_dir_path = os .path .join (self ._json_dir , "YOLODataset/labels/" )
174
174
self ._image_dir_path = os .path .join (self ._json_dir , "YOLODataset/images/" )
175
175
176
- for yolo_path in (
177
- os .path .join (self ._label_dir_path + "train/" ),
178
- os .path .join (self ._label_dir_path + "val/" ),
179
- os .path .join (self ._label_dir_path + "test/" ),
180
- os .path .join (self ._image_dir_path + "train/" ),
181
- os .path .join (self ._image_dir_path + "val/" ),
182
- os .path .join (self ._image_dir_path + "test/" ),
183
- ):
184
- if os .path .exists (yolo_path ):
185
- shutil .rmtree (yolo_path )
176
+ for yolo_path in [self ._label_dir_path , self ._image_dir_path ]:
177
+ shutil .rmtree (yolo_path , ignore_errors = True )
186
178
187
- os .makedirs (yolo_path )
179
+ parts = ["train" , "val" , "test" ] if create_test_dir else ["train" , "val" ]
180
+ image_dirs = [os .path .join (self ._image_dir_path , part ) for part in parts ]
181
+ label_dirs = [os .path .join (self ._label_dir_path , part ) for part in parts ]
182
+ dirs = image_dirs + label_dirs
183
+ for yolo_path in dirs :
184
+ os .makedirs (yolo_path , exist_ok = True )
188
185
189
186
def _get_dataset_part_json_names (self , dataset_part : str ):
190
187
"""Get json names in dataset_part folder"""
@@ -196,15 +193,15 @@ def _get_dataset_part_json_names(self, dataset_part: str):
196
193
json_names .append (sample_name + ".json" )
197
194
return json_names
198
195
199
- def _train_test_split (self , json_names , val_size , test_size ):
196
+ def _train_test_split (self , json_names , val_size , test_size = None ):
200
197
"""Split json names to train, val, test"""
201
198
total_size = len (json_names )
202
199
dataset_index = list (range (total_size ))
203
200
train_ids , val_ids = train_test_split (dataset_index , test_size = val_size )
204
201
test_ids = []
205
202
if test_size is None :
206
203
test_size = 0.0
207
- if test_size > 1e-8 :
204
+ if test_size > 0.0 :
208
205
train_ids , test_ids = train_test_split (
209
206
train_ids , test_size = test_size / (1 - val_size )
210
207
)
@@ -220,19 +217,19 @@ def convert(self, val_size, test_size):
220
217
os .path .join (self ._json_dir , "**" , "*.json" ), recursive = True
221
218
)
222
219
json_names = sorted (json_names )
220
+
223
221
train_json_names , val_json_names , test_json_names = self ._train_test_split (
224
222
json_names , val_size , test_size
225
223
)
226
224
227
- self ._make_train_val_dir ()
225
+ self ._make_train_val_dir (test_size > 0.0 )
228
226
229
227
# convert labelme object to yolo format object, and save them to files
230
228
# also get image from labelme json file and save them under images folder
231
- dirs = ("train/ " , "val/ " , "test/ " )
229
+ dirs = ("train" , "val" , "test" )
232
230
names = (train_json_names , val_json_names , test_json_names )
233
231
for target_dir , json_names in zip (dirs , names ):
234
- target_part = target_dir .replace ("/" , "" )
235
- logger .info ("Converting %s set ..." , target_part )
232
+ logger .info ("Converting %s set ..." , target_dir )
236
233
for json_name in tqdm .tqdm (json_names ):
237
234
self .covert_json_to_text (target_dir , json_name )
238
235
@@ -291,21 +288,20 @@ def _get_circle_shape_yolo_object(self, shape, img_h, img_w):
291
288
(obj_center_x - shape ["points" ][1 ][0 ]) ** 2
292
289
+ (obj_center_y - shape ["points" ][1 ][1 ]) ** 2
293
290
)
294
- obj_w = 2 * radius
295
- obj_h = 2 * radius
296
-
297
- yolo_center_x = round (float (obj_center_x / img_w ), 6 )
298
- yolo_center_y = round (float (obj_center_y / img_h ), 6 )
299
- yolo_w = round (float (obj_w / img_w ), 6 )
300
- yolo_h = round (float (obj_h / img_h ), 6 )
291
+ num_points = 36
292
+ points = np .zeros (2 * num_points )
293
+ for i in range (num_points ):
294
+ angle = 2.0 * math .pi * i / num_points
295
+ points [2 * i ] = (obj_center_x + radius * math .cos (angle )) / img_w
296
+ points [2 * i + 1 ] = (obj_center_y + radius * math .sin (angle )) / img_h
301
297
302
298
if shape ["label" ]:
303
299
label = shape ["label" ]
304
300
if label not in self ._label_list :
305
301
self ._update_id_map (label )
306
302
label_id = self ._label_id_map [shape ["label" ]]
307
303
308
- return label_id , yolo_center_x , yolo_center_y , yolo_w , yolo_h
304
+ return label_id , points . tolist ()
309
305
310
306
return None
311
307
@@ -344,10 +340,17 @@ def _save_dataset_yaml(self):
344
340
names_str += f'"{ label } ", '
345
341
names_str = names_str .rstrip (", " )
346
342
347
- content = (
348
- f"train: { train_dir } \n val: { val_dir } \n test: { test_dir } \n "
349
- f"nc: { len (self ._label_id_map )} \n "
350
- f"names: [{ names_str } ]"
351
- )
343
+ if os .path .exists (test_dir ):
344
+ content = (
345
+ f"train: { train_dir } \n val: { val_dir } \n test: { test_dir } \n "
346
+ f"nc: { len (self ._label_id_map )} \n "
347
+ f"names: [{ names_str } ]"
348
+ )
349
+ else :
350
+ content = (
351
+ f"train: { train_dir } \n val: { val_dir } \n "
352
+ f"nc: { len (self ._label_id_map )} \n "
353
+ f"names: [{ names_str } ]"
354
+ )
352
355
353
356
yaml_file .write (content )
0 commit comments