-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwider2pascal.py
More file actions
210 lines (161 loc) · 5.81 KB
/
wider2pascal.py
File metadata and controls
210 lines (161 loc) · 5.81 KB
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
208
209
210
'''
WIDER Faces to Pascal VOC:
Adapted from the Matlab version (https://github.com/albanie/wider2pascal)
'''
import os
import scipy.io as sio
import xml.etree.cElementTree as ET
import numpy as np
import cv2
from shutil import copyfile
def generateXML(imgDir, imgName, bboxList, targetName):
# info
objectName = "face"
imgFolder = "WIDER"
databaseName = "WIDER FACE"
annotationType = "WIDER"
ownerName = " Multimedia Laboratory, Department of Information Engineering, The Chinese University of Hong Kong (http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/)"
# annotation
anno = ET.Element("annotation")
# folder
folder = ET.SubElement(anno, "folder")
folder.text = imgFolder
# filename
filename = ET.SubElement(anno,"filename")
filename.text = imgName
# source
source = ET.SubElement(anno,"source")
database = ET.SubElement(source, "database")
database.text = databaseName
db_anno = ET.SubElement(source, "annotation")
db_anno.text = annotationType
# size
size = ET.SubElement(anno, "size")
# get metadata from image
# shape = (rows, cols, channels)
immeta = cv2.imread(imgDir)
# size->width
width = ET.SubElement(size, "width")
width.text = str(immeta.shape[1])
# size->height
height = ET.SubElement(size, "height")
height.text = str(immeta.shape[0])
# size->depth
depth = ET.SubElement(size, "depth")
depth.text = str(immeta.shape[2])
# owner
owner = ET.SubElement(anno, "owner")
name = ET.SubElement(owner, "name")
name.text = ownerName
# bounding box list
if bboxList.ndim == 1:
xmin = bboxList[0]
ymin = bboxList[1]
xmax = bboxList[0] + bboxList[2]
ymax = bboxList[1] + bboxList[3]
# object
obj = ET.SubElement(anno, "object")
name = ET.SubElement(obj,"name")
name.text = objectName
bbox = ET.SubElement(obj,"bndbox")
elem = ET.SubElement(bbox, "xmin")
elem.text = str(int(round(xmin)))
elem = ET.SubElement(bbox, "ymin")
elem.text = str(int(round(ymin)))
elem = ET.SubElement(bbox, "xmax")
elem.text = str(int(round(xmax)))
elem = ET.SubElement(bbox, "ymax")
elem.text = str(int(round(ymax)))
else:
for i in range(len(bboxList)):
xmin = bboxList[i][0]
ymin = bboxList[i][1]
xmax = bboxList[i][0] + bboxList[i][2]
ymax = bboxList[i][1] + bboxList[i][3]
# object
obj = ET.SubElement(anno, "object")
name = ET.SubElement(obj,"name")
name.text = objectName
bbox = ET.SubElement(obj,"bndbox")
elem = ET.SubElement(bbox, "xmin")
elem.text = str(int(round(xmin)))
elem = ET.SubElement(bbox, "ymin")
elem.text = str(int(round(ymin)))
elem = ET.SubElement(bbox, "xmax")
elem.text = str(int(round(xmax)))
elem = ET.SubElement(bbox, "ymax")
elem.text = str(int(round(ymax)))
tree = ET.ElementTree(anno)
tree.write(targetName)
def generateAnnotations(widerRootDir, annotationsDir, mode='train'):
# check if destination exists
if not os.path.exists(annotationsDir):
os.makedirs(annotationsDir)
# read matlab file
filepath = os.path.join(widerRootDir, 'wider_face_split')
filename = os.path.join(filepath,'wider_face_'+mode+'.mat')
data = sio.loadmat(filename, squeeze_me=True)
for eventId in range(len(data['event_list'])):
files = data['file_list'][eventId]
bboxList= data['face_bbx_list'][eventId]
for j in range(len(files)):
imgName = str(files[j])+'.jpg'
imgDir = os.path.join(widerRootDir, 'WIDER_'+str(mode),'images',data['event_list'][eventId], imgName)
annotationFilename = os.path.join(annotationsDir,str(files[j])+'.xml')
generateXML(imgDir, imgName, bboxList[j], annotationFilename)
def copyImages(widerRootDir, jpegImagesDir, mode='train'):
# check if destination exists
if not os.path.exists(jpegImagesDir):
os.makedirs(jpegImagesDir)
# read matlab file
filepath = os.path.join(widerRootDir, 'wider_face_split')
filename = os.path.join(filepath,'wider_face_'+mode+'.mat')
data = sio.loadmat(filename, squeeze_me=True)
for eventId in range(len(data['event_list'])):
files = data['file_list'][eventId]
for j in range(len(files)):
imgName = str(files[j])+'.jpg'
imgPaths = os.path.join(widerRootDir,'WIDER_'+mode,'images',data['event_list'][eventId], imgName)
dst = os.path.join(jpegImagesDir,imgName)
copyfile(imgPaths, dst);
def generateImageSets(widerRootDir, imageSetsDir, mode='train'):
# check if destination exists
if not os.path.exists(imageSetsDir):
os.makedirs(imageSetsDir)
# read matlab file
filepath = os.path.join(widerRootDir, 'wider_face_split')
filename = os.path.join(filepath,'wider_face_'+mode+'.mat')
data = sio.loadmat(filename, squeeze_me=True)
targetPath = os.path.join(imageSetsDir, str(mode)+'.txt')
fileId = open(targetPath,'w')
for eventId in range(len(data['file_list'])):
for file in data['file_list'][eventId]:
fileId.write(file+'\n')
fileId.close()
def convertWider2Pascal(SOURCE, TARGET):
annotationsDir = os.path.join(TARGET,'WIDER','Annotations')
jpegImagesDir = os.path.join(TARGET,'WIDER','JPEGImages')
imageSetsDir = os.path.join(TARGET,'WIDER','ImageSets')
print 'Generating Annotations: TRAIN'
generateAnnotations(SOURCE, annotationsDir)
print 'Generating Annotations: TEST'
generateAnnotations(SOURCE, annotationsDir, 'val')
print 'Copying Images : TRAIN'
copyImages(SOURCE, jpegImagesDir)
print 'Copying Images : TEST'
copyImages(SOURCE, jpegImagesDir, 'val')
print 'Generating ImageSets: TRAIN'
generateImageSets(SOURCE, imageSetsDir)
print 'Generating ImageSets: TEST'
generateImageSets(SOURCE, imageSetsDir, 'val')
print 'done'
if __name__ == '__main__':
'''
Assumes the wider dataset in ./dataset/
Generates resultant dataset in ./dataset/wider_pascal/
'''
ROOT = os.path.dirname(os.path.realpath(__file__))
WIDER = os.path.join(ROOT, 'dataset/wider')
TARGET = os.path.join(ROOT,'dataset/wider_pascal')
convertWider2Pascal(WIDER, TARGET)
print 'PASCAL VOC format WIDER dataset at',TARGET