forked from DrewNF/Tensorflow_Object_Tracking_Video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiclass_rectangle.py
executable file
·280 lines (232 loc) · 9.32 KB
/
multiclass_rectangle.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
import copy
import utils_video
#####################################################################################################################################################################
########################## CLASS AND FUNCTIONS DEFINED TO USE WITH TENSORBOX (MAINLY TO PARSE & WRITE .IDL FILE AND MANAGE RESULTS) #################################
#####################################################################################################################################################################
class Rectangle_Multiclass(object):
def __init__(self):
# Initialization Function
# BBox Parameters
self.cx = -1
self.cy = -1
self.width = -1
self.height = -1
self.true_confidence = -1
self.x1 = -1
self.x2 = -1
self.y1 = -1
self.y2 = -1
# Track Parameter
self.trackID=-1
# Label Parameters
self.label_confidence = -1
self.label= 'Not Set'
self.label_chall='Not Set'
self.label_code= 'Not Set'
### Safe Loading Values Functions
def load_labeled_rect(self, trackID, rect_conf, label_conf, x1, x2, y1, y2, label, label_chall, code):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.label=label
self.label_code=code
self.label_chall=label_chall
self.trackID=trackID
self.label_confidence=label_conf
self.true_confidence=rect_conf
def load_BBox(self, x1, x2, y1, y2, label, label_chall, code):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.label=label
self.label_code=code
self.label_chall=label_chall
def set_unlabeled_rect(self, cx, cy, width, height, confidence):
# Set unlabeled rect info to be processed forward
self.cx = cx
self.cy = cy
self.width = width
self.height = height
self.true_confidence = confidence
self.x1 = self.cx - self.width/2.
self.x2 = self.cx + self.width/2.
self.y1 = self.cy - self.height/2.
self.y2 = self.cy + self.height/2.
def add_delta(self, dx1, dx2, dy1, dy2):
# Set unlabeled rect info to be processed forward
self.x1 = self.x1+dx1
self.x2 = self.x2+dx2
self.y1 = self.y1+dy1
self.y2 = self.y2+dy2
self.cx = (self.x1 + self.x2)/2.
self.cy = (self.y1 + self.y2)/2.
self.width = max(self.x1,self.x2) - min(self.x1,self.x2)
self.height = max(self.y1,self.y2) - min(self.y1,self.y2)
def set_rect_coordinates(self, x1, x2, y1, y2):
# Set rect coordinates info to be processed forward
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
self.cx = (self.x1 + self.x2)/2.
self.cy = (self.y1 + self.y2)/2.
self.width = max(self.x1,self.x2) - min(self.x1,self.x2)
self.height = max(self.y1,self.y2) - min(self.y1,self.y2)
def load_label(self, trackID, label_conf, label, label_chall, code):
self.label=label
self.label_code=code
self.label_chall=label_chall
self.trackID=trackID
self.label_confidence=label_conf
def load_trackID(self, trackID):
self.trackID=trackID
def set_label(self, label_conf, label, label_chall, code):
self.label=label
self.label_code=code
self.label_chall=label_chall
self.label_confidence=label_conf
def check_rects_motion(self,filename, rect, dx1, dx2, dy1,dy2, error=1.2, attenuation=1.1):
## Rect is considered passed befor through add_delta
if((self.x1-rect.x1)>dx1*error)| ((self.y1-rect.y1)>dy1*error)|((self.x2-rect.x2)>dx2*error)|((self.y2-rect.y2)>dy2*error):
utils_video.draw_rectangle(filename,(self.x1, self.y1,self.x2, self.y2))
delta_cx=self.cx-rect.cx
delta_cy=self.cy-rect.cy
self.x1 =rect.x1 + delta_cx
self.y1 =rect.y1 + delta_cy
self.x2 =rect.x2 + delta_cx
self.y2 =rect.y2 + delta_cy
self.cx = (self.x1 + self.x2)/2.
self.cy = (self.y1 + self.y2)/2.
self.width = max(self.x1,self.x2) - min(self.x1,self.x2)
self.height = max(self.y1,self.y2) - min(self.y1,self.y2)
### Safe Duplicate functions
def duplicate(self):
new_rect=Rectangle_Multiclass()
new_rect.cx = copy.copy(self.cx)
new_rect.cy = copy.copy(self.cy)
new_rect.width = copy.copy(self.width)
new_rect.height = copy.copy(self.height)
new_rect.true_confidence = copy.copy(self.true_confidence)
new_rect.label_confidence = copy.copy(self.label_confidence)
new_rect.label= copy.copy(self.label)
new_rect.trackID=copy.copy(self.trackID)
new_rect.x1 = copy.copy(self.x1)
new_rect.x2 = copy.copy(self.x2)
new_rect.y1 = copy.copy(self.y1)
new_rect.y2 = copy.copy(self.y2)
return new_rect
### Computation Functions
def overlaps(self, other):
if abs(self.cx - other.cx) > (self.width + other.width) / 1.5:
return False
elif abs(self.cy - other.cy) > (self.height + other.height) / 2.0:
return False
else:
return True
def distance(self, other):
return sum(map(abs, [self.cx - other.cx, self.cy - other.cy,
self.width - other.width, self.height - other.height]))
def intersection(self, other):
left = max(self.cx - self.width/2., other.cx - other.width/2.)
right = min(self.cx + self.width/2., other.cx + other.width/2.)
width = max(right - left, 0)
top = max(self.cy - self.height/2., other.cy - other.height/2.)
bottom = min(self.cy + self.height/2., other.cy + other.height/2.)
height = max(bottom - top, 0)
return width * height
def area(self):
return self.height * self.width
def union(self, other):
return self.area() + other.area() - self.intersection(other)
def iou(self, other):
return self.intersection(other) / self.union(other)
def __eq__(self, other):
return (self.cx == other.cx and
self.cy == other.cy and
self.width == other.width and
self.height == other.height and
self.confidence == other.confidence and
self.label_confidence == other.label_confidence and self.label == other.label and self.trackID == other.trackID)
### Functions for parse the xml into the .idl file to train TENSORBOX
def get_label_string(self):
"""Get the string of the label of the rect."""
string=""
if self.label is not 'Not Set':
string=self.label +' '
return string
def get_code_string(self):
"""Get the string of the label of the rect."""
string=""
if self.label_code is not -1:
string=self.label_code +' '
return string
def get_chall_string(self):
"""Get the string of the label of the rect."""
string=""
if self.label_chall is not 'Not Set':
string=str(self.label_chall) +' '
return string
def get_coord_string(self):
"""Get the string of the coordinates of the rect."""
string='('+str(self.x1)+','+str(self.y1)+','+str(self.x2)+','+str(self.y2)+')'
return string
def get_rect_string(self):
"""Get the string of the infos of the rect."""
string='('+str(self.x1)+','+str(self.y1)+','+str(self.x2)+','+str(self.y2)+')'
if self.label_chall is not 'Not Set':
string=string+' /' + str(self.label_chall)
return string
def duplicate_rects(rects):
new_rects=[]
for rect in rects:
new_rect=Rectangle_Multiclass()
new_rect.cx = copy.copy(rect.cx)
new_rect.cy = copy.copy(rect.cy)
new_rect.width = copy.copy(rect.width)
new_rect.height = copy.copy(rect.height)
new_rect.true_confidence = copy.copy(rect.true_confidence)
new_rect.label_confidence = copy.copy(rect.label_confidence)
new_rect.label= copy.copy(rect.label)
new_rect.trackID=copy.copy(rect.trackID)
new_rect.x1 = copy.copy(rect.x1)
new_rect.x2 = copy.copy(rect.x2)
new_rect.y1 = copy.copy(rect.y1)
new_rect.y2 = copy.copy(rect.y2)
new_rects.append(new_rect)
return new_rects
def pop_max_iou(rects, rect):
max_iou=None
max_id=0
rect_id=0
for rectangle in rects:
if max_iou is None:
max_iou=rect.iou(rectangle)
max_id=rect_id
if rect.iou(rectangle)>max_iou:
max_iou=rect.iou(rectangle)
max_id=rect_id
rect_id=rect_id+1
if len(rects)>max_id:
new_rect=rects[max_id].duplicate()
rects.pop(max_id)
return new_rect
else: return None
def pop_max_overlap(rects, rect):
max_overlap=None
max_id=0
rect_id=0
for rectangle in rects:
if max_overlap is None:
max_overlap=rect.overlaps(rectangle)
max_id=rect_id
if rect.iou(rectangle)>max_overlap:
max_overlap=rect.overlaps(rectangle)
max_id=rect_id
rect_id=rect_id+1
if len(rects)>max_id:
new_rect=rects[max_id].duplicate()
rects.pop(max_id)
return new_rect
else: return None