-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
173 lines (122 loc) · 8.38 KB
/
Main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Main.py
import cv2
import numpy as np
import os
import time
import matplotlib.pyplot as plt
import DetectChars
import DetectPlates
from PIL import Image
import PossiblePlate
import pandas as pd
SCALAR_BLACK = (0.0, 0.0, 0.0)
SCALAR_WHITE = (255.0, 255.0, 255.0)
SCALAR_YELLOW = (0.0, 255.0, 255.0)
SCALAR_GREEN = (0.0, 255.0, 0.0)
SCALAR_RED = (0.0, 0.0, 255.0)
showSteps = False
#Accessing data from the json file and converting it into a data frame
#This dataset was aasigned by TCS-HumAIn
dataset = pd.read_json('/home/alim/Desktop/Indian-License-plate-recognition/images/Indian_Number_plates.json', lines=True)
dataset.head()
#Image urls found under the 'content' column which is at index : 2
X = dataset.iloc[:,1].values
#Let's take a random number say 5 and extract the image from the url
image_url = X[5]
#the image is accessed and saved as 'car.jpg' in images
import urllib.request
urllib.request.urlretrieve(image_url, 'images/car.jpg')
from skimage.io import imread
car_image = imread("images/car.jpg", as_grey=True)
# it should be a 2 dimensional array
print(car_image.shape)
def main(car_image):
CnnClassifier = DetectChars.loadCNNClassifier() # attempt KNN training
response = str(input('Do you want to see the Intermediate images: '))
if response == 'Y' or response == 'y':
showSteps = True
else:
showSteps = False
if CnnClassifier == False: # if KNN training was not successful
print("\nerror: CNN traning was not successful\n") # show error message
return # and exit program
imgOriginalScene = cv2.imread(car_image) # open image
plt.imshow(imgOriginalScene)
h, w = imgOriginalScene.shape[:2]
imgOriginalScene = cv2.resize(imgOriginalScene, (0, 0), fx = 1.4, fy = 1.4,interpolation=cv2.INTER_CUBIC)
#imgOriginalScene = cv2.fastNlMeansDenoisingColored(imgOriginalScene,None,10,10,7,21)
#imgOriginal = imgOriginalScene.copy()
if imgOriginalScene is None: # if image was not read successfully
print("\nerror: image not read from file \n\n") # print error message to std out
os.system("pause") # pause so user can see error message
return # and exit program
listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # detect plates. We get a list of
# combinations of contours that may be a plate.
listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates) # detect chars in plates
if showSteps == True:
Image.fromarray(imgOriginalScene,'RGB').show() # show scene image
if len(listOfPossiblePlates) == 0: # if no plates were found
print("\nno license plates were detected\n") # inform user no plates were found
response = ' '
return response,imgOriginalScene
else: # else
# if we get in here list of possible plates has at leat one plate
# sort the list of possible plates in DESCENDING order (most number of chars to least number of chars)
listOfPossiblePlates.sort(key = lambda possiblePlate: len(possiblePlate.strChars), reverse = True)
# suppose the plate with the most recognized chars (the first plate in sorted by string length descending order) is the actual plate
licPlate = listOfPossiblePlates[0]
if showSteps == True:
Image.fromarray(licPlate.imgPlate).show() # show crop of plate and threshold of plate
if len(licPlate.strChars) == 0: # if no chars were found in the plate
print("\nno characters were detected\n\n") # show message
return ' ',imgOriginalScene # and exit program
# end if
drawRedRectangleAroundPlate(imgOriginalScene, licPlate) # draw red rectangle around plate
"""
# Uncomment this if want to check for individual plate
print("\nlicense plate read from ", image," :",licPlate.strChars,"\n")
print("----------------------------------------")
"""
if showSteps == True:
writeLicensePlateCharsOnImage(imgOriginalScene, licPlate) # write license plate text on the image
Image.fromarray(imgOriginalScene).show() # re-show scene image
cv2.imwrite("imgOriginalScene.png", imgOriginalScene) # write image out to file
input('Press any key to continue...') # hold windows open until user presses a key
return licPlate.strChars,licPlate.imgPlate
###################################################################################################
def drawRedRectangleAroundPlate(imgOriginalScene, licPlate):
p2fRectPoints = cv2.boxPoints(licPlate.rrLocationOfPlateInScene) # get 4 vertices of rotated rect. Here, bounding rectangle is drawn with minimum area, so it considers the rotation also
cv2.line(imgOriginalScene, tuple(p2fRectPoints[0]), tuple(p2fRectPoints[1]), SCALAR_RED, 2) # draw 4 red lines
cv2.line(imgOriginalScene, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), SCALAR_RED, 2)
cv2.line(imgOriginalScene, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), SCALAR_RED, 2)
cv2.line(imgOriginalScene, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), SCALAR_RED, 2)
# end function
###################################################################################################
def writeLicensePlateCharsOnImage(imgOriginalScene, licPlate):
ptCenterOfTextAreaX = 0 # this will be the center of the area the text will be written to
ptCenterOfTextAreaY = 0
ptLowerLeftTextOriginX = 0 # this will be the bottom left of the area that the text will be written to
ptLowerLeftTextOriginY = 0
sceneHeight, sceneWidth, sceneNumChannels = imgOriginalScene.shape
plateHeight, plateWidth, plateNumChannels = licPlate.imgPlate.shape
intFontFace = cv2.FONT_HERSHEY_SIMPLEX # choose a plain jane font
fltFontScale = float(plateHeight) / 30.0 # base font scale on height of plate area
intFontThickness = int(round(fltFontScale * 1.5)) # base font thickness on font scale
textSize, baseline = cv2.getTextSize(licPlate.strChars, intFontFace, fltFontScale, intFontThickness) # call getTextSize
# unpack roatated rect into center point, width and height, and angle
( (intPlateCenterX, intPlateCenterY), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg ) = licPlate.rrLocationOfPlateInScene
intPlateCenterX = int(intPlateCenterX) # make sure center is an integer
intPlateCenterY = int(intPlateCenterY)
ptCenterOfTextAreaX = int(intPlateCenterX) # the horizontal location of the text area is the same as the plate
if intPlateCenterY < (sceneHeight * 0.75): # if the license plate is in the upper 3/4 of the image
ptCenterOfTextAreaY = int(round(intPlateCenterY)) + int(round(plateHeight * 1.6)) # write the chars in below the plate
else: # else if the license plate is in the lower 1/4 of the image
ptCenterOfTextAreaY = int(round(intPlateCenterY)) - int(round(plateHeight * 1.6)) # write the chars in above the plate
# end if
textSizeWidth, textSizeHeight = textSize # unpack text size width and height
ptLowerLeftTextOriginX = int(ptCenterOfTextAreaX - (textSizeWidth / 2)) # calculate the lower left origin of the text area
ptLowerLeftTextOriginY = int(ptCenterOfTextAreaY + (textSizeHeight / 2)) # based on the text area center, width, and height
# write the text on the image
cv2.putText(imgOriginalScene, licPlate.strChars, (ptLowerLeftTextOriginX, ptLowerLeftTextOriginY), intFontFace, fltFontScale, SCALAR_YELLOW, intFontThickness)
# end function
###################################################################################################