-
Notifications
You must be signed in to change notification settings - Fork 99
/
librect.py
190 lines (142 loc) · 4.22 KB
/
librect.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# pylint: disable=C0103
import numpy as np
import PIL.Image # https://github.com/python-pillow/Pillow
def largestRect(rects):
u"""retturn largest rect in rects
rects: list of rect
"""
if len(rects) < 2:
return rects
largest = rects[0]
for i in range(1, len(rects)):
if rects[i][2] > largest[2]:
largest = rects[i]
return largest
def overlapRange(lim1, lim2):
"""return overlapped lim
lim1:
lim2:
"""
start = max(lim1[0], lim2[0])
stop = min(lim1[1], lim2[1])
if start > stop:
return [None, None]
else:
return [start, stop]
def overlapRectArea(rect1, rect2):
"""return overlapped area
rect1:
rect2:
"""
left1, right1 = rect1[0], rect1[0]+rect1[2]
top1, bottom1 = rect1[1], rect1[1]+rect1[3]
left2, right2 = rect2[0], rect2[0]+rect2[2]
top2, bottom2 = rect2[1], rect2[1]+rect2[3]
[left3, right3] = overlapRange([left1, right1], [left2, right2])
[top3, bottom3] = overlapRange([top1, bottom1], [top2, bottom2])
if None in (left3, top3, right3, bottom3):
return 0.0
else:
area = (right3-left3)*(bottom3-top3)
area >= 0.0
return area
def getIoU(rect1, rect2):
u"""
return intersection over union
"""
area1 = rect1[2]*rect1[3]
area2 = rect2[2]*rect2[3]
intersection = overlapRectArea(rect1, rect2)
assert intersection >= 0
union = area1+area2 - intersection
assert union >= 0
IoU = intersection/float(union)
assert IoU >= 0
return IoU
def rect2bbox(rect):
"""convert rect into bbox.
tracker.init() need this data type.
"""
assert len(rect) == 4
x, y, w, h = rect
assert w > 0
assert h > 0
return (long(x), long(y), long(w), long(h))
def dets2rects(dets):
"""
convert dets type to rect type.
left, top, width, height
"""
rects = [[d.left(), d.top(), d.right()-d.left(), d.bottom()-d.top()] for d in dets]
return rects
def getBestIoU(rects, states):
u"""find best matched tracking for each rect.
rects: detected rects
states: tracking states
"""
asTrack = len(rects)*[None]
alreadyFounds = len(rects)*[0.0]
for j, rect in enumerate(rects):# 検出について
for k, (_, bbox) in enumerate(states):#追跡について
IoU = getIoU(bbox, rect)
assert IoU >= 0.0
assert len(rect) == 4
assert rect[2] > 0
assert rect[3] > 0
if IoU > alreadyFounds[j]:
alreadyFounds[j] = max(alreadyFounds[j], IoU)
asTrack[j] = k
return alreadyFounds, asTrack
def expandRegion(rect, rate):
"""expand rectange x,y,w,h keeping center postion.
rect: x,y,w,h
rate
"""
x, y, w, h = rect
xc, yc = x+w/2, y+w/2
nw = int(rate*w)
nh = int(rate*h)
nx = xc - nw/2
ny = yc - nh/2
return [nx, ny, nw, nh]
def sizedCrop(img, xyxy):
u"""Returns a rectangular region from this alignedImg.
The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.
When outside of the image is specified, it is displayed as black without an error.
"""
pilImg = PIL.Image.fromarray(img)
pilsubImg = pilImg.crop(xyxy)
subImg = np.asarray(pilsubImg)
return subImg
def test_overlapRegion():
lim = overlapRange([0, 10], [0, 10])
assert lim == [0, 10]
lim = overlapRange([0, 10], [0, 20])
assert lim == [0, 10]
lim = overlapRange([0, 10], [-10, 20])
assert lim == [0, 10]
lim = overlapRange([0, 10], [5, 10])
assert lim == [5, 10]
lim = overlapRange([0, 10], [5, 20])
assert lim == [5, 10]
lim = overlapRange([-10, 10], [5, 20])
assert lim == [5, 10]
lim = overlapRange([5, 10], [5, 20])
assert lim == [5, 10]
def test_getIoU():
IoU = getIoU([10, 20, 30, 40], [10, 20, 30, 40])
print IoU
assert IoU == 1.0
IoU = getIoU([10, 20, 30, 40], [10, 20, 30, 20])
print IoU
assert IoU <= 0.5+0.01
assert 0.5 - 0.01 <= IoU
IoU = getIoU([10, 20, 30, 40], [10, 25, 30, 40])
print IoU
assert IoU < 1.0
assert IoU >= 0.0
if __name__ == "__main__":
test_overlapRegion()
test_getIoU()