random

In societies and commercial spaces it’s difficult to manage the manual entries of vehicles. In order to solve this problem, we’ve developed a system which can read number plate.

We need to build a system which is capable of:

• Taking image/ video process it and extract the number
• We need a raspberry pi with camera module
• Software side we’re using Python, OpenCV, Numpy and Tesseract

In this blog we will be focusing on Number Plate Recognition from an image

Installation :

Numpy

• pip install numpy

OpenCV

• pip install opencv-python==4.1.0

Tesseract

• Install tesseract using windows installer available at: https://github.com/UB-Mannheim/tesseract/wiki
• Note the tesseract path from the installation. Find the tesseract.exe path you may find it in either C:/Program Files/Tesseract-OCR/tesseract.exe or C:/Users/USER/AppData/Local/Tesseract-OCR
• pip install pytesseract
• Note: Set the tesseract path in the script before calling image_to_string: pytesseract.pytesseract.tesseract_cmd = '<Your Path>\Tesseract-OCR\tesseract.exe'

Image for illustration:

Here we’re using an Indian number plate as a sample.

Code:

Explaination:

1. We read the image of a car into a variable using opencv. The image is resized according to needs. It is converted to gray scale image to decrease computations. Bilateral filter is added to reduce the noise in the background. We then find the edges in the image using Canny edges.

img = cv2.resize(img, (620,480) )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)

2. We find all the contours (connected components) in the edged image and sort them by their area. We iterate through the contours until we get one with approximately 4 corners (as number plates are rectangular in shape). We then mask the other parts of the image except for the number plate and pass it for character recognition.

• # find contours in the edged image, keep only the largest
# ones, and initialize our screen contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
• # loop over our contours
• for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)

# if our approximated contour has four points, then
# we can assume that we have found our screen
if len(approx) == 4:
screenCnt = approx
break

if screenCnt is None:
detected = 0
print("No contour detected")
else:
detected = 1

if detected == 1:
cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 3)

# Masking the part other than the number plate

3. Cropping unnecessary image. Only taking in consideration the Number Plate.

• (x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx+1, topy:bottomy+1]

4. Using Tesseract and converting image to String. This gives us the number on the number plate which is stored in Text variable.

If you are still facing issue regarding this topic Feel free to Ask Doubts in the Comment Box Below and Don't Forget to Follow us on 👍 Social Networks😉
Vehicles Number Plate Recognition Using IoT Reviewed by Avotrix.Author on Wednesday, November 04, 2020 Rating: 5