Skip to content

Commit c4b8833

Browse files
committed
* 目标检测数据集支持单日期格式新增,支持所有日期格式新增
1 parent d3cc63e commit c4b8833

File tree

3 files changed

+79
-31
lines changed

3 files changed

+79
-31
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
### 更新日志
22

3-
#### DatasetToolsV1.1.5 - 2023-10-13
4-
* 支持新增数据集
3+
#### DatasetToolsV1.1.6 - 2024-02-26
4+
* 目标检测数据集支持单日期格式新增,支持所有日期格式新增
55
---
66

77
<details onclose>
88
<summary>查看更多更新日志</summary>
99

10+
11+
#### DatasetToolsV1.1.5 - 2023-10-13
12+
* 支持新增数据集
13+
---
14+
1015
#### DatasetToolsV1.1.4 - 2023-09-27
1116
* 降低箱型校验标准
1217
---

dataset_tools/jade_create_object_dection_datasets.py

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
# @Desc : 制作目标检测数据集
99
from dataset_tools import *
1010
import os
11-
from jade import ProgressBar,GetLastDir
11+
from jade import ProgressBar,GetLastDir,CreateSavePath
1212
import shutil
1313
import random
1414
import 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+
1623
def 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

test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @File : test.py
4+
# @Author : jade
5+
# @Date : 2024/2/27 9:16
6+
# @Email : jadehh@1ive.com
7+
# @Software : Samples
8+
# @Desc :
9+
from dataset_tools.jade_create_object_dection_datasets import *
10+
11+
if __name__ == '__main__':
12+
CreateYearsDatasets("F:\数据集\VOC数据集\箱门检测数据集\ContainVOC",year=None,save_path="E:\数据集\VOC数据集\箱门检测数据集\ContainVOC",rate=0.95)

0 commit comments

Comments
 (0)