1- #coding=utf-8
1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+ # @File : jade_voc_datasets.py
4+ # @Author : jade
5+ # @Date : 2022/3/9 13:33
6+ # @Email : jadehh@1ive.com
7+ # @Software : Samples
8+ # @Desc :
9+ from jade import *
10+ from opencv_tools import *
211from xml .dom import minidom
312from dataset_tools import *
4- from jade import GetLastDir ,CreateSavePath
5- import os
6- import numpy as np
7-
813def GetXmlClassesNames (xml_path ):
914 classnames = []
1015 # Read the XML annotation file.
@@ -29,13 +34,6 @@ def GetXmlClassesNames(xml_path):
2934 if label not in classnames :
3035 classnames .append (label )
3136 return classnames
32-
33-
34-
35-
36-
37-
38-
3937def ProcessXml (xml_path ,is_rate = True ):
4038 # Read the XML annotation file.
4139 tree = ET .parse (xml_path )
@@ -81,10 +79,6 @@ def ProcessXml(xml_path,is_rate=True):
8179
8280 imagename = GetLastDir (xml_path )[:- 4 ]+ '.jpg'
8381 return imagename ,shape , bboxes , labels_text ,labels , difficult , truncated
84-
85-
86-
87-
8882def ProcessXml_Dataset (xml_path ):
8983 # Read the XML annotation file.
9084 tree = ET .parse (xml_path )
@@ -113,7 +107,6 @@ def ProcessXml_Dataset(xml_path):
113107 else :
114108 new_groudth .append ([0 ,0 ,0 ,0 ,0 ])
115109 return np .array (new_groudth )
116-
117110# 生成XML文件方式
118111def GenerateXml (filename ,shape ,bboxes ,labels_text ,save_path ):
119112 CreateSavePath (save_path )
@@ -211,33 +204,65 @@ def GenerateXml(filename,shape,bboxes,labels_text,save_path):
211204 f .write (doc .toprettyxml (indent = " " ).encode ("utf-8" ))
212205 f .close ()
213206
207+ class JadeVOCDatasets (object ):
208+ def __init__ (self ,root_path ):
209+ self .root_path = root_path
210+ super (JadeVOCDatasets , self ).__init__ ()
214211
215- def mkdir (path ):
216- # 引入模块
217- import os
218-
219- # 去除首位空格
220- path = path .strip ()
221- # 去除尾部 \ 符号
222- path = path .rstrip ("\\ " )
223-
224- # 判断路径是否存在
225- # 存在 True
226- # 不存在 False
227- isExists = os .path .exists (path )
228-
229- # 判断结果
230- if not isExists :
231- # 如果不存在则创建目录 # 创建目录操作函数
232- os .makedirs (path )
212+ def remove_no_labels (self ):
213+ file_list = os .listdir (self .root_path )
214+ processBar = ProgressBar (len (file_list ))
215+ for file_name in file_list :
216+ images_path = os .path .join (self .root_path ,file_name ,DIRECTORY_IMAGES )
217+ annos_path = os .path .join (self .root_path ,file_name ,DIRECTORY_ANNOTATIONS )
218+ image_list = GetAllImagesPath (images_path )
219+ for image_path in image_list :
220+ anno_path = os .path .join (annos_path ,GetLastDir (image_path )[:- 4 ]+ ".xml" )
221+ if os .path .exists (anno_path ):
222+ imagename ,shape , bboxes , labels_text ,labels , difficult , truncated = ProcessXml (anno_path )
223+ if len (labels_text ) == 0 :
224+ os .remove (image_path )
225+ else :
226+ os .remove (image_path )
227+ processBar .update ()
233228
234- print (path + ' 创建成功' )
235- return True
236- else :
237- # 如果目录存在则不创建,并提示目录已存在
238- print (path + ' 目录已存在' )
239- return False
240- #smb://192.168.1.202/data/HAND_DATASET/VOC_HANDS_DATASET.tar.gz
229+ def change_labels (self ,change_labesl ,changed_label ):
230+ file_list = os .listdir (self .root_path )
231+ processBar = ProgressBar (len (file_list ))
232+ for file_name in file_list :
233+ if os .path .isdir (os .path .join (self .root_path ,file_name )):
234+ images_path = os .path .join (self .root_path , file_name , DIRECTORY_IMAGES )
235+ annos_path = os .path .join (self .root_path , file_name , DIRECTORY_ANNOTATIONS )
236+ image_list = GetAllImagesPath (images_path )
237+ for image_path in image_list :
238+ anno_path = os .path .join (annos_path , GetLastDir (image_path )[:- 4 ] + ".xml" )
239+ if os .path .exists (anno_path ):
240+ imagename , shape , bboxes , labels_text , labels , difficult , truncated = ProcessXml (anno_path ,is_rate = False )
241+ new_labels_text = []
242+ for label_text in labels_text :
243+ if label_text in change_labesl :
244+ new_labels_text .append (changed_label )
245+ GenerateXml (GetLastDir (image_path )[:- 4 ],shape ,bboxes ,new_labels_text ,os .path .join (self .root_path ,file_name ,DIRECTORY_ANNOTATIONS ))
246+ processBar .update ()
241247
248+ def video_to_voc (self ,save_path ,detector = None ,fps = 5 ):
249+ video_list = GetFilesWithLastNamePath (self .root_path , ".avi" )
250+ processBar = ProgressBar (len (video_list ))
251+ for video_path in video_list :
252+ capture = cv2 .VideoCapture (video_path )
253+ index = 0
254+ while capture .isOpened ():
255+ ret , frame = capture .read ()
256+ if ret is False :
257+ break
258+ if detector is None :
259+ if index % fps == 0 :
260+ WriteChienePath (os .path .join (save_path , GetSeqNumber () + ".jpg" ), frame )
261+ index = index + 1
262+ processBar .update ()
242263
243264
265+ if __name__ == '__main__' :
266+ jadeVOCDatasets = JadeVOCDatasets (r'F:\数据集\VOC数据集\定制版顶相机箱号检测数据集' )
267+ # jadeVOCDatasets.remove_no_labels()
268+ jadeVOCDatasets .change_labels (["PCTNNO" ,"NCTNNO" ],"CTNNO" )
0 commit comments