forked from DrewNF/Tensorflow_Object_Tracking_Video
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulticlass_rectangle.py
More file actions
executable file
·327 lines (276 loc) · 11 KB
/
multiclass_rectangle.py
File metadata and controls
executable file
·327 lines (276 loc) · 11 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- coding: UTF-8 -*-
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.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.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.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
## rectは推定される現在の位置
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))
# current_frame - previous_frame
delta_cx=self.cx-rect.cx
delta_cy=self.cy-rect.cy
# previous_frae + delta_cx
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.confidence = copy.copy(self.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
# 現在フレームのrectからmax_iouなdect除く
def overlaps(self, other):
# 絶対値
# 多分画像サイズが3:4だから...?
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):
# selfはprevious
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)
# other: rectangle
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.confidence = copy.copy(rect.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 confirm_search_area(current_rect, before_rect, wariai):
x_area = before_rect.width * wariai
y_area = before_rect.height * wariai
if current_rect.x2 < (before_rect.x1 - x_area) or current_rect.x1 > (before_rect.x2 + x_area) or current_rect.y1 > (before_rect.y2 + y_area) or current_rect.y2 < (before_rect.y1 - y_area):
return False
else :
return True
# 返り値:max_iouな現在フレームのrect
# (current, previous)
def pop_max_iou(rects, rect):
max_iou=None
max_id=0
rect_id=0
wariai = 0.5 # width or height * wariai の範囲で探索を行う
# previous , corrent frameでmax_iouを計算
# 軸はprevious
for rectangle in rects:
# 最初のみ
if max_iou is None:
max_iou=rect.iou(rectangle)
# rect_id = 0
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 confirm_search_area(rectangle,rect, wariai):
# if max_iou is None:
# max_iou=rect.iou(rectangle)
# # rect_id = 0
# max_id=rect_id
#
# if rect.iou(rectangle)>max_iou:
# max_iou=rect.iou(rectangle)
# max_id=rect_id
# rect_id=rect_id+1
# else:
# continue
# 以下のif必要?
# max_iou = 0だったら
if max_iou == 0 :
return None
if len(rects)>max_id:
# max_iouなrectを代入
new_rect=rects[max_id].duplicate()
# 現在フレームのrectからmax_iouなdect除く
# iouが0の時は余ってるrectsの中でindexが最も小さいもの
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