forked from zzmcdc/face-eval
-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
292 lines (265 loc) · 8.72 KB
/
util.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import cPickle
import numpy
import pylab
from scipy.io.matlab import savemat, loadmat
def myimread(imgname, flip=False, resize=None):
"""
read an image
"""
img = None
if imgname.split(".")[-1] == "png":
img = pylab.imread(imgname)
else:
img = numpy.ascontiguousarray(pylab.imread(imgname)[::-1])
if flip:
img = numpy.ascontiguousarray(img[:, ::-1, :])
if resize != None:
from scipy.misc import imresize
img = imresize(img, resize)
return img
def save(filename, obj, prt=2):
"""
save any python object
"""
fd = open(filename, "w")
cPickle.dump(obj, fd, prt)
fd.close()
# def savemat(filename,dic):
# """
# save an array in matlab format
# """
# import scipy.io.matlab
# fd=open(filename,"w")
# scipy.io.matlab.savemat(filename,dic)
# fd.close()
def load(filename):
"""
load any python object
"""
fd = open(filename, "r")
aux = cPickle.load(fd)
fd.close()
return aux
# def loadmat(filename):
# """
# load an array in matlab format
# """
# import scipy.io.matlab
# aux = scipy.io.matlab.loadmat(filename)
# fd.close()
# return aux
def drawModel(mfeat, mode="black", parts=True):
"""
draw the HOG weight of an object model
"""
col = ["r", "g", "b"]
import drawHOG
lev = len(mfeat)
if mfeat[0].shape[0] > mfeat[0].shape[1]:
sy = 1
sx = lev
else:
sy = lev
sx = 1
for l in range(lev):
pylab.subplot(sy, sx, l + 1)
if mode == "white":
drawHOG9(mfeat[l])
elif mode == "black":
img = drawHOG.drawHOG(mfeat[l])
pylab.axis("off")
pylab.imshow(img, cmap=pylab.cm.gray, interpolation="nearest")
if parts == True:
for x in range(0, 2 ** l):
for y in range(0, 2 ** l):
boxHOG(mfeat[0].shape[1] * x, mfeat[0].shape[0] * y,
mfeat[0].shape[1], mfeat[0].shape[0], col[l], 5 - l)
def drawDeform(dfeat, mindef=0.001):
"""
draw the deformation weight of an object model
"""
from matplotlib.patches import Ellipse
lev = len(dfeat)
if 1:
sy = 1
sx = lev
else:
sy = lev
sx = 1
pylab.subplot(sy, sx, 1)
x1 = -0.5
x2 = 0.5
y1 = -0.5
y2 = 0.5
pylab.fill([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1],
"b", alpha=0.15, edgecolor="b", lw=1)
pylab.fill([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1],
"r", alpha=0.15, edgecolor="r", lw=1)
wh = numpy.exp(-mindef / dfeat[0][0, 0, 0]) / numpy.exp(1)
hh = numpy.exp(-mindef / dfeat[0][0, 0, 1]) / numpy.exp(1)
e = Ellipse(xy=[0, 0], width=wh, height=hh, alpha=0.35)
col = numpy.array([wh * hh] * 3).clip(0, 1)
col[0] = 0
e.set_facecolor(col)
pylab.axis("off")
pylab.gca().add_artist(e)
pylab.gca().set_ylim(-0.5, 0.5)
pylab.gca().set_xlim(-0.5, 0.5)
for l in range(1, lev):
pylab.subplot(sy, sx, l + 1)
for ry in range(2 ** (l - 1)):
for rx in range(2 ** (l - 1)):
drawDef(dfeat[l][ry * 2:(ry + 1) * 2, rx * 2:(rx + 1)
* 2, 2:] * 4 ** l, 4 * ry, 4 * rx, distr="child")
drawDef(dfeat[l][ry * 2:(ry + 1) * 2, rx * 2:(rx + 1) * 2, :2] *
4 ** l, ry * 2 ** (l), rx * 2 ** (l), mindef=mindef, distr="father")
# pylab.gca().set_ylim(-0.5,(2.6)**l)
pylab.axis("off")
pylab.gca().set_ylim((2.6) ** l, -0.5)
pylab.gca().set_xlim(-0.5, (2.6) ** l)
def drawDef(dfeat, dy, dx, mindef=0.001, distr="father"):
"""
auxiliary funtion to draw recursive levels of deformation
"""
from matplotlib.patches import Ellipse
pylab.ioff()
if distr == "father":
py = [0, 0, 2, 2]
px = [0, 2, 0, 2]
if distr == "child":
py = [0, 1, 1, 2]
px = [1, 2, 0, 1]
ordy = [0, 0, 1, 1]
ordx = [0, 1, 0, 1]
x1 = -0.5 + dx
x2 = 2.5 + dx
y1 = -0.5 + dy
y2 = 2.5 + dy
if distr == "father":
pylab.fill([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1],
"r", alpha=0.15, edgecolor="b", lw=1)
for l in range(len(py)):
aux = dfeat[ordy[l], ordx[l], :].clip(-1, -mindef)
wh = numpy.exp(-mindef / aux[0]) / numpy.exp(1)
hh = numpy.exp(-mindef / aux[1]) / numpy.exp(1)
e = Ellipse(
xy=[(px[l] + dx), (py[l] + dy)], width=wh, height=hh, alpha=0.35)
x1 = -0.75 + dx + px[l]
x2 = 0.75 + dx + px[l]
y1 = -0.76 + dy + py[l]
y2 = 0.75 + dy + py[l]
col = numpy.array([wh * hh] * 3).clip(0, 1)
if distr == "father":
col[0] = 0
e.set_facecolor(col)
pylab.gca().add_artist(e)
if distr == "father":
pylab.fill([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1],
"b", alpha=0.15, edgecolor="b", lw=1)
def overlap(rect1, rect2):
"""
Calculate the overlap between two boxes
"""
dy1 = abs(rect1[0] - rect1[2]) + 1
dx1 = abs(rect1[1] - rect1[3]) + 1
dy2 = abs(rect2[0] - rect2[2]) + 1
dx2 = abs(rect2[1] - rect2[3]) + 1
a1 = dx1 * dy1
a2 = dx2 * dy2
ia = 0
if rect1[2] > rect2[0] and rect2[2] > rect1[0] and rect1[3] > rect2[1] and rect2[3] > rect1[1]:
xx1 = max(rect1[1], rect2[1])
yy1 = max(rect1[0], rect2[0])
xx2 = min(rect1[3], rect2[3])
yy2 = min(rect1[2], rect2[2])
ia = (xx2 - xx1 + 1) * (yy2 - yy1 + 1)
return ia / float(a1 + a2 - ia)
def inclusion(rect1, rect2):
"""
Calculate the intersection percentage between two rectangles
Note that it is not anymore symmetric
"""
dy1 = abs(rect1[0] - rect1[2]) + 1
dx1 = abs(rect1[1] - rect1[3]) + 1
dy2 = abs(rect2[0] - rect2[2]) + 1
dx2 = abs(rect2[1] - rect2[3]) + 1
a1 = dx1 * dy1
a2 = dx2 * dy2
ia = 0
if rect1[2] > rect2[0] and rect2[2] > rect1[0] and rect1[3] > rect2[1] and rect2[3] > rect1[1]:
xx1 = max(rect1[1], rect2[1])
yy1 = max(rect1[0], rect2[0])
xx2 = min(rect1[3], rect2[3])
yy2 = min(rect1[2], rect2[2])
ia = (xx2 - xx1 + 1) * (yy2 - yy1 + 1)
return ia / float(a1)
def myinclusion(rect1, rect2):
"""
Calculate the intersection percentage between two rectangles
Note that it is not anymore symmetric
"""
dy1 = abs(rect1[0] - rect1[2]) + 1
dx1 = abs(rect1[1] - rect1[3]) + 1
dy2 = abs(rect2[0] - rect2[2]) + 1
dx2 = abs(rect2[1] - rect2[3]) + 1
cy1 = (rect1[0] - rect1[2]) / 2.0
cx1 = (rect1[1] - rect1[3]) / 2.0
cy2 = (rect2[0] - rect2[2]) / 2.0
cx2 = (rect2[1] - rect2[3]) / 2.0
dc = numpy.sqrt(
((cy1 - cy2) / float(dy2)) ** 2 + ((cx1 - cx2) / float(dx2)) ** 2)
# print dc
a1 = dx1 * dy1
a2 = dx2 * dy2
if dx1 > dy1: # xgt
a21 = max(dx2, dx1) * dy1
else: # ygt
a21 = max(dy1, dy2) * dx1
ia = 0
if rect1[2] > rect2[0] and rect2[2] > rect1[0] and rect1[3] > rect2[1] and rect2[3] > rect1[1]:
xx1 = max(rect1[1], rect2[1])
yy1 = max(rect1[0], rect2[0])
xx2 = min(rect1[3], rect2[3])
yy2 = min(rect1[2], rect2[2])
ia = (xx2 - xx1 + 1) * (yy2 - yy1 + 1)
# print dy1,dx1,dy2,dx2
# print ia
# print a21
return ia / float(a21) - dc
def overlapx(rect1, rect2, pixels=20):
"""
Calculate the intersection percentage between two rectangles
Note that it is not anymore symmetric
"""
dy1 = abs(rect1[0] - rect1[2]) + 1
dx1 = abs(rect1[1] - rect1[3]) + 1
dy2 = abs(rect2[0] - rect2[2]) + 1
dx2 = abs(rect2[1] - rect2[3]) + 1
cy1 = (rect1[0] - rect1[2]) / 2.0
cx1 = (rect1[1] - rect1[3]) / 2.0
cy2 = (rect2[0] - rect2[2]) / 2.0
cx2 = (rect2[1] - rect2[3]) / 2.0
dc = max(abs(cy1 - cy2) / float(pixels * 2),
abs(cx1 - cx2) / float(pixels * 2))
return 1 - dc
def boxHOG(px, py, dx, dy, col, lw):
"""
bbox one the HOG weights
"""
k = 1
d = 15
pylab.plot([px * d + 0 - k, px * d + 0 - k],
[py * d + 0 - k, py * d + dy * d - k], col, lw=lw)
pylab.plot([px * d + 0 - k, px * d + dx * d - k],
[py * d + 0 - k, py * d + 0 - k], col, lw=lw)
pylab.plot([px * d + dx * 15 - k, px * d + dx * d - k],
[py * d + 0 - k, py * d + dy * d - k], col, lw=lw)
pylab.plot([px * d + 0 - k, px * d + dx * d - k],
[py * d + dy * d - k, py * d + dy * d - k], col, lw=lw)
pylab.axis("image")
def box(p1y, p1x, p2y, p2x, col='b', lw=1):
"""
plot a bbox with the given coordinates
"""
pylab.plot(
[p1x, p1x, p2x, p2x, p1x], [p1y, p2y, p2y, p1y, p1y], col, lw=lw)