

Do you ever use your camera to its full capability? Have you ever wondered how those fancy cameras at toll booths or parking lots effortlessly read number plates? It's all thanks to Automatic Number Plate Recognition (ANPR). In this blog post, we're about to embark on an exciting exploration of Automatic Number Plate Recognition (ANPR), a technology that's reshaping security and traffic management. We'll be divulging the power of OpenCV for image processing and EasyOCR for text recognition to create a robust ANPR system. By the end of this guide, you'll understand the intricacies of ANPR and how to implement it using Python.
Automatic Number Plate Recognition (ANPR) is a sophisticated technology, and by leveraging the capabilities of EasyOCR, an Optical Character Recognition (OCR) tool, we can create a system that not only detects number plates but also accurately reads the alphanumeric characters on them.
Image Preprocessing: Before feeding the image to EasyOCR, it undergoes preprocessing to enhance the visibility of the number plate. This enhancement includes converting the image to grayscale, applying filters to reduce noise, and using edge detection to highlight the contours of the plate.
Region of Interest (ROI) Extraction: The preprocessed image is then analyzed to identify the region of interest, the area containing the number plate. Techniques like contour detection and masking help isolate this region.
Text Extraction: Once the ROI is isolated, it's passed to EasyOCR for text extraction. EasyOCR processes the image and outputs the recognized text, the number plate's alphanumeric code.
Post-processing: The extracted text may undergo post-processing to correct any errors or format it appropriately. This step ensures the output is clean and ready for use.
Ease of Use: EasyOCR requires minimal setup, making it accessible even to those with limited experience in OCR.
High Accuracy: The deep learning models used by EasyOCR provide high accuracy in text recognition, even in challenging conditions.
Language Support: EasyOCR supports multiple languages across the regions to identify diverse number plate formats.
Speed: EasyOCR is fast and proves to be beneficial for real-time ANPR applications.

Install and Import Dependencies
Before we can start coding, we need to set up our environment. We'll install the necessary libraries and import them into our script:
!pip install opencv-python-headless easyocr import cv2 import easyocr import matplotlib.pyplot as plt
Installing the libraries ensures we have the tools required for image processing (OpenCV) and text recognition (EasyOCR). Importing them allows us to use their functions in our code.
Reading an image
We need an image to work with. Here, we load an image file containing a number plate:
image_path = 'path_to_image.jpg' image = cv2.imread(image_path)
Adding a path to the image to start analyzing it.
Apply Filter and Find Edges For Localization
To make the number plate stand out, we'll apply filters and detect edges
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) bfilter = cv2.bilateralFilter(gray, 11, 17, 17) edged = cv2.Canny(bfilter, 30, 200)
Converting the image to grayscale (gray) simplifies the processing. The bilateral filter (bfilter) smooths the image while preserving edges. The Canny edge detector (edged) helps us identify the sharp transitions that are likely to be the number plate's edges.
Find the Contours and Apply Mask
We'll look for shapes in the image that could be our number plate:
keypoints = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(keypoints) contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
Finding contours (keypoints) helps us identify the boundaries of objects in the image. Sorting them by area (contours) allows us to focus on the largest, most likely candidates for a number plate.
Creating a Blank Mask
We'll create a blank canvas to isolate the number plate:
mask = np.zeros(gray.shape, np.uint8)
A blank mask (mask) is used to isolate the number plate from the rest of the image, making it easier to read the text.
Drawing the Contours
We'll draw the contours on our blank mask to highlight the number plate:
cv2.drawContours(mask, [screenCnt], 0, 255, -1) cv2.bitwise_and(image, image, mask=mask)
Drawing contours (cv2.drawContours) on the mask helps us focus on the area of interest. The bitwise AND operation (cv2.bitwise_and) isolates the number plate by applying the mask to the original image.
Use Easy OCR to Read Text
Now, we'll use EasyOCR to read the characters from the isolated number plate:
reader = easyocr.Reader(['en']) result = reader.readtext(masked)
Recognizing text in images. The readtext function processes the masked image to extract the number plate text.
Render Results: Overlay Results on the Original Image
We'll display the recognized text on the original image:
text = result[0][-2] font = cv2.FONT_HERSHEY_SIMPLEX res = cv2.putText(img, text=text, org=(approx[0][0][0], approx[1][0][1]+60), fontFace=font, fontScale=1, color=(0,255,0), thickness=2, lineType=cv2.LINE_AA) res = cv2.rectangle(img, tuple(approx[0][0]), tuple(approx[2][0]), (0,255,0),3) plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
Overlaying the text (cv2.putText) and drawing a rectangle (cv2.rectangle) around the number plate helps visualize the results. Displaying the image with plt.imshow allows us to see the final output.
