Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Count number of Faces using Python - OpenCV
Next article icon

Vehicle detection is an important application of computer vision that helps in monitoring traffic, automating parking systems and surveillance systems. In this article, we’ll implement a simple vehicle detection system using Python and OpenCV using a pre-trained Haar Cascade classifier and we will get a video in which vehicles will be detected and it will be represented by a rectangular frame around it.

Step 1: Installing OpenCV Library

We need to install theOpenCVlibrary or its implementation. To install it in our system we can use the below pip command:

pip install opencv-python

Step 2: Importing Required Libraries

We will using OpenCV for vehicle detection and handling video streams. Also we need time module to add delays after detecting a vehicle.

Python
importcv2importtime

Step 3: Loading Haar Cascade Classifier and Video

We need to downlaod the Haar Cascade files (download it fromhere) and load the Haar Cascade classifier that’s trained to detect vehicles. Also we have to load the video file (download it fromhere) from which vehicles will be detected. Now to capture a video we need to create a VideoCapture() object.

Python
haar_cascade='cars.xml'car_cascade=cv2.CascadeClassifier(haar_cascade)ifcar_cascade.empty():raiseException("Error loading Haar cascade file.")video='car_video.avi'cap=cv2.VideoCapture(video)ifnotcap.isOpened():raiseException("Error opening video file.")

Step 4: Detecting Vehicles in Video Frames

We read each frame of the video, convert it to grayscale and use the Haar Cascade classifier to detect vehicles. Once a vehicle is detected we draw rectangles around it.

  • cars = car_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3): Detects vehicles in the grayscale image using the Haar Cascade classifier.
Python
car_detected=Falsedetection_time=Nonewhilecap.isOpened():ret,frames=cap.read()ifnotret:breakgray=cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY)cars=car_cascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=3)iflen(cars)>0andnotcar_detected:car_detected=Truedetection_time=time.time()print("Car detected!")ifcar_detectedandtime.time()-detection_time>5:breakfor(x,y,w,h)incars:cv2.rectangle(frames,(x,y),(x+w,y+h),(0,0,255),2)cv2.imshow('video',frames)ifcv2.waitKey(33)==27:break

Step 5: Release Video and Close Windows

After processing the frames we release the video capture object and close any OpenCV windows.

Python
cap.release()cv2.destroyAllWindows()

Output:

VIDEO-DETECTION-USING-OPENCV-
Vehicles detected

Vehicle detection systems can be further enhanced by integrating more advanced techniques such as deep learning-based models which offer better accuracy and robustness in complex environments. By using frameworks like TensorFlow or PyTorch vehicle detection systems can achieve real-time performance and handle various challenges.

Note : Above code will not run on online IDE. It will work on local system only.
You can download source code fromhere.

For more articles on detections using Haar Cascade -Face Detection Basics


OpenCV Python Program For Vehicle Detection in a Video Frame
Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp