-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
296 lines (233 loc) · 12.3 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# USAGE
# python Main.py --image Sample/s1.jpg @ untuk file gambar
# python Main.py --video Sample/sv1.mp4 @ untuk file video
# python Main.py @ untuk cam
import imutils
import argparse
import cv2
import numpy as np
import Preprocess as pp
import os
import time
import math
import Calibration as cal
import DetectChars
import DetectPlates
import PossiblePlate
# Module level variables for image ##########################################################################
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)
VERIF = 8 # number for verification the plate license
showSteps = True
# Main ##################################################################################################
def main():
# argument for input video/image/calibration
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
help = "path to video file")
ap.add_argument("-i", "--image",
help = "Path to the image")
ap.add_argument("-c", "--calibration",
help = "image or video or camera")
args = vars(ap.parse_args())
if args.get("calibration", True):
imgOriginalScene = cv2.imread(args["calibration"])
if imgOriginalScene is None:
print(" Please check again the path of image or argument !")
imgOriginalScene = imutils.resize(imgOriginalScene, width = 720)
cal.calibration(imgOriginalScene)
return
if args.get("video", True):
camera = cv2.VideoCapture(0)
if camera is None:
print(" Please check again the path of video or argument !")
loop = True
elif args.get("image", True):
imgOriginalScene = cv2.imread(args["image"])
if imgOriginalScene is None:
print(" Please check again the path of image or argument !")
loop = False
else:
camera = cv2.VideoCapture(0)
loop = True
# add knn library for detect chars
blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training
if blnKNNTrainingSuccessful == False:
print("\nerror: KNN traning was not successful\n")
return
count = 0
license = []
VER = np.zeros(VERIF)
for x in VER:
license.append("")
numlicense = ""
knn = 0
car_cascade = cv2.CascadeClassifier('car.xml')
# Looping for Video
while (loop):
# grab the current frame
(grabbed, frame) = camera.read()
if args.get("video") and not grabbed:
break
# resize frame
imgOriginalScene = imutils.resize(frame, width = 620)
#cv2.imshow("imageori", imgOriginalScene)
x = imgOriginalScene.copy()
#=======================================================
#=======================================================
# Panggil preprocess untuk dapet grayscale dan threshold
imgGrayscale, imgThresh = pp.preprocess(imgOriginalScene)
imgGrayscale2, imgThresh2 = pp.preprocess(x)
# Proses utama deteksi
# Hasil berupa frame dan nomor polisi
imgOriginalScene, licenses = searching(imgOriginalScene,loop)
# only save 5 same license each time
license[count+1] = licenses
if (license[count] == license[count+1]):
license[count]=license[count+1]
count = count + 1
elif (license[count] != license[count+1]):
coba = license[count+1]
count = 0
license[count] = coba
if count == (VERIF-1):
if (license[VERIF-1] == ""):
print("no characters were detected\n")
else:
#if number license same, not be saved
if numlicense == license[VERIF-1]:
print("still = " + numlicense + "\n")
else:
numlicense = license[VERIF-1]
print("A new license plate read from image = " + license[VERIF-1] + "\n")
cv2.imshow(license[VERIF-1], imgOriginalScene)
namefile = "hasil/"+ license[VERIF-1] + ".png"
cv2.imwrite(namefile, imgOriginalScene)
count = 0
#print(license)
# re-show scene image
#imgOriginalScene = cv2.blur(imgOriginalScene,(12,12))
#cv2.putText(imgOriginalScene,"Press 's' to save frame to be 'save.png', for calibrating",(10,30),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255,255,255),1,bottomLeftOrigin = False)
#drawRedRectangleAroundPlate(imgOriginalScene, imgOriginalScene)
#cv2.rectangle(imgOriginalScene,((imgOriginalScene.shape[1]/2-230),(imgOriginalScene.shape[0]/2-80)),((imgOriginalScene.shape[1]/2+230),(imgOriginalScene.shape[0]/2+80)),SCALAR_GREEN,3)
result = imgOriginalScene
cv2.imshow("result", result)
#cv2.imshow("ori", frame)
key = cv2.waitKey(5) & 0xFF
if key == ord('s'):
knn = str(knn)
savefileimg = "calib_knn/img_" + knn + ".png"
savefileThr = "calib_knn/Thr_" + knn + ".png"
# cv2.saveimage("save.png", imgOriginalScene)
cv2.imwrite(savefileimg, frame)
cv2.imwrite(savefileThr, imgThresh)
print("image save !")
knn = int(knn)
knn = knn + 1
if key == 27: # if the 'q' key is pressed, stop the loop
break
camera.release() # cleanup the camera and close any open windows
# For image only
if (loop == False):
imgOriginalScene = imutils.resize(imgOriginalScene, width = 720)
#cv2.imshow("original",imgOriginalScene)
imgGrayscale, imgThresh = pp.preprocess(imgOriginalScene)
#cv2.imshow("threshold",imgThresh)
cv2.imshow("grayscale",imgGrayscale)
imgOriginalScene,license = searching(imgOriginalScene,loop)
#imgOriginalScene = imutils.detransform(imgOriginalScene)
cv2.waitKey(0)
cv2.waitKey(0)
cv2.destroyAllWindows()
return result
# end main
###################################################################################################
def drawRedRectangleAroundPlate(imgOriginalScene, licPlate):
p2fRectPoints = cv2.boxPoints(licPlate.rrLocationOfPlateInScene) # get 4 vertices of rotated rect
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
# Proses utama deteksi pelat nomor
def searching(imgOriginalScene,loop):
licenses = ""
if imgOriginalScene is None: # if image not read successfully
print("error: image not read from file \n") # print error message
os.system("pause")
return
# end if
listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # Deteksi kemungkinan plat nomor
#time.sleep(0.1)
listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates) # Deteksi karakter pada plat nomor
#time.sleep(0.1)
if (loop == False):
cv2.imshow("imgOriginalScene", imgOriginalScene)
if len(listOfPossiblePlates) == 0:
if (loop == False): # if no plates were found
print("no license plates were detected\n") # inform user no plates were found
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 (loop == False):
#cv2.imshow("imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate
#cv2.imshow("imgThresh", licPlate.imgThresh)
if len(licPlate.strChars) == 0: # if no chars were found in the plate
if (loop == False):
print("no characters were detected\n")
return # show message
# end if
drawRedRectangleAroundPlate(imgOriginalScene, licPlate)
writeLicensePlateCharsOnImage(imgOriginalScene, licPlate)
print("ini coy", licPlate.strChars)
licenses = licPlate.strChars
#if ((licenses[0] and licenses[len(licenses)-1]) == ('0' or '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9')):
# licenses = ""
# print("license plate False !! \n and ")
# draw red rectangle around plate
#print (licenses)
#print(licPlate)
if (loop == False):
print("license plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out
# write license plate text on the image
if (loop == False):
cv2.imshow("imgOriginalScene", imgOriginalScene) # re-show scene image
cv2.imwrite("imgOriginalScene.png", imgOriginalScene)
return imgOriginalScene, licenses
###################################################################################################
if __name__ == "__main__":
main()