33import argparse
44import json
55import configparser as cfg
6- import os
6+ import os
77import shutil
88# pathlib https://www.cnblogs.com/sigai/p/8074329.html
99# coco 数据格式 http://www.xyu.ink/3612.html
1919'''
2020
2121class DARKNET2COCO :
22- def __init__ (self ,genconfig_data ):
22+ def __init__ (self ,genconfig_data ):
2323 self .src_data = genconfig_data
2424 self .src = Path (self .src_data ).parent
2525 self .dst = Path (self .src ) / "coco_dataset"
@@ -52,11 +52,11 @@ def __init__(self,genconfig_data):
5252 if not (Path (self .dst )/ self .coco_images / self .coco_train ).is_dir ():
5353 ( Path (self .dst )/ self .coco_images / self .coco_train ).mkdir ()
5454
55-
55+
5656 if not Path (self .dst / self .coco_images / self .coco_valid ).is_dir ():
5757 ( Path (self .dst )/ self .coco_images / self .coco_valid ).mkdir ()
5858
59-
59+
6060 if not (Path (self .dst )/ self .coco_annotation ).is_dir ():
6161 ( Path (self .dst )/ self .coco_annotation ).mkdir ()
6262
@@ -68,7 +68,7 @@ def __init__(self,genconfig_data):
6868
6969 def initcfg (self ):
7070 if not self .ready :
71- return
71+ return
7272 self .cnf = cfg .RawConfigParser ()
7373 with open (self .src_data ) as f :
7474 file_content = '[dummy_section]\n ' + f .read ()
@@ -105,9 +105,9 @@ def get_list(self,name):
105105 allfiles = f .readlines ()
106106 for file in allfiles :
107107 content .append (file .strip ())
108-
108+
109109 return content
110- # derived from https://github.com/zhiqwang/yolov5-rt-stack/blob/master/yolort/utils/yolo2coco.py
110+ # derived from https://github.com/zhiqwang/yolov5-rt-stack/blob/master/yolort/utils/yolo2coco.py
111111 def _get_annotation (self ,vertex_info , height , width ):
112112
113113 cx , cy , w , h = [float (i ) for i in vertex_info ]
@@ -123,14 +123,14 @@ def _get_annotation(self,vertex_info, height, width):
123123
124124 bbox = [x , y , w , h ]
125125 return segmentation , bbox , area
126-
126+
127127 def read_annotation (self ,txtfile ,img_id ,height ,width ,annotation_id ):
128128 annotation = []
129129
130130 if not Path (txtfile ).exists ():
131131 return {},0
132132 with open (txtfile ) as f :
133- allinfo = f .readlines ()
133+ allinfo = f .readlines ()
134134
135135 for line in allinfo :
136136 label_info = line .replace ('\n ' , '' ).replace ('\r ' , '' )
@@ -139,7 +139,7 @@ def read_annotation(self,txtfile,img_id,height,width,annotation_id):
139139 continue
140140
141141 category_id , vertex_info = label_info [0 ], label_info [1 :]
142-
142+
143143 segmentation , bbox , area = self ._get_annotation (vertex_info , height , width )
144144 annotation .append ( {
145145 'segmentation' : segmentation ,
@@ -151,7 +151,7 @@ def read_annotation(self,txtfile,img_id,height,width,annotation_id):
151151 'id' : annotation_id ,
152152 })
153153 annotation_id += 1
154-
154+
155155 return annotation ,annotation_id
156156
157157 def get_category (self ):
@@ -165,15 +165,15 @@ def get_category(self):
165165
166166 def generate (self ):
167167 self .classnum = self .getint ("classes" )
168- self .train = Path ( self .src_data ).parent / Path (self .getstring ("train" )).name
169- self .valid = Path ( self .src_data ).parent / Path (self .getstring ("valid" )).name
170- self .names = Path ( self .src_data ).parent / Path (self .getstring ("names" )).name
168+ self .train = Path ( self .src_data ).parent / Path (self .getstring ("train" )).name
169+ self .valid = Path ( self .src_data ).parent / Path (self .getstring ("valid" )).name
170+ self .names = Path ( self .src_data ).parent / Path (self .getstring ("names" )).name
171171 self .train_files = self .get_path (self .train )
172172 if os .path .exists (self .valid ):
173173 self .valid_files = self .get_path (self .valid )
174174 self .name_lists = self .get_list (self .names )
175175 self .get_category ()
176-
176+
177177 dest_path_train = Path (self .dst )/ self .coco_images / self .coco_train
178178 self .gen_dataset (self .train_files ,dest_path_train ,self .coco_train_json )
179179
@@ -185,7 +185,7 @@ def generate(self):
185185
186186# https://cocodataset.org/#format-data
187187 def gen_dataset (self ,file_lists ,target_img_path ,target_json ):
188-
188+
189189 images = []
190190 annotations = []
191191 annotation_id = 1
@@ -203,15 +203,15 @@ def gen_dataset(self,file_lists,target_img_path,target_json):
203203 else :
204204 cv .imwrite (str (target_img_path / destfilename ),imgsrc )
205205 # shutil.copyfile(file,target_img_path/ )
206-
206+
207207 image = imgsrc .shape # 获取图片宽高及通道数
208208 height = image [0 ]
209209 width = image [1 ]
210210 images .append ({
211211 'date_captured' : '2021' ,
212212 'file_name' : destfilename ,
213213 'id' : img_id ,
214-
214+
215215 'height' : height ,
216216 'width' : width ,
217217 })
@@ -220,7 +220,7 @@ def gen_dataset(self,file_lists,target_img_path,target_json):
220220 new_anno ,annotation_id = self .read_annotation (txt ,img_id ,height ,width ,annotation_id )
221221 if len (new_anno )> 0 :
222222 annotations .extend (new_anno )
223-
223+
224224
225225
226226 json_data = {
@@ -234,14 +234,14 @@ def gen_dataset(self,file_lists,target_img_path,target_json):
234234 with open (target_json , 'w' , encoding = 'utf-8' ) as f :
235235 json .dump (json_data , f , ensure_ascii = False )
236236
237-
237+
238238class YOLO2COCO (DARKNET2COCO ):
239239 def __init__ (self ,srcdir ):
240240 self .srcdir = srcdir
241241 self .srcimgdir = Path (srcdir )/ "images/train2017"
242242 self .srclabeldir = Path (srcdir )/ "labels/train2017"
243-
244-
243+
244+
245245 if not self .srcimgdir .exists () or not self .srclabeldir .exists ():
246246 raise "wrong path, not found labels or images dir."
247247 self .dstdir = Path (srcdir )/ "darknet"
@@ -255,7 +255,7 @@ def __init__(self,srcdir):
255255 self .classlist = set ()
256256 self .classname = self .dstdir / "classes.names"
257257 self .convert2darknet ()
258-
258+
259259 def convert2darknet (self ):
260260 imgfiles = self .srcimgdir .rglob ("*.jpg" )
261261 with open (self .train ,"w" ) as f :
@@ -281,20 +281,18 @@ def convert2darknet(self):
281281 f .write ("train=gen_train.txt" + "\n " )
282282 f .write ("names=classes.names" + "\n " )
283283 f .write ("valid=none\n " )
284-
284+
285285
286286 with open (self .classname ,"w" ) as f :
287287 maxclass = max (self .classlist )
288288 for clsid in range (maxclass + 1 ):
289289 f .write ("class_" + str (clsid )+ "\n " )
290-
291-
292-
293-
290+
294291 def generate (self ):
295292 super (YOLO2COCO ,self ).__init__ (str (self .gen_config ))
296293 super (YOLO2COCO ,self ).generate ()
297294
295+
298296if __name__ == "__main__" :
299297 parser = argparse .ArgumentParser ('Datasets converter from yolo to coco' , add_help = False )
300298
@@ -309,4 +307,4 @@ def generate(self):
309307 converter = DARKNET2COCO (args .data_path )
310308 else :
311309 converter = YOLO2COCO (args .data_path )
312- converter .generate ()
310+ converter .generate ()
0 commit comments