This Python script demonstrates an Automatic Number Plate Recognition (ANPR) system using OpenCV and Tesseract OCR. It detects and extracts the number plate from an input image and recognizes the characters on the plate.
- OpenCV: A popular computer vision library used for image processing tasks such as reading, resizing, filtering, edge detection, contour detection, and drawing.
- NumPy: A fundamental package for scientific computing with Python, used here for array manipulation.
- imutils: A package providing convenience functions to make basic image processing functions such as resizing, rotating, and displaying images easier with OpenCV.
- pytesseract: A Python wrapper for Google's Tesseract-OCR Engine. It allows for easy integration of OCR capabilities into Python applications.
- Pandas: A powerful data manipulation and analysis library. Here it's used to store the recognized text and current timestamp into a CSV file.
-
Read Image: The script reads the input image 'car_2.jpg' using OpenCV.
-
Preprocessing:
- Resize the image to a width of 500 pixels.
- Convert the image to grayscale.
- Apply bilateral filtering to reduce noise while preserving edges.
- Detect edges using Canny edge detection.
-
Contour Detection:
- Find contours in the edged image.
- Sort contours by area in descending order and select the largest 30 contours.
- Loop through each contour and approximate the shape to a polygon with four vertices, presumably representing the number plate.
-
Number Plate Extraction:
- Create a mask to extract the region of interest (number plate) from the image.
- Apply the mask to the original image.
-
Text Recognition:
- Configure Tesseract OCR.
- Run OCR on the extracted number plate region to recognize the text.
-
Data Storage:
- Store the recognized text along with the current timestamp in a Pandas DataFrame.
-
Output:
- Display the final image with the extracted number plate region.
- Print the recognized text.
- Save the DataFrame to a CSV file named 'data.csv'.
- Make sure you have Python installed on your system along with the necessary libraries: OpenCV, NumPy, imutils, pytesseract, and Pandas.
- Clone the repository or download the script 'code_M1.py'.
- Place an image named 'car_2.jpg' (or others) in the same directory as the script.
- Run the script using
python code_M1.py
. - The recognized number plate text will be printed in the console, and the extracted number plate region will be displayed.
- Ensure that Tesseract OCR is properly installed on your system and its executable is in the PATH.
- Adjust the parameters and configurations as needed for different input images and desired performance.
- The code provided in this repository was referenced from Vehicle-Number-Plate-Reading and was thoroughly understood before being rewritten.
- While the core functionality remains the same, modifications have been made for clarity.
- All credit for the original implementation goes to the author(s) of the referenced repository.