Task 08 💻

Rupesh Jadhav
3 min readSep 7, 2021

Object Recognition using CNN model

Task Description 📄

📌 In this task :

Create a model that will detect a car in a live stream or video and recognize characters on number plate of the car .

👉Secondly , it will use the characters and fetch the owners information using RTO API’s .
👉Create a Web portal where all this information will be displayed (using html,css,and js)

import cv2from matplotlib import pyplot as pltimport numpy as npimport imutilsimport easyocr

1. Read in image, Grayscale and Blur

img = cv2.imread('image4.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(cv2.cvtColor(gray, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x1e83664c700>

2. Apply filter and find edges for localization

bfilter = cv2.bilateralFilter(gray , 11, 17, 17) #noise reduction
edged = cv2.Canny(bfilter, 30, 200) #edge detection
plt.imshow(cv2.cvtColor(edged, cv2.COLOR_BGR2RGB))

<matplotlib.image.AxesImage at 0x1e8372a6a30>

3. Find Contours and Apply Mask

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]
location = None
for contour in contours:
approx = cv2.approxPolyDP(contour, 10, True)
if len(approx) == 4:
location = approx
break
locationarray([[[300, 540]],

[[306, 589]],

[[543, 592]],

[[538, 543]]], dtype=int32)
mask = np.zeros(gray.shape, np.uint8)
new_image = cv2.drawContours(mask, [location], 0, 255, -1)
new_image = cv2.bitwise_and(img, img, mask=mask)
plt.imshow(cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB))<matplotlib.image.AxesImage at 0x1e801a50a30>(x,y) = np.where(mask==255)
(x1, y1) = (np.min(x), np.min(y))
(x2, y2) = (np.max(x), np.max(y))
cropped_image = gray[x1:x2+1, y1:y2+1]
plt.imshow(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB))<matplotlib.image.AxesImage at 0x1e801852c70>

4. Use Easy OCR To Read Text

reader = easyocr.Reader(['en'])
result = reader.readtext(cropped_image)
result
CUDA not available - defaulting to CPU. Note: This module is much faster with a GPU.[([[0, 0], [244, 0], [244, 53], [0, 53]], 'H982 FKL', 0.9769778047590311)]

5. Render Result

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,-1), thickness=2, lineType=cv2.LINE_AA)
res = cv2.rectangle(img, tuple(approx[0][0]), tuple(approx[2][0]), (0,255,-1),3)
plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x1e835bad730>text'H982 FKL'

CAR DETAILS- Python

import cgi
import subprocess
import time

print(“content-type: text/html”)
print()

# FieldStrorage is a function to take input
f = cgi.FieldStorage()
plate_no = f.getvalue(“x”)

if plate_no == “KA 05 NB 1786”:
print(“<body style=’padding: 40px;’>”)
print(‘<h1 style=”color:lightblue;” >VECHICLE DETAILS</h1>’)
print(‘’’<pre>
Registration Name : Rupesh Jadhav
License No : 12345677889
Make / Model : BABA SUZUKI/ ERTIGA
Registration Date : 1/12/2011
Registering Authority : MAHARASTRA, INDIA
Vehicle Class : MCWG
Fuel Type : CNG
Engine No : IVK3N1734538
Chassis No : HMSURVWVQSVWE
Insurance Upto : 5/13/2022
Fitness Upto : 4/8/2030
</pre>’’’)
print(“</body>”)

elif plate_no == “MH:01.CT.5470”:
print(“<body style=’padding: 40px;’>”)
print(‘<h1 style=”color:yellow;” >VECHICLE DETAILS</h1>’)
print(‘’’<pre>
Registration Name : Jack Ryder
License No : 098363357292
Make / Model : HONDA / CITY
Registration Date : 1/12/2014
Registering Authority : Berlin
Vehicle Class : MCWG
Fuel Type : CNG
Engine No : IVK3N1734538
Chassis No : HMSURVWVQSVWE
Insurance Upto : 5/13/2022
Fitness Upto : 4/8/2030
</pre>’’’)
print(“</body>”)

elif plate_no == “MH 12 HZ5641 ”:
print(“<body style=’padding: 40px;’>”)
print(‘<h1 style=”color:#df405a;” >Output</h1>’)
print(‘’’<pre>
Registration Name : Harry Potter
License No : 735382528936
Make / Model : MARUTI SUZUKI / ALTO800
Registration Date : 1/12/2014
Registering Authority : MANDSAUR, INDIA
Vehicle Class : MCWG
Fuel Type : CNG
Engine No : IVK3N1734538
Chassis No : HMSURVWVQSVWE
Insurance Upto : 5/13/2022
Fitness Upto : 4/8/2030
</pre>’’’)
print(“</body>”)

else:
print(“<body style=’padding: 40px;’>”)
print(‘<h1 style=”color:#df405a;” >Output</h1>’)
print(“No data available for this number…”)
print(“</body>”)

--

--

Rupesh Jadhav

ML Learner and Computer Vision/ AWS / DevOps Tools / Cloud Computing /