In this Python tutorial, we will write a Python program for vehicle detection with OpenCV. The OpenCV (cv2) module supports computer vision and deep learning. The objective of this vehicle driving Python tutorial is detection of a vehicle in video frames. In addition, the vehicles will be tracked within each frame.
Vehicle Detection Advantages
Vehicle detection reliability offers advantages for site safety and traffic control. Identifying the perfect technology for your requirements can be challenging for a vehicle detection application. Some of the factors that can play a major role in the success of a vehicle detection system are application environment (indoors or outdoors) , sensor mounting, sensing range, and target size.
Vehicle Detection OpenCV Breakdown
In the below, I will break down sections of the OpenCV vehicle detection code. The focus of the code is to capture the frames from the video and train the XML classifier for some features of objects that we would like to detect. In this example, the objects are vehicles.
Import cv2 and Create Variables for Video and XML
In the below code block, we are importing the cv2 module, capturing the video frames, and training the XML classifiers that describe the vehicle objects that will need to detect. In addition, we create a c variable with the number 1 to count every frame.
Install OpenCV-Python Module
If you need to install the OpenCV-Python module, run: pip install opencv-Python
cap = cv2.VideoCapture('C:\path\to\video\highway_vehicles.avi')
car_cascade = cv2.CascadeClassifier('C:\path\to\XML\cars.xml')
c = 1
Video Frames Capturing while loop
The below while loop will be used to capture each frame of the video. The while loop will complete the list below to ensure each frame is captured and vehicles are detected with a rectangle around each.
- while loop captures
- Read each video frame
- Each frame conversion to gray scale
- Detect different size vehicles in each frame
- Each detected vehicle will have a rectangle
- Save every 10th frame in the video
- Each frame image will be 340 X 220 pixels
#while loop captures
# reads frames from a video
ret, frames = cap.read()
#each frame conversion to gray scale
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
#detects vehicle of different sizes in the input image
cars = car_cascade.detectMultiScale(gray, 1.1, 1)
#each detected vehicle will have a rectangle
for (x, y, w, h) in cars:
cv2.rectangle(frames, (x, y), (x + w, y + h), (0, 0, 255), 2)
#save every 10th frame in the video
#each frame must be resized to 340 x 20 pixels
if c <= 680:
if c % 20 == 0:
success, image = cap.read()
resize = cv2.resize(frames, (340, 220), interpolation=cv2.INTER_LINEAR)
cv2.imwrite("%03ddetection.jpg" % c, resize)
c += 1
#k = cv2.waitKey(0)
if cv2.waitKey(1) & 0xFF == ord('q'):
print("Video detection halted.")
print("Video is completed.")
Once the above code is consolidated into a single script it will produce single frames images. Below is a sample collage of a few frames from the video for vehicle detection.