Skip to content

Commit b905520

Browse files
committed
* 优化目标检测数据集制作方法
1 parent c4b8833 commit b905520

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

dataset_tools/jade_create_object_dection_datasets.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import shutil
1313
import random
1414
import xml.etree.ElementTree as ET
15+
from dataset_tools.jade_voc_datasets import GetXmlClassesNames
1516

1617
def CreateSavePath(save_path):
1718
if os.path.exists(save_path):
@@ -143,9 +144,13 @@ def CreateVOCDataset(dir, datasetname,save_path=None,rate=0.95):
143144
if len(f2.read()) == 0:
144145
pass
145146
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)
148-
f.write(filename + "\n")
147+
class_name_list = GetXmlClassesNames(os.path.join(dir, DIRECTORY_ANNOTATIONS, train_image_file[:-4] + ".xml"))
148+
if len(class_name_list) > 0:
149+
shutil.copy(os.path.join(dir, JPEGImages, train_image_file), save_image_path)
150+
shutil.copy(os.path.join(dir, DIRECTORY_ANNOTATIONS, train_image_file[:-4] + ".xml"), save_xml_path)
151+
f.write(filename + "\n")
152+
else:
153+
print(os.path.join(dir, DIRECTORY_ANNOTATIONS, train_image_file[:-4] + ".xml"))
149154
# f.write(image_file + " " + xml_file + "\n")
150155

151156
for test_image_file in test_image_files:
@@ -160,10 +165,14 @@ def CreateVOCDataset(dir, datasetname,save_path=None,rate=0.95):
160165
if len(f2.read()) == 0:
161166
pass
162167
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)
165-
f.write(filename + "\n")
166-
# f.write(image_file + " " + xml_file + "\n")
168+
class_name_list = GetXmlClassesNames(
169+
os.path.join(dir, DIRECTORY_ANNOTATIONS, test_image_file[:-4] + ".xml"))
170+
if len(class_name_list) > 0:
171+
shutil.copy(os.path.join(dir, JPEGImages, test_image_file), save_image_path)
172+
shutil.copy(os.path.join(dir, DIRECTORY_ANNOTATIONS, test_image_file[:-4] + ".xml"), save_xml_path)
173+
f.write(filename + "\n")
174+
else:
175+
print(os.path.join(dir, DIRECTORY_ANNOTATIONS, test_image_file[:-4] + ".xml"))
167176

168177
for train_image_file in train_image_files:
169178
with open(os.path.join(Main_path, "train.txt"), "a") as f:
@@ -175,7 +184,15 @@ def CreateVOCDataset(dir, datasetname,save_path=None,rate=0.95):
175184
if len(f2.read()) == 0:
176185
pass
177186
else:
178-
f.write(image_file + " " + xml_file + "\n")
187+
class_name_list = GetXmlClassesNames(
188+
os.path.join(dir, DIRECTORY_ANNOTATIONS, train_image_file[:-4] + ".xml"))
189+
if len(class_name_list) > 0:
190+
shutil.copy(os.path.join(dir, JPEGImages, train_image_file), save_image_path)
191+
shutil.copy(os.path.join(dir, DIRECTORY_ANNOTATIONS, train_image_file[:-4] + ".xml"), save_xml_path)
192+
f.write(image_file + " " + xml_file + "\n")
193+
else:
194+
print(os.path.join(dir, DIRECTORY_ANNOTATIONS, train_image_file[:-4] + ".xml"))
195+
179196
# f.write(filename + "\n")
180197

181198

@@ -189,7 +206,16 @@ def CreateVOCDataset(dir, datasetname,save_path=None,rate=0.95):
189206
if len(f2.read()) == 0:
190207
pass
191208
else:
192-
f.write(image_file + " " + xml_file + "\n")
209+
class_name_list = GetXmlClassesNames(
210+
os.path.join(dir, DIRECTORY_ANNOTATIONS, test_image_file[:-4] + ".xml"))
211+
if len(class_name_list) > 0:
212+
shutil.copy(os.path.join(dir, JPEGImages, test_image_file), save_image_path)
213+
shutil.copy(os.path.join(dir, DIRECTORY_ANNOTATIONS, test_image_file[:-4] + ".xml"),
214+
save_xml_path)
215+
f.write(image_file + " " + xml_file + "\n")
216+
else:
217+
print(os.path.join(dir, DIRECTORY_ANNOTATIONS, test_image_file[:-4] + ".xml"))
218+
193219
# f.write(filename + "\n")
194220

195221
def CreateLabelList(dir):

dataset_tools/jade_voc_datasets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from xml.dom import minidom
1212
from dataset_tools import *
1313
from opencv_tools import *
14-
14+
import xml.etree.ElementTree as ET
1515

1616
def GetXmlClassesNames(xml_path):
1717
classnames = []

0 commit comments

Comments
 (0)