88# @Desc : 制作目标检测数据集
99from dataset_tools import *
1010import os
11- from jade import ProgressBar ,GetLastDir
11+ from jade import ProgressBar ,GetLastDir , CreateSavePath
1212import shutil
1313import random
1414import xml .etree .ElementTree as ET
1515
16+ def CreateSavePath (save_path ):
17+ if os .path .exists (save_path ):
18+ return save_path
19+ else :
20+ os .makedirs (save_path )
21+ return save_path
22+
1623def ProcessXml (xml_path ):
1724 # Read the XML annotation file.
1825 tree = ET .parse (xml_path )
@@ -52,47 +59,64 @@ def ProcessXml(xml_path):
5259 imagename = GetLastDir (xml_path )[:- 4 ]+ '.jpg'
5360 return imagename ,shape , bboxes , labels_text ,labels , difficult , truncated
5461
55- def CreateYearsDatasets (dir ,rate = 0.95 ):
62+ def CreateYearsDatasets (dir ,year = None , save_path = None , rate = 0.95 ):
5663 years = os .listdir (dir )
57- if os .path .exists (os .path .join (dir ,"train.txt" )):
58- os .remove (os .path .join (dir ,"train.txt" ))
59- if os .path .exists (os .path .join (dir ,"test.txt" )):
60- os .remove (os .path .join (dir ,"test.txt" ))
61- progressBar1 = ProgressBar (len (years ))
62- with open (os .path .join (dir ,"train.txt" ),"w" ) as f1 :
64+ if os .path .exists (os .path .join (save_path ,"train.txt" )):
65+ os .remove (os .path .join (save_path ,"train.txt" ))
66+ if os .path .exists (os .path .join (save_path ,"test.txt" )):
67+ os .remove (os .path .join (save_path ,"test.txt" ))
68+ if year is None :
69+ progressBar1 = ProgressBar (len (years ))
70+ else :
71+ progressBar1 = ProgressBar (1 )
72+ if os .path .exists (save_path ):
73+ pass
74+ else :
75+ os .makedirs (save_path )
76+ if year is None :
6377 for year in years :
6478 if os .path .isdir (os .path .join (dir , year )):
65- if os .path .exists (os .path .join (dir ,year ,DIRECTORY_IMAGES )) and os .path .exists (os .path .join (dir ,year ,DIRECTORY_ANNOTATIONS )):
66- CreateVOCDataset (os .path .join (dir ,year ),year ,rate )
67- with open (os .path .join (dir ,year ,"ImageSets" ,"Main" ,"train.txt" )) as f2 :
68- for content in f2 .read ().split ("\n " )[:- 1 ]:
69- f1 .write (content + "\n " )
79+ if os .path .exists (os .path .join (dir , year , DIRECTORY_IMAGES )) and os .path .exists (
80+ os .path .join (dir , year , DIRECTORY_ANNOTATIONS )):
81+ CreateVOCDataset (os .path .join (dir , year ), year , save_path , rate )
7082 progressBar1 .update ()
71-
72- progressbar2 = ProgressBar (len (years ))
73- with open (os .path .join (dir , "test.txt" ), "w" ) as f1 :
83+ else :
84+ if os .path .isdir (os .path .join (dir , year )):
85+ if os .path .exists (os .path .join (dir , year , DIRECTORY_IMAGES )) and os .path .exists (
86+ os .path .join (dir , year , DIRECTORY_ANNOTATIONS )):
87+ CreateVOCDataset (os .path .join (dir , year ), year , save_path , rate )
88+
89+ years = os .listdir (save_path )
90+ with open (os .path .join (save_path , "train.txt" ), "w" ) as f1 :
91+ progressbar2 = ProgressBar (len (years ))
7492 for year in years :
7593 if os .path .isdir (os .path .join (dir , year )):
76- if os .path .exists (os .path .join (dir ,year ,DIRECTORY_IMAGES )) and os .path .exists (os .path .join (dir ,year ,DIRECTORY_ANNOTATIONS )):
77- CreateVOCDataset (os .path .join (dir ,year ),year ,rate )
78- with open (os .path .join (dir , year , "ImageSets" , "Main" , "test.txt" )) as f2 :
94+ with open (os .path .join (save_path , year , "ImageSets" , "Main" , "train.txt" )) as f2 :
7995 for content in f2 .read ().split ("\n " )[:- 1 ]:
96+ f1 .write (content + "\n " )
97+ progressbar2 .update ()
8098
99+ with open (os .path .join (save_path , "test.txt" ), "w" ) as f1 :
100+ progressbar2 = ProgressBar (len (years ))
101+ for year in years :
102+ if os .path .isdir (os .path .join (dir , year )):
103+ with open (os .path .join (save_path , year , "ImageSets" , "Main" , "test.txt" )) as f2 :
104+ for content in f2 .read ().split ("\n " )[:- 1 ]:
81105 f1 .write (content + "\n " )
82- progressbar2 .update ()
83- CreateLabelList (dir )
106+ progressbar2 .update ()
107+ CreateLabelList (save_path )
84108
85109
86110##制作VOC数据集
87- def CreateVOCDataset (dir , datasetname ,rate = 0.95 ):
111+ def CreateVOCDataset (dir , datasetname ,save_path = None , rate = 0.95 ):
88112 """
89113
90114 :param dir:
91115 :param datasetname:
92116 :param rate:
93117 :return:
94118 """
95- root_path = dir
119+ root_path = os . path . join ( save_path , datasetname )
96120 dataset_name = datasetname
97121 Annotations = DIRECTORY_ANNOTATIONS
98122 JPEGImages = DIRECTORY_IMAGES
@@ -103,8 +127,7 @@ def CreateVOCDataset(dir, datasetname,rate=0.95):
103127 shutil .rmtree (os .path .join (root_path , "ImageSets" , "Main" ))
104128 os .makedirs (os .path .join (root_path , "ImageSets" , "Main" ))
105129 Main_path = os .path .join (root_path , "ImageSets" , "Main" )
106- image_files = os .listdir (os .path .join (root_path , JPEGImages ))
107-
130+ image_files = os .listdir (os .path .join (dir , JPEGImages ))
108131 train_image_files = random .sample (image_files , int (len (image_files ) * rate ))
109132 test_image_files = [file for file in image_files if file not in train_image_files ]
110133
@@ -114,10 +137,14 @@ def CreateVOCDataset(dir, datasetname,rate=0.95):
114137 image_file = dataset_name + "/" + JPEGImages + "/" + train_image_file
115138 xml_file = dataset_name + "/" + Annotations + "/" + train_image_file [:- 4 ] + ".xml"
116139 filename = train_image_file [:- 4 ]
140+ save_image_path = CreateSavePath (os .path .join (root_path ,DIRECTORY_IMAGES ))
141+ save_xml_path = CreateSavePath (os .path .join (root_path ,DIRECTORY_ANNOTATIONS ))
117142 with open (os .path .join (dir ,JPEGImages ,train_image_file ),"rb" ) as f2 :
118143 if len (f2 .read ()) == 0 :
119144 pass
120145 else :
146+ shutil .copy (os .path .join (dir , JPEGImages , train_image_file ), save_image_path )
147+ shutil .copy (os .path .join (dir , DIRECTORY_ANNOTATIONS , train_image_file [:- 4 ] + ".xml" ), save_xml_path )
121148 f .write (filename + "\n " )
122149 # f.write(image_file + " " + xml_file + "\n")
123150
@@ -127,10 +154,14 @@ def CreateVOCDataset(dir, datasetname,rate=0.95):
127154 image_file = dataset_name + "/" + JPEGImages + "/" + test_image_file
128155 xml_file = dataset_name + "/" + Annotations + "/" + test_image_file [:- 4 ] + ".xml"
129156 filename = test_image_file [:- 4 ]
157+ save_image_path = CreateSavePath (os .path .join (root_path , DIRECTORY_IMAGES ))
158+ save_xml_path = CreateSavePath (os .path .join (root_path , DIRECTORY_ANNOTATIONS ))
130159 with open (os .path .join (dir ,JPEGImages ,test_image_file ),"rb" ) as f2 :
131160 if len (f2 .read ()) == 0 :
132161 pass
133162 else :
163+ shutil .copy (os .path .join (dir , JPEGImages , train_image_file ), save_image_path )
164+ shutil .copy (os .path .join (dir , DIRECTORY_ANNOTATIONS , train_image_file [:- 4 ] + ".xml" ), save_xml_path )
134165 f .write (filename + "\n " )
135166 # f.write(image_file + " " + xml_file + "\n")
136167
@@ -179,10 +210,10 @@ def CreateLabelList(dir):
179210 label_list .append (label )
180211 progressbar .update ()
181212 progressBar2 = ProgressBar (len (label_list ))
182- for label_name in label_list :
183- with open ( os . path . join ( dir , "label_list.txt" ), "a" ) as f :
184- f .write (label_name + "\n " )
185- progressBar2 .update ()
213+ with open ( os . path . join ( dir , " label_list.txt" ), "wb" ) as f :
214+ for label_name in label_list :
215+ f .write (( label_name + "\n " ). encode ( "utf-8" ) )
216+ progressBar2 .update ()
186217 else :
187218 print ("请先制作数据集" )
188219
0 commit comments