-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculate Speed.py
79 lines (62 loc) · 2.09 KB
/
Calculate Speed.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
import cv2
import numpy as np
import time
# Function to calculate speed
def calculate_speed(distance, time_diff):
speed = distance / time_diff
return speed
# Constants
LEFT_LINE_X = 100 # x-coordinate of the left line
RIGHT_LINE_X = 500 # x-coordinate of the right line
LINE_Y = 50 # y-coordinate of the line
# Initialize variables
object_passed_left = False
object_passed_right = False
start_time = None
# Function to detect when the object passes the line
def check_object_passed(x):
global object_passed_left, object_passed_right
if x < LEFT_LINE_X:
object_passed_left = True
elif x > RIGHT_LINE_X:
object_passed_right = True
# Function to draw lines on the frame
def draw_lines(frame):
cv2.line(frame, (LEFT_LINE_X, 0), (LEFT_LINE_X, frame.shape[0]), (0, 255, 0), 2)
cv2.line(frame, (RIGHT_LINE_X, 0), (RIGHT_LINE_X, frame.shape[0]), (0, 255, 0), 2)
# Capture video from the webcam
cap = cv2.VideoCapture(0)
# Main loop
while True:
ret, frame = cap.read()
if not ret:
break
draw_lines(frame)
# Convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect objects in the frame
detector = cv2.SimpleBlobDetector_create()
keypoints = detector.detect(gray)
for keypoint in keypoints:
x = int(keypoint.pt[0])
y = int(keypoint.pt[1])
cv2.circle(frame, (x, y), 5, (0, 0, 255), -1)
check_object_passed(x)
if object_passed_left and object_passed_right:
if start_time is None:
start_time = time.time()
else:
end_time = time.time()
time_diff = end_time - start_time
distance = RIGHT_LINE_X - LEFT_LINE_X
speed = calculate_speed(distance, time_diff)
print("Speed:", speed, "pixels per second")
start_time = None
object_passed_left = False
object_passed_right = False
cv2.imshow('Frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()