-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
111 lines (92 loc) · 5.11 KB
/
tempCodeRunnerFile.py
File metadata and controls
111 lines (92 loc) · 5.11 KB
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
import os
import cv2
import pandas as pd
import time
from ultralytics import YOLO
from tracker import Tracker
model = YOLO('yolov8s.pt')
class_list = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven',
'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
tracker = Tracker()
count = 0
cap = cv2.VideoCapture('highway.mp4') # Initialize with webcam feed (index 0)
down = {}
up = {}
counter_down = []
counter_up = []
red_line_y = 198
blue_line_y = 268
offset = 6
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (840, 480)) # Adjust resolution as needed
while True:
ret, frame = cap.read()
if not ret:
break
count += 1
frame = cv2.resize(frame, (840, 480)) # Adjust frame size as needed
results = model.predict(frame)
a = results[0].boxes.data
a = a.detach().cpu().numpy()
px = pd.DataFrame(a).astype("float")
detected_list = []
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 'car' in c:
detected_list.append([x1, y1, x2, y2])
bbox_id = tracker.update(detected_list)
for bbox in bbox_id:
x3, y3, x4, y4, id = bbox
cx = int(x3 + x4) // 2
cy = int(y3 + y4) // 2
if red_line_y < (cy + offset) and red_line_y > (cy - offset):
down[id] = time.time() # current time when vehicle touches the first line
if id in down:
if blue_line_y < (cy + offset) and blue_line_y > (cy - offset):
elapsed_time = time.time() - down[id] # current time when vehicle touches the second line.
if counter_down.count(id) == 0:
counter_down.append(id)
distance = 100 # meters
a_speed_ms = distance / elapsed_time
a_speed_kh = a_speed_ms * 3.6 # Speed in kilometers per hour
cv2.circle(frame, (cx, cy), 4, (0, 0, 255), -1)
cv2.rectangle(frame, (x3, y3), (x4, y4), (0, 255, 0), 2) # Draw bounding box
cv2.putText(frame, str(id), (x3, y3), cv2.FONT_HERSHEY_COMPLEX, 0.6, (255, 255, 255), 1)
cv2.putText(frame, str(int(a_speed_kh)) + 'Km/h', (x4, y4), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 255), 2)
if blue_line_y < (cy + offset) and blue_line_y > (cy - offset):
up[id] = time.time()
if id in up:
if red_line_y < (cy + offset) and red_line_y > (cy - offset):
elapsed1_time = time.time() - up[id]
if counter_up.count(id) == 0:
counter_up.append(id)
distance1 = 100 # meters
a_speed_ms1 = distance1 / elapsed1_time
a_speed_kh1 = a_speed_ms1 * 3.6
cv2.circle(frame, (cx, cy), 4, (0, 0, 255), -1)
cv2.rectangle(frame, (x3, y3), (x4, y4), (0, 255, 0), 2) # Draw bounding box
cv2.putText(frame, str(id), (x3, y3), cv2.FONT_HERSHEY_COMPLEX, 0.6, (255, 255, 255), 1)
cv2.putText(frame, str(int(a_speed_kh1)) + 'Km/h', (x4, y4), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 255), 2)
text_color = (0, 0, 0) # Black color for text
yellow_color = (52, 235, 67) # Yellow color for background
red_color = (205, 105, 180) # Red color for lines
blue_color = (255, 0, 0) # Blue color for lines
cv2.rectangle(frame, (0, 0), (230, 70), yellow_color, -1)
cv2.line(frame, (172, 198), (774, 198), red_color, 2)
cv2.putText(frame, ('Red Line'), (172, 198), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1, cv2.LINE_AA)
cv2.line(frame, (8, 268), (927, 268), blue_color, 2)
cv2.putText(frame, ('Blue Line'), (8, 268), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1, cv2.LINE_AA)
cv2.putText(frame, ('Going Down - ' + str(len(counter_down))), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1, cv2.LINE_AA)
cv2.putText(frame, ('Going Up - ' + str(len(counter_up))), (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1, cv2.LINE_AA)
out.write(frame)
cv2.imshow("frames", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
out.release()
cv2.destroyAllWindows()