OpenCV Python Project for Bus Detection From an Image¶
In this tutorial, we’ll code a system for counting and detecting vehicles. It will be sufficient to function for both still images and moving pictures. For the same, we’ll be using OpenCV for carrying out all image processing operations and for detecting and counting cars and buses using a haar cascade classifier. However, you can also create your own haar cascade classifier.
Project Scope¶
- Helps traffic police:
- Maintaining records:
- Traffic surveillance control:
Importing required libraries¶
from PIL import Image
import cv2
import numpy as np
import requests
Next, we will fetch the image from the internet that we will be working on. We will then resize the image and convert it into a NumPy array
Reading image from URL¶
image = Image.open(requests.get('https://i.ytimg.com/vi/AkLfPjxlyfw/maxresdefault.jpg', stream=True).raw)
image = image.resize((350,225))
image_arr = np.array(image)
image
To get better output, we will perform some transformations on the image. Here we will convert the image into grayscale.
Grayscale Image¶
grey = cv2.cvtColor(image_arr,cv2.COLOR_BGR2GRAY)
Image.fromarray(grey)
After that, we’ll use GaussianBlur to clean out the image’s noise. One image processing approach is gaussian blur. It is frequently used in graphics design as well to reduce noise and smooth the image so that further preprocessing would produce better results. Gaussian blur technique also reduces the image’s details while minimising image noise. Here, we’ll use the GaussianBlur function() to implement this preprocessing method
Gaussian Blur¶
blur = cv2.GaussianBlur(grey,(5,5),0)
Image.fromarray(blur)
**Here we will dilate the image. Dilation is one of the morphological techniques where we try to fill the pixels with the element, also known as kernels (structured pieces), to fill the missing parts of the images whenever needed.***
- Note: It is just the opposite of erosion.
Dilation¶
dilated = cv2.dilate(blur,np.ones((3,3)))
Image.fromarray(dilated)
Now we will perform a Morphology transformation with the kernel. Here we are using a morphology-Ex technique that tells the function on which image processing operations need to be done. The second argument is about what operations must be done, and you may need elliptical/circular shaped kernels. To implement the morphology-Ex method using OpenCV we will be using the get structuring element method.
Morphology-Ex, structuring element¶
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
closing = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE, kernel)
Image.fromarray(closing)
Detecting cars using car cascade¶
Here we will use the CascadeClassifier function, the predefined function of OpenCV, to train the images from the pre-trained XML file (cars.xml). We need to detect multiple objects, i.e. cars, to use detectMultiScale.
car_cascade_src = 'Bus_front.xml'
car_cascade = cv2.CascadeClassifier(car_cascade_src)
cars = car_cascade.detectMultiScale(closing, 1.1, 1)
cars
array([[144, 48, 86, 86]], dtype=int32)
We will use the above-returned contours and draw a rectangle around detected cars. Here we will see that it will create the rectangle with a red boundary around every car it detects.
cnt = 0
for (x,y,w,h) in cars:
cv2.rectangle(image_arr,(x,y),(x+w,y+h),(255,0,0),2)
cnt += 1
print(cnt, " cars found")
Image.fromarray(image_arr)
1 cars found