-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.py
57 lines (51 loc) · 2 KB
/
tracker.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
import math
import cv2
DISTANCE = 10
class EuclideanDistanceTracker:
def __init__(self):
self.count_id = 0
self.center_dict = {}
self.final_dict = {}
self.final_dict_copy = {}
def update(self, centers, img):
object_center_ids = []
for center in centers:
same_object = False
cx = center[0]
cy = center[1]
for id, ptx in self.center_dict.items():
eculi_distance = math.hypot(cx - ptx[0], cy - ptx[1])
if eculi_distance < 10:
self.center_dict[id] = (cx, cy)
object_center_ids.append([cx, cy, id])
same_object = True
break
if same_object == False:
self.center_dict[self.count_id] = (cx, cy)
object_center_ids.append([cx, cy, self.count_id])
self.count_id += 1
new_center_point = {}
for obj_center_id in object_center_ids:
count = 1
cx, cy, obj_id = obj_center_id
center = (cx, cy)
new_center_point[obj_id] = (cx, cy)
if obj_id in self.final_dict.keys():
_, _, count = self.final_dict[obj_id]
count += 1
self.final_dict[obj_id] = (cx, cy, count)
else:
self.final_dict[obj_id] = (cx, cy, count)
for id, ptx in self.final_dict_copy.items():
after = self.final_dict[id]
if after == ptx:
time = (after[2]/29.7)/3600
speed = round((10/1000)/time)
text = f"{speed} Km/hr"
print(f"Speed of the vehicle {id}: {speed:>2f}")
cv2.putText(img, text, (ptx[0], ptx[1]),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 255))
self.final_dict.pop(id)
self.final_dict_copy = self.final_dict.copy()
self.center_dict = new_center_point.copy()
return object_center_ids