In this article, I will guide you on how to do in python using the library and trained cascade classifier in just a few lines of code. real-time vehicle detection OpenCV a brief about vehicle detection is one of the many application of , whereby focuses on detecting cars within an image together with the location coordinates. Real-time vehicle detection object detection where is being used? vehicle detection together with road detection is heavily applied on , for a car to navigate safely along the road, it has to know where other cars are positioned so as it can avoid a collision. self-driving cars Also vehicle detection is being used in traffic surveillance systems in such a way it can detect the traffic based on number vehicle and use that data to manage and control it what will you build? In this tutorial, we will learn how to perform Real-time vehicle detection in a using OpenCV and a pre-trained cascade model. video or from camera streams Requirements To able to follow through with this tutorial you’re supposed to have the following on your machine OpenCV Cars Cascade model Demo Video installation $ pip install opencv-python Pre-trained Cascade classifier As I have explained earlier, we are not going to be training our model to spot cars in video frames from scratch instead we gonna use a pre-trained one. These trained are usually being stored in the format, therefore you should download the cascade that was trained to detect cars and have it in the project directory. cascade classifiers XML To download the trained cascade mode click-here Demo video with cars in it You can actually use any video you want as long it has cars in it, the cascade model will be able to detect them. If you would like to use the same video I used for this article ; Download here Project Directory Your project directory should look like this . ├── app.py ├── cars.mp4 └── haarcascade_car.xml Let's get hands dirty Now let's begin by building what we have just talked about, using you have the XML Model and demo video on your project directory. Loading our Model use to load the trained model as shown in the code below. cv2.CascadeClassifier() haarcascade cv2 cars_cascade = cv2.CascadeClassifier( ) import 'haarcascade_car.xml' Detecting cars in a video we will use the method to detect and to get the of vehicles in the video frames. detectMultiScale () coordinates The method receives 3 parameters to actually give your coordinates as shown below detectMultiScale () Grayscale image scaleFactor minNeighbors specify the image to be processed, in our case a grayscale image is going to be image fetched from the video streams. Grayscale image specify how much the image size is reduced at each image scale, you can learn more about it here, a good value is mostly chosen as ScaleFactor 1.05 specify how many neighbors each candidate rectangle should have to retain it, this parameter will affect the quality of the detected faces. minNeighbors A higher value results in fewer detections but with higher quality usually, 3-6 is a good value for it Syntax to detect cars + their positional coordinates cars = cars_cascade.detectMultiScale(frame, scaleFactor, minNeighbors) When you run the above line of code it will perform in the and then return to us all coordinates of cars found (diagonal coordinates point). cars detection frame image Drawing rectangle around detected cars After detecting all the coordinates of all the cars in a frame, we to draw a rectangle around it for us to able to see the detection process visually. We will use the method to draw a around every detected car using returned by our cascade classifier. cv2.rectangle() rectangle diagonal coordinate points Syntax to use the cv2.rectangle () method cv2.rectangle(frame , point1, point2, color = (), thickness=value) building a function to do all the detection process We need to condense what we just learned and put into a single function that receives image frames and then draws rectangles around it using the detected coordinates just as shown below. def detect_cars(frame): cars = cars_cascade.detectMultiScale(frame, , ) (x, y, w, h) cars: cv2.rectangle(frame, (x, y), (x+w,y+h), color=( , , ), thickness= ) frame 1.15 4 for in 0 255 0 2 return Building a function to simulate the detection process finally let's add a single function to simulate the whole process from loading the video, to perform vehicle detection by calling the detect_cars function, and then render a frame with detected vehicles on the screen. def Simulator(): CarVideo = cv2.VideoCapture( ) CarVideo.isOpened(): ret, frame = CarVideo.read() controlkey = cv2.waitKey( ) ret: cars_frame = detect_cars(frame) cv2.imshow( , cars_frame) : controlkey == ord( ): CarVideo.release() cv2.destroyAllWindows() 'cars.mp4' while 1 if 'frame' else break if 'q' break Add these two lines so as we make sure that we are running our python code as a script. __name__ == : Simulator() if '__main__' Let's bundle everything together Now we know how to do each independent piece of our detection script, it's time to put them together so as we can run it. Once you put all the concept we learned above into one , your code is going to look just as shown below app.py cv2 cars_cascade = cv2.CascadeClassifier( ) def detect_cars(frame): cars = cars_cascade.detectMultiScale(frame, , ) (x, y, w, h) cars: cv2.rectangle(frame, (x, y), (x+w,y+h), color=( , , ), thickness= ) frame def Simulator(): CarVideo = cv2.VideoCapture( ) CarVideo.isOpened(): ret, frame = CarVideo.read() controlkey = cv2.waitKey( ) ret: cars_frame = detect_cars(frame) cv2.imshow( , cars_frame) : controlkey == ord( ): CarVideo.release() cv2.destroyAllWindows() __name__ == : Simulator() import 'haarcascade_car.xml' 1.15 4 for in 0 255 0 2 return 'cars.mp4' while 1 if 'frame' else break if 'q' break if '__main__' We have reached the end of our article, hope you learned something, now share it with your fellow friends on and other developer communities Twitter The can be found on original article kalebujordan.com Follow me on Twiter I recommend you to also check this; How to convert picture to sound in Python Build a Real-time barcode reader in Python Getting started with image processing using a pillow How to detect Edges in a picture using OpenCV Canny algorithm How to perform text classification using TensorFlow in python How to perform sentiment analysis on Twitter data