Skip to content

Commit 2493a76

Browse files
committed
clear code
1 parent ca9632a commit 2493a76

26 files changed

+533
-109
lines changed

coco_visual.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
# !/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
# @File: coco_visual.py
14
import json
2-
import os, cv2
5+
import os
6+
import argparse
37

4-
train_json = 'E:\\workprj\\mousika\\ocr\\yolo2coco\\data\\coco\\annotations\\instance_train2017.json'
5-
train_path = 'E:\\workprj\\mousika\\ocr\\yolo2coco\\data\\coco\\train2017'
8+
import cv2
69

7-
def visualization_bbox1(num_image, json_path,img_path):# 需要画的第num副图片, 对应的json路径和图片路径
10+
11+
def visualization_bbox(num_image, json_path, img_path):
12+
# 需要画的第num副图片, 对应的json路径和图片路径
813
with open(json_path) as annos:
914
annotation_json = json.load(annos)
1015

@@ -23,8 +28,8 @@ def visualization_bbox1(num_image, json_path,img_path):# 需要画的第num副
2328
if annotation_json['annotations'][i]['image_id'] == id:
2429
num_bbox = num_bbox + 1
2530
x, y, w, h = annotation_json['annotations'][i]['bbox'] # 读取边框
26-
image = cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 255, 255), 2)
27-
31+
image = cv2.rectangle(image, (int(x), int(y)),
32+
(int(x + w), int(y + h)), (0, 255, 255), 2)
2833

2934
print('The unm_bbox of the display image is:', num_bbox)
3035

@@ -38,5 +43,11 @@ def visualization_bbox1(num_image, json_path,img_path):# 需要画的第num副
3843
cv2.imshow(image_name, image)
3944
cv2.waitKey(0)
4045

46+
4147
if __name__ == "__main__":
42-
visualization_bbox1(293, train_json, train_path)
48+
parser = argparse.ArgumentParser()
49+
parser.add_argument('--json_path', type=str, required=True)
50+
parser.add_argument('--img_dir', type=str, required=True)
51+
args = parser.parse_args()
52+
53+
visualization_bbox(1, args.json_path, args.img_dir)

dark2yolo.py renamed to darknet2coco.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import argparse
44
import json
55
import configparser as cfg
6-
import os
6+
import os
77
import shutil
88
# pathlib https://www.cnblogs.com/sigai/p/8074329.html
99
# coco 数据格式 http://www.xyu.ink/3612.html
@@ -19,7 +19,7 @@
1919
'''
2020

2121
class 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+
238238
class 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+
298296
if __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()

dataset/YOLOV5/classes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stamp
7.68 KB
Loading
15.9 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 0.497537 0.395161 0.807882 0.653226
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0 0.274554 0.205357 0.388393 0.383929
2+
0 0.754464 0.207589 0.375000 0.379464
3+
0 0.504464 0.511161 0.339286 0.343750
4+
0 0.243304 0.792411 0.379464 0.379464
5+
0 0.774554 0.796875 0.370536 0.370536

dataset/YOLOV5/train.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dataset/YOLOV5/stamp/images/images(3).jpg

dataset/YOLOV5/val.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dataset/YOLOV5/stamp/images/images(13).jpg

0 commit comments

Comments
 (0)