-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwinderface_convert.py
170 lines (147 loc) · 5.09 KB
/
winderface_convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
from multiprocessing import Pool
from skimage import io
from mltools import __CPUS__
from mltools.src.utils.img2xml.multi_object_process import img2xml_multiobj
from mltools.src.log.logger import logger
from tqdm import tqdm
BASE_DIR = os.path.abspath(os.path.dirname(os.getcwd()))
def write_xml(imgname, objlist, savepath, folder, name, oriImgPath=""):
# imgname = lines[start].split('/')[-1]
xmlname = imgname.replace(".jpg", ".xml").replace("\n", "")
tmpPath = savepath + xmlname
path = imgname
if oriImgPath == "":
logger.warning("Origin image needed!")
width = 0
height = 0
else:
oriImg = io.imread(oriImgPath + os.sep + imgname)
width = oriImg.shape[1]
height = oriImg.shape[0]
del oriImg
objs = []
# objlist = lines[start + 2:end]
for ob in objlist:
# print(ob)
if len(ob) > 20:
tmp = ob.split(" ")
xmin = tmp[0]
ymin = tmp[1]
w = tmp[2]
h = tmp[3]
xmax = int(xmin) + int(w)
ymax = int(ymin) + int(h)
obj = dict()
obj["name"] = name
obj["diffcult"] = 0
bndbox = dict()
bndbox["xmin"] = xmin
bndbox["ymin"] = ymin
bndbox["xmax"] = xmax
bndbox["ymax"] = ymax
obj["bndbox"] = bndbox
objs.append(obj)
img2xml_multiobj(tmpPath, tmpPath, folder, imgname, path, width, height, objs)
# gc.collect()
def convert_widerface(filepath: str, savepath="", parallel=False):
"""Annotation of widerface is like
0--Parade/0_Parade_marchingband_1_849.jpg
1
449 330 122 149 0 0 0 0 0 0
The format of txt ground truth. \n
File name \n
Number of bounding box \n
x1, y1, w, h, blur, expression, illumination, invalid, occlusion, pose
which is not suitable for labelImg. Besides, convertion may take a really long time using single process.
"""
with open(filepath, "r", encoding="utf-8") as f:
lines = f.readlines()
ids = list(index for (index, d) in enumerate(lines) if d.endswith(".jpg\n"))
# print(len(ids))
if savepath == "":
savepath = BASE_DIR + os.sep + "xmls_"
if not os.path.isdir(savepath):
os.mkdir(savepath)
folder = "face"
name = "face"
lastImg = lines[ids[-1]]
imgname = lastImg.split("/")[-1].replace("\n", "")
xmlname = imgname.replace(".jpg", ".xml")
tmpPath = savepath + xmlname
path = imgname
width = 0
height = 0
objs = []
objlist = lines[ids[-1] + 2 :]
for ob in objlist:
if len(ob) > 20:
tmp = ob.split(" ")
xmin = tmp[0]
ymin = tmp[1]
w = tmp[2]
h = tmp[3]
xmax = int(xmin) + int(w)
ymax = int(ymin) + int(h)
obj = dict()
obj["name"] = name
obj["diffcult"] = 0
bndbox = dict()
bndbox["xmin"] = xmin
bndbox["ymin"] = ymin
bndbox["xmax"] = xmax
bndbox["ymax"] = ymax
obj["bndbox"] = bndbox
objs.append(obj)
img2xml_multiobj(tmpPath, tmpPath, folder, imgname, path, width, height, objs)
if not parallel:
for i in tqdm(range(len(ids) - 1)):
start = ids[i]
end = ids[i + 1]
imgname = lines[start].split("/")[-1]
xmlname = imgname.replace(".jpg", ".xml").replace("\n", "")
tmpPath = savepath + xmlname
path = imgname
width = 0 # for test
height = 0 # for test
objs = []
objlist = lines[start + 2 : end]
for ob in objlist:
# print(ob)
if len(ob) > 20:
tmp = ob.split(" ")
xmin = tmp[0]
ymin = tmp[1]
w = tmp[2]
h = tmp[3]
xmax = int(xmin) + int(w)
ymax = int(ymin) + int(h)
obj = dict()
obj["name"] = name
obj["diffcult"] = 0
bndbox = dict()
bndbox["xmin"] = xmin
bndbox["ymin"] = ymin
bndbox["xmax"] = xmax
bndbox["ymax"] = ymax
obj["bndbox"] = bndbox
objs.append(obj)
img2xml_multiobj(
tmpPath, tmpPath, folder, imgname, path, width, height, objs
)
logger.info("Done! See {}.".format(savepath))
else:
pool = Pool(__CPUS__ - 1)
pool_list = []
for i in tqdm(range(len(ids) - 1)):
start = ids[i]
end = ids[i + 1]
imgname = lines[start].split("/")[-1]
objlist = lines[start + 2 : end]
resultsPool = pool.apply_async(
write_xml, (imgname, objlist, savepath, folder, name)
)
pool_list.append(resultsPool)
for pr in tqdm(pool_list):
re_list = pr.get()
logger.info("Done! See {}.".format(savepath))