-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcountingYolov8.py
130 lines (98 loc) · 3.13 KB
/
countingYolov8.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
import cv2
import pandas as pd
import numpy as np
from ultralytics import YOLO
from tracker import*
import cvzone
url = "#########################################/video"
model=YOLO('yolov8s.pt')
def RGB(event, x, y, flags, param):
if(event==cv2.EVENT_MOUSEMOVE):
point = [x,y]
print(point)
cv2.namedWindow('RGB')
cv2.setMouseCallback('RGB', RGB)
cap=cv2.VideoCapture('3.mp4')
# fourcc = cv2.VideoWriter_fourcc(*'avc1')
# out = cv2.VideoWriter('output.avi',fourcc, 5, (640,480))
output = cv2.VideoWriter('output_final.avi',cv2.VideoWriter_fourcc(*'MPEG'),30,(1020,500))
file = open('coco.names', 'r')
data = file.read()
class_list = data.split('\n')
count=0
persondown={}
tracker=Tracker()
counter1=[]
personup={}
counter2=[]
cy1=194
cy2=220
offset=6
while True:
ret, frame = cap.read()
if not ret:
break
#frame=stream_read()
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(rows)
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 'person' 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
cv2.circle(frame,(cx,cy),4,(255,0,255),-1)
## for down going
if (cy1<(cy+offset) and (cy1>cy-offset)):
cv2.rectangle(frame, (x3,y3),(x4,y4),(0,0,255),2)
cvzone.putTextRect(frame,f'{id}', (x3,y3), 1,2)
persondown[id]=(cx,cy)
if (id in persondown):
if (cy2<(cy+offset) and (cy2>cy-offset)):
cv2.rectangle(frame, (x3,y3),(x4,y4),(0,255,255),2)
cvzone.putTextRect(frame,f'{id}', (x3,y3), 1,2)
if counter1.count(id)==0:
counter1.append(id)
## for up going
if (cy2<(cy+offset) and (cy2>cy-offset)):
cv2.rectangle(frame, (x3,y3),(x4,y4),(0,255,0),2)
cvzone.putTextRect(frame,f'{id}', (x3,y3), 1,2)
personup[id]=(cx,cy)
if (id in personup):
if (cy1<(cy+offset) and (cy1>cy-offset)):
cv2.rectangle(frame, (x3,y3),(x4,y4),(0,255,255),2)
cvzone.putTextRect(frame,f'{id}', (x3,y3), 1,2)
if counter2.count(id)==0:
counter2.append(id)
cv2.line(frame,(3,cy1), (1018,cy1),(0,255,0),2)
cv2.line(frame,(5,cy2), (1019,cy2),(0,255,255),2)
#print(persondown)
#print(counter1)
#print(len(counter1)) #lenght means we can get the counnt who is going down
downcount=len(counter1)
upcount=len(counter2)
cvzone.putTextRect(frame, f'Down: {downcount}', (50,60), 2,2)
cvzone.putTextRect(frame, f'Up: {upcount}', (50,160), 2,2)
output.write(frame)
cv2.imshow('RGB', frame)
if cv2.waitKey(1) & 0xff==27:
break
cap.release()
cv2.destroyAllWindows()