-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking_&_counting.py
115 lines (83 loc) · 2.78 KB
/
tracking_&_counting.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
import cv2
import pandas as pd
from ultralytics import YOLO
from tracker import*
model=YOLO('yolov8s.pt')
def RGB(event, x, y, flags, param):
if event == cv2.EVENT_MOUSEMOVE :
colorsBGR = [x, y]
print(colorsBGR)
cv2.namedWindow('RGB')
cv2.setMouseCallback('RGB', RGB)
cap=cv2.VideoCapture('/home/raspproject/yolov8counting-trackingvehicles-main/veh2.mp4')
my_file = open("coco.txt", "r")
data = my_file.read()
class_list = data.split("\n")
#print(class_list)
count=0
tracker=Tracker()
cy1=322
cy2=368
offset=6
vh_down={}
counter=[]
vh_up={}
counter1=[]
while True:
ret,frame = cap.read()
if not ret:
break
count += 1
if count % 3 != 0:
continue
frame=cv2.resize(frame,(1020,500))
results=model.predict(frame)
# print(results)
a=results[0].boxes.data
px=pd.DataFrame(a).astype("float")
# print(px)
list=[]
for index,row in px.iterrows():
# print(row)
x1=int(row[0])
y1=int(row[1])
x2=int(row[2])
y2=int(row[3])
d=int(row[5])
c=class_list[d]
if 'car' in c:
list.append([x1,y1,x2,y2])
bbox_id=tracker.update(list)
for bbox in bbox_id:
x3,y3,x4,y4,id=bbox
cx=int(x3+x4)//2
cy=int(y3+y4)//2
if cy1<(cy+offset) and cy1>(cy-offset):
vh_down[id]=cy
if id in vh_down:
if cy2<(cy+offset) and cy2>(cy-offset):
cv2.circle(frame,(cx,cy),4,(0,0,255),-1)
cv2.putText(frame,str(id),(cx,cy),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,255,255),2)
if counter.count(id)==0:
counter.append(id)
if cy2<(cy+offset) and cy2>(cy-offset):
vh_up[id]=cy
if id in vh_up:
if cy1<(cy+offset) and cy1>(cy-offset):
cv2.circle(frame,(cx,cy),4,(0,0,255),-1)
cv2.putText(frame,str(id),(cx,cy),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,255,255),2)
if counter1.count(id)==0:
counter1.append(id)
cv2.line(frame,(274,cy1),(814,cy1),(255,255,255),1)
cv2.putText(frame,('Line-1'),(274,cy1),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,255,255),2)
cv2.line(frame,(177,cy2),(927,cy2),(255,255,255),1)
cv2.putText(frame,('Line-2'),(177,cy2),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,255,255),2)
d=len(counter)
cv2.putText(frame,('Going_Down-')+str(d),(60,40),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,255,255),2)
d1=len(counter1)
cv2.putText(frame,('Going_Up-')+str(d1),(60,130),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,255,255),2)
cv2.imshow("RGB", frame)
if cv2.waitKey(1)&0xFF==27:
break
cap.release()
cv2.destroyAllWindows()