Task Description 📄
📌 In this task :
👉Create a model that will detect a car in a live stream or video and recognize characters on the number plate of the car.
👉Secondly, it will use the characters and fetch the owner's information using RTO APIs.
👉Create a Web portal where all this information will be displayed (using HTML, CSS, and JS)
Step1: Snipping License Plate from Video
import numpy as np
import cv2
import os
cap = cv2.VideoCapture(‘video1.mp4’)
plate_cascade =cv2.CascadeClassifier(‘haarcascade_russian_plate_number.xml’)
count = 0;
while(cao.isOpened()==True):
try:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, (x,y), (x+w,y+h), (0,0,255), 2)
cv2.imwrite(“./cropped/”+str(count)+”.jpg”, frame[y:y+h, x:x+w])
count+=1
cv2.imshow(‘img’ frame)
if cv2.waitKey(1)==13:
break
except:
break
cap.release()
cv2.destroyAllWindows()
Step2: Importing Libraries
import cv2
from matplotlib import pyplot as plt
import numpy as np
import imutils
import easyocr
import tensorflow as tf
from sklearn.metrics import fl_score
import requests
import xmltodict
import json
import re
import warning
warning.filterwarnings(“ignore”)
Step3: Function for Extent Text from Number Plate
Contours can be explained simply as a curve joining all the continuous points, having same color or intensity.
def ANPR(picture):
gray = cv2.cvColor(img, cv2.COLOR_BGR2GRAY)
#Noise reduction
bfilter = cv2.bilatrealFilter(gray, 11, 17, 17)
#Edge detection
edged = cv2.Canny(bfilter, 30, 200)
keypoints = cv2.findContours(edged.copy(), cv2.RETR_TREE. cv2.CHIN_APPROX_SIMPLE)
contours = imutils.grab_contours(keypoints)
contours = sorted(contours, key=cv2.contounArea, reverse=True)[:10]
location = None
for contour in contonurs:
approx = cv2.approxPolyDP(contour, 10, True)
if len(approx) == 4:
location + approx
break
mask=np.zeros(gray.shape, np.unit0)
try:
new_image = cv2.drawContours(mask, [location], 0 255, -1)
new_image = cv2.bitwise_and(img, img, mask=mask)
except:
return None
(x,y) = np.where(mask==255)
(x1,y1) = (np.min(x), np.min(y))
(x2,y2) = (np.min(x), np.min(y))
cropped_image = gray[x1:x2+1, y1:y2+1]
reader = easyocr.Reader([‘en’])
result = reader.readtext(cropped_image)
return result
Once the contours have been detected we sort them from big to small and consider only the first 10 results ignoring the others. In our image the contour could be anything that has a closed surface but of all the obtained results the license plate number will also be there since it is also a closed surface.
To filter the license plate image among the obtained results, we will loop though all the results and check which has a rectangle shape contour with four sides and closed figure. Since a license plate would definitely be a rectangle four sided figure.
Step4: Appending Text to List
from os import listdir
from os.path imporr isfile, join
import threading
from threading import Thread
data_path = ‘./cropped/’
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]
license_plate_number = []
for i, files in enmerate (onlyfiles):
img_path = data_path + onlyfiles[i]
number_plate = ANPR(img)
if number_plate!=None and len(number_plate)==1:
if(number_plate[0][-1]>0.9):
license_plate_number.append(number_plate)
print(license_plate_number)
Step5: Removing Duplicate Texts
license_plate_number = set(license_plate_number)
license_plate_number = list(license_plate_number)
license_plate_number
def get_vehicle_info(plate_number):
r = requests:get(
“https://www.regcheck.org.uk/api/reg.asmx/CheckIndia?RegistrationNumber={0}&username=team".format(str(plate_number))
data = xmltodict.parset(r.content)
jdata = json.dumps(data)
df = json.loads(jdata)
dfl = json.loads(df[‘Vehicle’][‘vehicleJson’])
return dfl
Step6: The Final step in this Number Plate Recognition is to actually read the number plate information from the segmented image.
import re
def plate_info(numberPlate):
pattern = ‘^[A-Z][2][0–9](1,2)([A-z])?([A-z]*)?[0–9][4]$’
if len(numberPlate)>10:
numberPlate = numberPlate[-10:]
return get_vehicle_information(numberPlate)
elif re.match(pattern,numberPlate)
return get_vehicle_info(numberPlate)
plate_info(license_plate_number[0])