-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeed_estimate_class_updated3.py
315 lines (237 loc) · 14.9 KB
/
speed_estimate_class_updated3.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from __future__ import print_function, absolute_import
import cv2
import math
import numpy as np
from scipy.optimize import least_squares, leastsq
#from auto_track import analytics_rbc
import hourglass_single
class speed_estimation:
def __init__(self):
self.time = 1/20 # set to time duration between two consecutive frames
self.tracked = []
self.last = []
self.tracking = []
self.framecount = 0
self.frame_number = 0
self.model = hourglass_single.Hourglass()
# self.prev_frame = np.zeros((img_width, img_height))
def flip(self, pos):
if pos[2]<0:
pos*=-1
return pos
def distance(self, last_pos, current_pos):
val=0
for i in range(0,3):
val += (last_pos[i]-current_pos[i])*(last_pos[i]-current_pos[i])
val=math.pow(val,0.5)
return val
def compute_pose(self, points, imagepoints):
pts={0: [45.0, 256.5, 71.0], 1: [-45.0, 256.5, 71.0], 2: [56.0, 186.7, 89.0], 3: [-56.0, 186.7, 89.0], 4: [51.0, 240.0, 149.5], 5: [-51.0, 240.0, 149.5], 6: [0.0, 260.0, 41.5], 7: [-47.5, 43.6, 104.7], 8: [-57.0, 57.8, 145.5], 9: [-54.3, 135.0, 149.8], 10: [-66.4, 220.0, 19.8], 11: [-43.0, 68.0, 165.6], 12: [57.0, 57.8, 145.5], 13: [54.3, 135.0, 149.8], 14: [43.0, 68.0, 165.6], 15: [47.5, 43.6, 104.7], 16: [66.4, 220.0, 19.8], 17: [44.2, 61.5, 152.4], 18: [-44.2, 61.5, 152.2], 19: [49.0, 56.0, 116.5], 20: [-49.0, 56.0, 116.5], 21: [0.0, 0.0, 38.0], 22: [33.0, 36.0, 83.5], 23: [-33.0, 36.0, 83.5]}
'''order of pts: rear light (left), rear light (right), greenlow corner left, greenlow corner right, top corner rear left, top corner rear right, rear center, indicator light right,
right mirror, right center pole top, right wheel, top corner front right, left mirror, left center pole top, top corner front left , indicator light left, left wheel, Wind shield(top left)
Wind shield(top right), Wind shield(bottom left), Wind shield(bottom right), front bonet, head light left, Head light right'''
#insert new camera matrix here
mtx = np.array([[1324.110551, 0.000000, 993.993108], [0.000000, 1324.110210, 621.997610],[ 0. , 0. , 1. ]])
dist = np.array([[-0.401747, 0.148985, -0.008159, -0.006626, 0.000000]])
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
worldpoints = np.zeros([len(points),3])
twist = np.zeros([len(points),2])
j=0
for i in points:
worldpoints[j] = pts[i]
j+=1
for i in range(len(imagepoints)):
twist[i] = [float(imagepoints[i][0]), float(imagepoints[i][1])]
_, rvecs, tvecs = cv2.solvePnP(worldpoints, twist, mtx, dist)
tvecs = self.flip(tvecs)
# mean_error = 0
# for i in range(len(points)):
# imgpoint = np.array([float(imagepoints[i][0]), float(imagepoints[i][1])])
# ponting = np.array([[pts[i]]], dtype=np.float)
# imgpoints2, _ = cv2.projectPoints(ponting, rvecs, tvecs, mtx, dist)
# error = cv2.norm(imgpoint, imgpoints2[0][0], cv2.NORM_L2)/len(imgpoints2)
# mean_error += error
# old_error = mean_error/len(points)
# placeholder = np.array([0, 0, 0, 0, 0, 0], dtype=np.float)
# for i in range(len(rvecs)):
# placeholder[i]=rvecs[i]
# placeholder[i+3]=tvecs[i]
# twist_reshaped = twist.reshape(-1)
# print (twist_reshaped.shape)
# pose_opt = leastsq(reproject_error, placeholder, args = (points, twist_reshaped), maxfev=10)
# pose_opt = np.asarray(pose_opt)
# tvecs_opt, rvecs_opt = pose_opt[0][3:6], pose_opt[0][0:3]
# tvecs_opt = flip(tvecs_opt)
# mean_error = 0
# for i in range(len(points)):
# imgpoint = np.array([float(imagepoints[i][0]), float(imagepoints[i][1])])
# ponting = np.array([[pts[i]]], dtype=np.float)
# imgpoints2, _ = cv2.projectPoints(ponting, rvecs_opt, tvecs_opt, mtx, dist)
# error = cv2.norm(imgpoint, imgpoints2[0][0], cv2.NORM_L2)/len(imgpoints2)
# mean_error += error
# new_error = mean_error/len(points)
# print ("translation", old_error - new_error, old_error, new_error)
# #time.sleep(1)
# if new_error<=old_error:
# return tvecs_opt
# else:
# return tvecs
return tvecs
def draw_boxes(self, frame, class_boxes, speed, onlyspeed):
for item in class_boxes:
classname = item['classname']
bbox = item['bbox']
id_number = item['id']
#speed = item['speed']
bbox = list(map(int,bbox))
x1,y1,x2,y2 = bbox
#print ("well", x1, x2, y1, y2)
#time.sleep(1)
#cv2.line(frame,count_line[0],count_line[1],(0,0,0),2)
cropped = frame[y1:y2, x1:x2]
cv2.putText(frame, "ID:"+str(id_number) , (x2,y2),cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0 , 0),2, cv2.LINE_AA)
cv2.rectangle(frame, (x1,y1),(x2,y2),(255, 255, 0),3)
cv2.putText(frame, str(classname), (int(x1), int(y1)), cv2.FONT_HERSHEY_COMPLEX,1.15, (0, 255, 255),2)
if (onlyspeed==True):
cv2.putText(frame, "Speed:"+str(round(speed, 2))+ "m/s" , (x2+50,y2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0 , 0),2, cv2.LINE_AA)
return frame, cropped
def init_trackers(self, back, front, position, detection):
if back>=2:
self.tracking.append({'track_id': detection['id'], 'detections':0, 'lastpose': position, 'speed':0, 'currentpose': position, 'computes': 0, 'orientation': 'away', 'lastframe':self.framecount, 'currentframe': self.framecount, 'latest':0})
#print ("orientation", "away", back, front, position)
elif front>=3:
self.tracking.append({'track_id': detection['id'], 'detections':0, 'lastpose': position, 'speed':0, 'currentpose': position, 'computes': 0, 'orientation': 'towards', 'lastframe':self.framecount, 'currentframe': self.framecount, 'latest':0})
#print ("orientation", "towards", back, front, position)
else:
self.tracking.append({'track_id': detection['id'], 'detections':0, 'lastpose': position, 'speed':0, 'currentpose': position, 'computes': 0, 'orientation': 'unknown', 'lastframe':self.framecount, 'currentframe': self.framecount, 'latest':0})
#print ("orientation", "unknown", back, front, position)
def transform_points(self, points, class_box, frame):
transformed_points=[]
for point in points:
x,y = int(point[0]+class_box['bbox'][0]), int(point[1]+class_box['bbox'][1])
transformed_points.append([x,y])
cv2.circle(frame, (x,y), 5, (0,0,255), -1)
return frame, transformed_points
def get_keypoints(self, auto_cropped):
cropped = np.array(cv2.cvtColor(auto_cropped, cv2.COLOR_BGR2RGB))
cropped_new = np.moveaxis(cropped, 2, 0)
pointers, points = self.model.forward_pass(cropped_new)
return pointers, points
def check_orientation(self, pointers):
back=0
front=0
for idx in pointers:
if (idx == 0) or (idx == 1) or (idx == 6):
back+=1
elif (idx==17) or (idx==18) or (idx==19) or (idx==20) or (idx==21):
front+=1
return back, front
def decrement(self, tracked_vehicle):
tracked_vehicle['detections']-=1
tracked_vehicle['computes']-=1
return tracked_vehicle
def update_tracked_frame(self, tracked_vehicle, position):
tracked_vehicle['currentpose'] = position
tracked_vehicle['currentframe'] = self.framecount
tracked_vehicle['detections'] = 0
return tracked_vehicle
def update_speed(self, tracked_vehicle, position, orientation, speed):
tracked_vehicle['speed'] += speed/100.0
tracked_vehicle['lastpose'] = position
#can be used for drawing boxes with speed info
#extra, unnecessary = self.draw_boxes(frame, [i], tracked_vehicle['speed']/tracked_vehicle['computes'], True)
print ("\n", 'speed', tracked_vehicle['speed']/tracked_vehicle['computes'], tracked_vehicle['track_id'], speed, orientation, tracked_vehicle['currentframe'], tracked_vehicle['lastframe'])
tracked_vehicle['lastframe'] = self.framecount
tracked_vehicle['latest'] = tracked_vehicle['speed']/tracked_vehicle['computes']
return tracked_vehicle
def speed_estimate(self, sframe):
#only need to call this function after init the class, see the arguments needed
#returns tracking - a dictionary that contains vehicle ID, speed, number of computes, current and last pose (see the function "init_trackers" for a complete list of keywords)
#i represents a single vehicle detection
frame = sframe.get_image()
trackers = sframe.get_trackers()
for track in trackers:
if not track.is_confirmed() or track.time_since_update > 1:
continue
i = {}
i['id'] = str(track.track_id)
i['bbox'] = track.to_tlbr()
i['classname'] = track.classname
if not(any(i['id'] == x for x in self.tracked)):
frame, cropped = self.draw_boxes(frame, [i], 0, False)
ht, wd = cropped.shape[0], cropped.shape[1]
if (ht>100 and wd>100) or (ht>150) or (wd>150):
pointers, points = self.get_keypoints(cropped)
frame, transformed_points = self.transform_points(points, i, frame)
if len(points)>=4:
self.tracked.append(i['id'])
position = self.compute_pose(pointers, transformed_points)
back, front = self.check_orientation(pointers)
self.init_trackers(back, front, position, i)
elif (i['id'] == x for x in self.tracked):
frame, cropped = self.draw_boxes(frame, [i], 0, False)
ht, wd = cropped.shape[0], cropped.shape[1]
for j in self.tracking:
if j['track_id']==i['id']:
frame, cray = self.draw_boxes(frame, [i], j['latest'], True)
# cv2.imshow("frame", frame)
j['detections']+=1
if j['detections']==2:
print ("pair detected")# j["detections"], framecount)
if (ht>100 and wd>100) or (ht>150) or (wd>150):
j['computes']+=1
pointers, points = self.get_keypoints(cropped)
frame, transformed_points = self.transform_points(points, i, frame)
#needed only to verify the number of keypoints detected
#print ("length", len(points))
if len(points)>=4:
position = self.compute_pose(pointers, transformed_points)
if (j['orientation'] == 'away') and (position[2]>=j['lastpose'][2]):
if ((abs(position[2]-j['lastpose'][2]))<(750+(self.framecount-j['lastframe'])*20)) and ((abs(position[2]-j['lastpose'][2]))>50): #(framecount-j['lastframe'])*20):
j = self.update_tracked_frame(j, position)
speed = self.distance(j['lastpose'], j['currentpose'])/(float(j['currentframe']-j['lastframe'])*self.time)
if speed < 2000:
j = self.update_speed(j, speed, position, frame, [i], "moving away", speed)
else:
j = self.decrement(j)
else:
j = self.decrement(j)
elif (j['orientation']=='towards') and (position[2]<=j['lastpose'][2]):
if ((abs(position[2]-j['lastpose'][2]))<(750+(self.framecount-j['lastframe'])*20)) and ((abs(position[2]-j['lastpose'][2])) > 50): # (framecount-j['lastframe'])*20):
j = self.update_tracked_frame(j, position)
speed = self.distance(j['lastpose'], j['currentpose'])/(float(j['currentframe']-j['lastframe'])*self.time)
if speed<2000:
j = self.update_speed(j, speed, position, frame, [i], "moving closer", speed)
else:
j = self.decrement(j)
else:
j = self.decrement(j)
elif (j['orientation']=='unknown'):
back, front = self.check_orientation(pointers)
if back >= 2:
j['orientation'] = 'away'
elif front >= 3:
j['orientation'] = 'towards'
if ((abs(position[2]-j['lastpose'][2]))<(750+(self.framecount-j['lastframe'])*20)) and ((abs(position[2]-j['lastpose'][2])) > 50): #(framecount-j['lastframe'])*20):
j = self.update_tracked_frame(j, position)
speed = self.distance(j['lastpose'], j['currentpose'])/(float(j['currentframe']-j['lastframe'])*self.time)
if speed < 2000:
j = self.update_speed(j, speed, position, frame, [i], "orientation unknown", speed)
else:
j = self.decrement(j)
else:
j = self.decrement(j)
else:
j = self.decrement(j)
else:
j = self.decrement(j)
else:
j['detections'] -= 1
for x in self.tracked:
if not(any(x == m.track_id for m in trackers)):
self.tracked.remove(x)
print("tracked IDs", self.tracked)
self.frame_number += 1
cv2.imshow("frame", sframe.get_image())
# contains speed estimates as well - you can also return the frames on which the speed values are written
return self.tracking