Skip to content

Commit 7ae928f

Browse files
committed
update 支持Paddle数据集转YOLO数据集
1 parent 0fecd5a commit 7ae928f

2 files changed

Lines changed: 104 additions & 79 deletions

File tree

dataset_tools/jade_voc_datasets.py

Lines changed: 102 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from xml.dom import minidom
1212
from dataset_tools import *
1313
from opencv_tools import *
14+
15+
1416
def GetXmlClassesNames(xml_path):
1517
classnames = []
1618
# Read the XML annotation file.
@@ -30,12 +32,14 @@ def GetXmlClassesNames(xml_path):
3032
truncated = []
3133
classnames = []
3234
for obj in root.findall('object'):
33-
#label = (obj.find('bndbox')).find('name').text
35+
# label = (obj.find('bndbox')).find('name').text
3436
label = obj.find('name').text
3537
if label not in classnames:
3638
classnames.append(label)
3739
return classnames
38-
def ProcessXml(xml_path,is_rate=True):
40+
41+
42+
def ProcessXml(xml_path, is_rate=True):
3943
# Read the XML annotation file.
4044
tree = ET.parse(xml_path)
4145
root = tree.getroot()
@@ -52,7 +56,7 @@ def ProcessXml(xml_path,is_rate=True):
5256
difficult = []
5357
truncated = []
5458
for obj in root.findall('object'):
55-
#label = (obj.find('bndbox')).find('name').text
59+
# label = (obj.find('bndbox')).find('name').text
5660
label = obj.find('name').text
5761
if label in VOC_LABELS.keys():
5862
labels.append(str(VOC_LABELS[label][0]))
@@ -71,15 +75,18 @@ def ProcessXml(xml_path,is_rate=True):
7175

7276
bbox = obj.find('bndbox')
7377
if is_rate is False:
74-
bboxes.append([float(bbox.find('xmin').text) ,float(bbox.find('ymin').text),float(bbox.find('xmax').text),float(bbox.find('ymax').text)])
78+
bboxes.append([float(bbox.find('xmin').text), float(bbox.find('ymin').text), float(bbox.find('xmax').text),
79+
float(bbox.find('ymax').text)])
7580
else:
7681
bboxes.append((float(bbox.find('xmin').text) / float(shape[1]),
7782
float(bbox.find('ymin').text) / float(shape[0]),
7883
float(bbox.find('xmax').text) / float(shape[1]),
7984
float(bbox.find('ymax').text) / float(shape[0])))
8085

81-
imagename = GetLastDir(xml_path)[:-4]+'.jpg'
82-
return imagename,shape, bboxes, labels_text,labels, difficult, truncated
86+
imagename = GetLastDir(xml_path)[:-4] + '.jpg'
87+
return imagename, shape, bboxes, labels_text, labels, difficult, truncated
88+
89+
8390
def ProcessXml_Dataset(xml_path):
8491
# Read the XML annotation file.
8592
tree = ET.parse(xml_path)
@@ -94,50 +101,52 @@ def ProcessXml_Dataset(xml_path):
94101
ground_truth = []
95102
labels = []
96103
for obj in root.findall('object'):
97-
#label = (obj.find('bndbox')).find('name').text
104+
# label = (obj.find('bndbox')).find('name').text
98105
label = obj.find('name').text
99106
bbox = obj.find('bndbox')
100107
ground_truth.append(np.array([int(bbox.find('xmin').text) / shape[1],
101-
int(bbox.find('ymin').text) / shape[0],
102-
int(bbox.find('xmax').text)/shape[1],
103-
int(bbox.find('ymax').text)/shape[0],int(VOC_LABELS[label][0])]))
108+
int(bbox.find('ymin').text) / shape[0],
109+
int(bbox.find('xmax').text) / shape[1],
110+
int(bbox.find('ymax').text) / shape[0], int(VOC_LABELS[label][0])]))
104111
new_groudth = []
105112
for i in range(10):
106-
if i < len(ground_truth):
113+
if i < len(ground_truth):
107114
new_groudth.append(ground_truth[i])
108115
else:
109-
new_groudth.append([0,0,0,0,0])
110-
return np.array(new_groudth)
116+
new_groudth.append([0, 0, 0, 0, 0])
117+
return np.array(new_groudth)
118+
119+
111120
# 生成XML文件方式
112-
def GenerateXml(filename,shape,bboxes,labels_text,save_path):
121+
def GenerateXml(filename, shape, bboxes, labels_text, save_path):
113122
CreateSavePath(save_path)
114123
# 为根元素添加10个子元素
115-
difficult = [0]*len(bboxes)
116-
truncated = [0]*len(bboxes)
124+
difficult = [0] * len(bboxes)
125+
truncated = [0] * len(bboxes)
117126
obj_numbsers = len(labels_text)
118-
childElementname = ['folder','filename','source','size','segmented','object']
119-
sourcename = ['database','annotation','image']
120-
source_value = ['The Hand Database','VOC Hand','flickr']
121-
sizename = ['width','height','depth']
127+
childElementname = ['folder', 'filename', 'source', 'size', 'segmented', 'object']
128+
sourcename = ['database', 'annotation', 'image']
129+
source_value = ['The Hand Database', 'VOC Hand', 'flickr']
130+
sizename = ['width', 'height', 'depth']
122131
width = int(shape[1])
123132
height = int(shape[0])
124133
depth = int(shape[2])
125-
size_value = [width,height,depth]
126-
objname = ['name','pose','difficult','truncated','bndbox']
127-
bndbox_key = ['xmin','ymin','xmax','ymax']
128-
obj_value = [labels_text,['Unspecified']*obj_numbsers,difficult,truncated]
134+
size_value = [width, height, depth]
135+
objname = ['name', 'pose', 'difficult', 'truncated', 'bndbox']
136+
bndbox_key = ['xmin', 'ymin', 'xmax', 'ymax']
137+
obj_value = [labels_text, ['Unspecified'] * obj_numbsers, difficult, truncated]
129138
bndbox_value = bboxes
130139
doc = minidom.Document()
131140
annotation = doc.createElement("annotation")
132141
doc.appendChild(annotation)
133142
for i in range(6):
134143

135-
if i==0:
144+
if i == 0:
136145
secname = doc.createElement(childElementname[i])
137146
annotation.appendChild(secname)
138147
source_name = doc.createTextNode("JPEGImages")
139148
secname.appendChild(source_name)
140-
if i==1:
149+
if i == 1:
141150
secname = doc.createElement(childElementname[i])
142151
annotation.appendChild(secname)
143152
source_name = doc.createTextNode(filename)
@@ -174,22 +183,22 @@ def GenerateXml(filename,shape,bboxes,labels_text,save_path):
174183
for m in range(4):
175184
forthname = doc.createElement(bndbox_key[m])
176185
thirdname.appendChild(forthname)
177-
if m==0:
186+
if m == 0:
178187
xmin = int(bndbox_value[n][m])
179188
if xmin < 0:
180189
xmin = 0
181190
source_name = doc.createTextNode(str(xmin))
182-
if m==1:
191+
if m == 1:
183192
ymin = int(float(bndbox_value[n][m]))
184193
if ymin < 0:
185194
ymin = 0
186195
source_name = doc.createTextNode(str(ymin))
187-
if m==2:
196+
if m == 2:
188197
xmax = int(bndbox_value[n][m])
189198
if xmax > width:
190199
xmax = width
191200
source_name = doc.createTextNode(str(xmax))
192-
if m==3:
201+
if m == 3:
193202
ymax = int(bndbox_value[n][m])
194203
if ymax > height:
195204
ymax = height
@@ -200,53 +209,58 @@ def GenerateXml(filename,shape,bboxes,labels_text,save_path):
200209
source_name = doc.createTextNode(str(obj_value[k][n]))
201210
thirdname.appendChild(source_name)
202211

203-
save_xml_path = os.path.join(save_path,filename+".xml")
212+
save_xml_path = os.path.join(save_path, filename + ".xml")
204213
f = open(save_xml_path, "wb")
205214
f.write(doc.toprettyxml(indent=" ").encode("utf-8"))
206215
f.close()
207216

217+
208218
class JadeVOCDatasets(object):
209-
def __init__(self,root_path):
219+
def __init__(self, root_path):
210220
self.root_path = root_path
211221
super(JadeVOCDatasets, self).__init__()
212222

213223
def remove_no_labels(self):
214224
file_list = os.listdir(self.root_path)
215-
processBar = ProgressBar(len(file_list))
225+
processBar = ProgressBar(len(file_list))
216226
for file_name in file_list:
217-
images_path = os.path.join(self.root_path,file_name,DIRECTORY_IMAGES)
218-
annos_path = os.path.join(self.root_path,file_name,DIRECTORY_ANNOTATIONS)
219-
image_list = GetAllImagesPath(images_path)
220-
for image_path in image_list:
221-
anno_path = os.path.join(annos_path,GetLastDir(image_path)[:-4]+".xml")
222-
if os.path.exists(anno_path):
223-
imagename,shape, bboxes, labels_text,labels, difficult, truncated = ProcessXml(anno_path)
224-
if len(labels_text) == 0:
227+
if os.path.isdir(os.path.join(self.root_path, file_name)):
228+
images_path = os.path.join(self.root_path, file_name, DIRECTORY_IMAGES)
229+
annos_path = os.path.join(self.root_path, file_name, DIRECTORY_ANNOTATIONS)
230+
image_list = GetAllImagesPath(images_path)
231+
for image_path in image_list:
232+
anno_path = os.path.join(annos_path, GetLastDir(image_path)[:-4] + ".xml")
233+
if os.path.exists(anno_path):
234+
imagename, shape, bboxes, labels_text, labels, difficult, truncated = ProcessXml(anno_path)
235+
if len(labels_text) == 0:
236+
os.remove(image_path)
237+
os.remove(anno_path)
238+
else:
225239
os.remove(image_path)
226-
else:
227-
os.remove(image_path)
228240
processBar.update()
229241

230-
def change_labels(self,change_labesl,changed_label):
242+
def change_labels(self, change_labesl, changed_label):
231243
file_list = os.listdir(self.root_path)
232-
processBar = ProgressBar(len(file_list))
244+
processBar = ProgressBar(len(file_list))
233245
for file_name in file_list:
234-
if os.path.isdir(os.path.join(self.root_path,file_name)):
246+
if os.path.isdir(os.path.join(self.root_path, file_name)):
235247
images_path = os.path.join(self.root_path, file_name, DIRECTORY_IMAGES)
236248
annos_path = os.path.join(self.root_path, file_name, DIRECTORY_ANNOTATIONS)
237249
image_list = GetAllImagesPath(images_path)
238250
for image_path in image_list:
239251
anno_path = os.path.join(annos_path, GetLastDir(image_path)[:-4] + ".xml")
240252
if os.path.exists(anno_path):
241-
imagename, shape, bboxes, labels_text, labels, difficult, truncated = ProcessXml(anno_path,is_rate=False)
253+
imagename, shape, bboxes, labels_text, labels, difficult, truncated = ProcessXml(anno_path,
254+
is_rate=False)
242255
new_labels_text = []
243256
for label_text in labels_text:
244257
if label_text in change_labesl:
245258
new_labels_text.append(changed_label)
246-
GenerateXml(GetLastDir(image_path)[:-4],shape,bboxes,new_labels_text,os.path.join(self.root_path,file_name,DIRECTORY_ANNOTATIONS))
259+
GenerateXml(GetLastDir(image_path)[:-4], shape, bboxes, new_labels_text,
260+
os.path.join(self.root_path, file_name, DIRECTORY_ANNOTATIONS))
247261
processBar.update()
248262

249-
def video_to_voc(self,save_path,detector=None,fps=5):
263+
def video_to_voc(self, save_path, detector=None, fps=5):
250264
video_list = GetFilesWithLastNamePath(self.root_path, ".avi")
251265
processBar = ProgressBar(len(video_list))
252266
for video_path in video_list:
@@ -262,66 +276,77 @@ def video_to_voc(self,save_path,detector=None,fps=5):
262276
index = index + 1
263277
processBar.update()
264278

279+
def remove_labels_file(self):
280+
progressBar = ProgressBar(len(os.listdir(self.root_path)))
281+
for year in os.listdir(self.root_path):
282+
if os.path.isdir(os.path.join(self.root_path, year)):
283+
labels_dir = os.path.join(self.root_path, year, "Labels")
284+
if os.path.exists(labels_dir):
285+
shutil.rmtree(labels_dir)
286+
progressBar.update()
287+
265288
def VOCDatasetToYoloDatasets(self):
289+
"""
290+
class, x_center, y_center, width, heigh
291+
"""
266292
VOC_LABELS = []
267-
with open(os.path.join(self.root_path,"label_list.txt"),"rb") as f:
293+
with open(os.path.join(self.root_path, "label_list.txt"), "rb") as f:
268294
content_list = f.readlines()
269295
for content in content_list:
270296
content = str(content, encoding="utf-8").strip()
271297
VOC_LABELS.append(content)
272298
progressBar = ProgressBar(len(os.listdir(self.root_path)))
273299
for year in os.listdir(self.root_path):
274-
if os.path.isdir(os.path.join(self.root_path,year)):
275-
labels_dir = os.path.join(self.root_path,year,"Labels")
300+
if os.path.isdir(os.path.join(self.root_path, year)):
301+
labels_dir = os.path.join(self.root_path, year, "Labels")
276302
if os.path.exists(labels_dir):
277303
shutil.rmtree(labels_dir)
278304
CreateSavePath(labels_dir)
279-
xml_path_list = GetFilesWithLastNamePath(os.path.join(self.root_path,year,DIRECTORY_ANNOTATIONS),".xml")
305+
xml_path_list = GetFilesWithLastNamePath(os.path.join(self.root_path, year, DIRECTORY_ANNOTATIONS),
306+
".xml")
280307
for xml_path in xml_path_list:
281-
imagename,shape, bboxes, labels_text,labels, difficult, truncated = ProcessXml(xml_path)
282-
with open(os.path.join(labels_dir,"{}.txt".format(imagename.split(".")[0])),"wb") as f:
308+
_, shape, bboxes, labels_text, labels, difficult, truncated = ProcessXml(xml_path)
309+
label_path = os.path.join(labels_dir, "{}.txt".format(GetLastDir(xml_path).split(".xml")[0]))
310+
with open(label_path, "wb") as f:
283311
for (label, box) in zip(labels_text, bboxes):
284-
f.write("{} {} {} {} {}".format(VOC_LABELS.index(label),box[0],box[1],box[2],box[3]).encode("utf-8"))
312+
center_x = (box[2] + box[0]) / 2
313+
center_y = (box[3] + box[1]) / 2
314+
width = box[2] - box[0]
315+
height = box[3] - box[1]
316+
f.write(
317+
"{} {} {} {} {}".format(VOC_LABELS.index(label), center_x,center_y, width, height).encode(
318+
"utf-8"))
285319
progressBar.update()
286-
# with open(os.path.join(self.root_path, "train.txt"), "rb") as f:
287-
# content_list = f.readlines()
288-
# for content in content_list:
289-
# content = str(content, encoding="utf-8")
290-
# image_path, xml_path = os.path.join(self.root_path, content.split(" ")[0].strip()), os.path.join(self.root_path, content.split(" ")[1].strip())
291-
# imagename,shape, bboxes, labels_text,labels, difficult, truncated = ProcessXml(xml_path)
292-
# for (label,box) in zip(labels_text,bboxes):
293-
# print(VOC_LABELS.index(label),box)
294-
295320

296321

297322
def remove_voc_imagesets(root_path):
298323
processBar = ProgressBar(len(os.listdir(root_path)))
299324
for year in os.listdir(root_path):
300-
if os.path.isdir(os.path.join(root_path,year)):
301-
year_path = os.path.join(root_path,year)
325+
if os.path.isdir(os.path.join(root_path, year)):
326+
year_path = os.path.join(root_path, year)
302327
for file in os.listdir(year_path):
303-
if file == "ImageSets":
304-
shutil.rmtree(os.path.join(year_path,file))
328+
if file == "ImageSets":
329+
shutil.rmtree(os.path.join(year_path, file))
305330
processBar.update()
306331

307-
def paddle_pretrain_detection_dataset(root_path,detector,threshold=0.6):
308-
image_path = os.path.join(root_path,DIRECTORY_IMAGES)
309-
save_anno_path = CreateSavePath(os.path.join(root_path,DIRECTORY_ANNOTATIONS))
332+
333+
def paddle_pretrain_detection_dataset(root_path, detector, threshold=0.6):
334+
image_path = os.path.join(root_path, DIRECTORY_IMAGES)
335+
save_anno_path = CreateSavePath(os.path.join(root_path, DIRECTORY_ANNOTATIONS))
310336
image_path_list = GetAllImagesPath(os.path.join(image_path))
311337
processBar = ProgressBar(len(image_path_list))
312338
for image_path in image_path_list:
313339
file_name = GetLastDir(image_path).split(".")[0]
314340
image = ReadChinesePath(image_path)
315-
result = detector.predict(image,threshold)
341+
result = detector.predict(image, threshold)
316342
if result["labels"][0] == -1:
317-
GenerateXml(file_name,image.shape,[],[],save_anno_path)
343+
GenerateXml(file_name, image.shape, [], [], save_anno_path)
318344
else:
319-
GenerateXml(file_name,image.shape,result["boxes"],result["labels"],save_anno_path)
345+
GenerateXml(file_name, image.shape, result["boxes"], result["labels"], save_anno_path)
320346
processBar.update()
321347

322348

323-
324349
if __name__ == '__main__':
325350
jadeVOCDatasets = JadeVOCDatasets(r'E:\Data\VOC数据集\箱门检测数据集\ContainVOC')
326351
# jadeVOCDatasets.remove_no_labels()
327-
jadeVOCDatasets.VOCDatasetToYoloDatasets()
352+
jadeVOCDatasets.VOCDatasetToYoloDatasets()

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
from dataset_tools.jade_create_paddle_ocr_datasets import *
1111
from dataset_tools.jade_create_object_dection_datasets import CreateYearsDatasets
1212
if __name__ == '__main__':
13-
1413
#removeNolabelDatasets(r"F:\数据集\关键点检测数据集\定制版箱号关键点数据集\2022-03-09")
1514
#create_text_detection_datasets(r"F:\数据集\关键点检测数据集\定制版箱号关键点数据集",r"E:\Data\字符检测识别数据集\定制版箱号关键点数据集",0.95)
1615
#CreatePaddleOCRDatasets(root_path="E:\Data\字符检测识别数据集\镇江大港厂内车牌关键点检测数据集", save_path="E:\Data\OCR\镇江大港厂内车牌识别数据集",dataset_type="镇江厂内车牌数据集")
1716
#removeNolabelVocDatasets(r"E:\Data\VOC数据集\集装箱残损检测数据集")
1817
#CreateYearsDatasets(r"E:\Data\VOC数据集\集装箱残损检测数据集")
1918
#create_text_detection_datasets(r"F:\数据集\关键点检测数据集\箱号关键点数据集",r'E:\Data\字符检测识别数据集\箱号关键点数据集')
20-
CreatePaddleOCRDatasets(r'E:\Data\字符检测识别数据集\箱号关键点数据集', save_path="E:\Data\OCR\箱号识别数据集",dataset_type="箱号数据集")
19+
#CreatePaddleOCRDatasets(r'F:\数据集\VOC数据集\箱门检测数据集\ContainVOC', save_path="E:\Data\OCR\箱号识别数据集",dataset_type="箱号数据集")
20+
CreateYearsDatasets("E:\Data\VOC数据集\箱门检测数据集\ContainVOC",0.95)

0 commit comments

Comments
 (0)