Aaasbm Python PROJECT Arya Sir (1) ... Document
Aaasbm Python PROJECT Arya Sir (1) ... Document
SYSTEM
ASBM UNIVERSITY
SHIKSHA VIHAR, BHOLA, CHANDAKA, BHUBANESWAR-754012, ODISHA
Submitted By:-
JYOTISANKAR ROUT
REDG.NO:-24MCA12
PROGRAMME: MCA
SEMESTER: 2nd , BATCH: 2024-26
Submitted To:
Prof. ARYA SUMAN PATTNAIK
Assistant Professor
SIGNATURE
PROJECT-1: Automatic License Plate
Recognition using OpenCV
in Python .
Demo Image
Step 4: Find contours and apply mark to separate out actual number plate
If you are wondering what are contours, Contours can be understood as the boundaries of
a shape with the same intensity in an image. They are used in image analysis to identify
objects in an image or separate an object from the background. The cv2.findContours()
function is used to detect these contours in binary images.
First line of the given snippet will find all the contours in the provided image. then
based on the tuple value of those contours are stored in contours variable. after that
sorting is performed to based on contours area and top 10 contours are finalized for
further processing.
This code is will find a contour that approximates a polygon with four sides, which could
potentially be the license plate in an Automatic Number Plate Recognition (ANPR)
system. We are looping over top 10 contours and finding best fit for number plate. We
are checking whether any contour have potentially 4 sides, because our number plate
have 4 sides and if found it could be our number plate.
#Loop over our contours to find the best possible approximate contour of
10 contours
location = None
for contour in contours:
approx = cv2.approxPolyDP(contour, 10, True)
if len(approx) == 4:
location = approx
Break
Output:
Location: [[[ 85 137]]
[[178 145]]
[[182 126]]
[[ 89 118]]]
Here, you can see we are getting the co-ordinates of number plate as output. After this
our next step will be masking out only area which include number plate so that later when
we are going to extract text from it using OCR, we can do it efficiently. To mask out we
will execute the following code.
Output:
Now, here comes the part where we are going to crop the number plate region from the
image (contour). below code will help to achieve this.
The above code will identify the region in the image where the license plate is located i.e.
where the mask is white and crops the image to this region. It uses np.where() function to
find the coordinates of all white pixels in the mask, and np.min() and np.max() to find the
minimum and maximum x and y coordinates, respectively. These represent the bounding
box of the license plate in the image. Lastly the image is then cropped to this bounding
box using numpy array slicing, resulting in an image that only contains the license plate.
Step 5: Extract text from images using OCR
Now it is crucial step in ANPR to convert image into text. This step will help us to use
the number plate data. we can store the data on number plate on database and use it
later for number of applications, like automatic toll or automatic parking charges, etc.
Following code will convert the image into text using EasyOCR library.
reader = easyocr.Reader(['en'])
#create an easyocr reader object with english as
the language
result = reader.readtext(cropped_image)
#read text from the cropped image
result
Output: [([[0, 0], [98, 0], [98, 28], [0, 28]], 'HR.26 BR 9044', 0.8064767816895628)]
Step 6: Display the final output
Now we got the text from the number plate it is time to show it on original image. code
will extracts the recognized text from the OCR result, then uses OpenCV to draw this text
and a rectangle around the license plate on the original image. The text is positioned just
near the license plate and the rectangle is drawn around the license plate based on its
approximated location. The final image, with the overlaid text and rectangle, is then
displayed.
text = result[0][-2] #Extract the text from the result
font = cv2.FONT_HERSHEY_SIMPLEX #Font style
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) #put the text on the image
res = cv2.rectangle(img, tuple(approx[0][0]), tuple(approx[2][0]),
(0,255,0),3)
#Draw a rectangle around the text
Conclusion:-
This is how we perform Automatic License/Number Plate Recognition (ANPR) using
python. we performed filters on the images, them mask out after finding contours. then
using EasyOCR we detected the text which was provided as output