-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclick.py
104 lines (77 loc) · 2.49 KB
/
click.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
import cv2
import pandas as pd
from ultralytics import YOLO
import cvzone
import numpy as np
from tracker import Tracker
import time
model = YOLO('models/yolov5su.pt')
target_width = 1000
target_height = 600
line_start = (100, target_height - target_height // 3)
line_end = (target_width - 100, target_height - target_height // 3)
# Define default colors for traffic light
DEFAULT_RED = (0, 0, 255)
DEFAULT_GREEN = (0, 255, 0)
# Initialize traffic light color and timer
light_color = DEFAULT_GREEN
light_timer = time.time()
def select_color(event, x, y, flags, param):
global light_color
if event == cv2.EVENT_LBUTTONDOWN:
light_color = DEFAULT_RED
elif event == cv2.EVENT_RBUTTONDOWN:
light_color = DEFAULT_GREEN
cap = cv2.VideoCapture('videos/veh6.mp4')
cv2.namedWindow('Traffic Control')
cv2.setMouseCallback('Traffic Control', select_color)
my_file = open("coco1.txt", "r")
data = my_file.read()
class_list = data.split("\n")
cy1 = 427
offset = 6
count = 0
tracker = Tracker()
vehicle_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
count += 1
if count % 3 != 0:
continue
# Change traffic light color every 5 seconds
if time.time() - light_timer > 5:
light_timer = time.time()
frame = cv2.resize(frame, (1020, 500))
results = model.predict(frame)
a = results[0].boxes.data
px = pd.DataFrame(a).astype("float")
bbox_idx = []
for index, row in px.iterrows():
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 'bus' in c or 'car' in c or 'truck' in c:
bbox_idx.append([x1, y1, x2, y2])
for bbox in bbox_idx:
x3, y3, x4, y4 = bbox
cx = int(x3 + x4) // 2
cy = int(y3 + y4) // 2
if cy1 < (cy + offset) and cy1 > (cy - offset):
if light_color == DEFAULT_RED:
cv2.rectangle(frame, (x3, y3), (x4, y4), (0, 255, 0), 2)
cvzone.putTextRect(frame, f'ID: {vehicle_count}', (x3, y3), 1, 1)
vehicle_count += 1
cvzone.putTextRect(frame, f'vehicle count: {vehicle_count}', (50, 60), 2, 2)
cv2.line(frame, line_start, line_end, (0, 255, 0), 2)
# Draw traffic light
cv2.circle(frame, (500, 50), 30, light_color, -1)
cv2.imshow("Traffic Control", frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()