-
Notifications
You must be signed in to change notification settings - Fork 0
/
det.py
45 lines (33 loc) · 1.2 KB
/
det.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
import cv2
# Load the pre-trained Haar Cascade classifier for cars
car_cascade = cv2.CascadeClassifier('haarcascade_cars.xml')
# Function to detect cars in a frame
def detect_cars(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=1, minSize=(30, 30))
for (x, y, w, h) in cars:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
return frame
def main():
# Open a connection to the webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
return
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
# Detect cars in the frame
frame = detect_cars(frame)
# Display the frame with detected cars
cv2.imshow('Car Detection', frame)
# Break the loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()