-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_webcam.py
110 lines (92 loc) · 3.27 KB
/
main_webcam.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
import cv2
import pandas as pd
import numpy as np
from ultralytics import YOLO
from tracker import *
import cvzone
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(0)
my_file = open("coco.txt", "r")
data = my_file.read()
class_list = data.split("\n")
# print(class_list)
count = 0
tracker = Tracker()
# polygon shape coordinates:
# 1 #4
# 2 #3
# webcam area1-red-exit, area2-green-enter
area1 = [(295, 387), (280, 400), (616, 404), (606, 381)]
area2 = [(318, 407), (311, 427), (607, 425), (616, 409)]
people_enter = {}
counter1 = []
people_exit = {}
counter2 = []
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.boxes
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 '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
# Exiting Counter
results = cv2.pointPolygonTest(np.array(area1, np.int32), ((x4, y4)), False)
if results >= 0:
people_exit[id] = (x4, y4)
if id in people_exit:
results1 = cv2.pointPolygonTest(np.array(area2, np.int32), ((x4, y4)), False)
if results1 >= 0:
cv2.rectangle(frame, (x3, y3), (x4, y4), (255, 0, 255), 1)
cv2.circle(frame, (x4, y4), 4, (255, 0, 0), -1)
cvzone.putTextRect(frame, f'{id}', (x3, y3), 1, 2)
if counter2.count(id) == 0: # For not counting same id again
counter2.append(id)
# Entering Counter
results2 = cv2.pointPolygonTest(np.array(area2, np.int32), ((x4, y4)), False)
if results2 >= 0:
people_enter[id] = (x4, y4)
if id in people_enter:
results3 = cv2.pointPolygonTest(np.array(area1, np.int32), ((x4, y4)), False)
if results3 >= 0:
cv2.rectangle(frame, (x3, y3), (x4, y4), (255, 0, 255), 1)
cv2.circle(frame, (x4, y4), 4, (255, 0, 0), -1)
cvzone.putTextRect(frame, f'{id}', (x3, y3), 1, 2)
if counter1.count(id) == 0: # For not counting same id again
counter1.append(id)
cv2.polylines(frame, [np.array(area1, np.int32)], True, (0, 0, 255), 1)
cv2.polylines(frame, [np.array(area2, np.int32)], True, (0, 255, 0), 1)
er = (len(counter1))
et = (len(counter2))
cvzone.putTextRect(frame, f'People Enter: {er}', (50, 50), 3, 2)
cvzone.putTextRect(frame, f'People Exit: {et}', (50, 100), 3, 2)
cv2.imshow("RGB", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()