-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_convert_widerface.py
207 lines (180 loc) · 5.95 KB
/
test_convert_widerface.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'''
lanhuage: python
Descripttion:
version: beta
Author: xiaoshuyui
Date: 2020-11-16 08:34:52
LastEditors: xiaoshuyui
LastEditTime: 2020-11-26 16:13:45
'''
import os
import sys
sys.path.append('..')
from multiprocessing import Pool
from skimage import io
from convertmask import __CPUS__
from convertmask.utils.img2xml.processor_multi_object import img2xml_multiobj
from convertmask.utils.methods.logger import logger
from tqdm import tqdm
filepath = 'D:\\facedetect\\dump\\wider_face_split\\wider_face_train_bbx_gt.txt'
# filepath = 'D:\\facedetect\\dump\\wider_face_split\\wider_face_val_bbx_gt.txt'
BASE_DIR = os.path.abspath(os.path.dirname(os.getcwd()))
def writeXml(imgname, objlist, savepath, folder, 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)
occlusion = int(tmp[-2])
blur = int(tmp[4])
if blur > 0:
name = 'blur'
elif occlusion > 0:
name = 'occlusion'
else:
name = 'face'
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 readTxt(filepath: str, savepath='', mProcess=False):
with open(filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
# test
# for (index, d) in enumerate(lines):
# print(str(index) + d)
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)
occlusion = int(tmp[-2])
blur = int(tmp[4])
if blur > 1:
name = 'blur'
elif occlusion > 0:
name = 'occlusion'
else:
name = 'face'
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 mProcess:
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)
occlusion = int(tmp[-2])
blur = int(tmp[4])
if blur > 0:
name = 'blur'
elif occlusion > 0:
name = 'occlusion'
else:
name = 'face'
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)
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(
writeXml, (imgname, objlist, savepath, folder))
pool_list.append(resultsPool)
for pr in tqdm(pool_list):
re_list = pr.get()
if __name__ == "__main__":
readTxt(filepath,
savepath='D:\\facedetect\\dump\\xmls3face_\\',
mProcess=True)