-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
149 lines (90 loc) · 5.01 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
# Main.py
import cv2
import numpy as np
import os
import DetectChars
import DetectPlates
import PossiblePlate
import Point
# module level variables ##########################################################################
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 = True
###################################################################################################
def main():
blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN()
if blnKNNTrainingSuccessful == False:
print "\n\nerror: KNN traning was not successful\n\n"
return
# end if
imgOriginalScene = cv2.imread("1.png") # open image
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 function (which exits program)
listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene)
listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates)
cv2.imshow("imgOriginalScene", imgOriginalScene)
if len(listOfPossiblePlates) == 0:
print "\nno license plates were detected\n"
else:
# if we get in here vector of possible plates has at leat one plate
# sort the vector 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]
cv2.imshow("imgPlate", licPlate.imgPlate)
cv2.imshow("imgThresh", licPlate.imgThresh)
if len(licPlate.strChars) == 0:
print "\nno characters were detected\n\n"
return
# end if
drawRedRectangleAroundPlate(imgOriginalScene, licPlate)
print "\nlicense plate read from image = " + licPlate.strChars + "\n"
print "----------------------------------------"
writeLicensePlateCharsOnImage(imgOriginalScene, licPlate)
cv2.imshow("imgOriginalScene", imgOriginalScene)
cv2.imwrite("imgOriginalScene.png", imgOriginalScene)
# end if else
cv2.waitKey(0) # hold windows open until user presses a key
return
# end main
###################################################################################################
def drawRedRectangleAroundPlate(imgOriginalScene, licPlate):
p2fRectPoints = cv2.boxPoints(licPlate.rrLocationOfPlateInScene)
cv2.line(imgOriginalScene, tuple(p2fRectPoints[0]), tuple(p2fRectPoints[1]), SCALAR_RED, 2)
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):
ptCenterOfTextArea = Point.Point()
ptLowerLeftTextOrigin = Point.Point()
sceneHeight, sceneWidth, sceneNumChannels = imgOriginalScene.shape
plateHeight, plateWidth, plateNumChannels = licPlate.imgPlate.shape
intFontFace = cv2.FONT_HERSHEY_SIMPLEX
fltFontScale = float(plateHeight) / 30.0
intFontThickness = int(round(fltFontScale * 1.5))
textSize, baseline = cv2.getTextSize(licPlate.strChars, intFontFace, fltFontScale, intFontThickness)
ptCenterOfTextArea = Point.Point()
( (intPlateCenterX, intPlateCenterY), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg ) = licPlate.rrLocationOfPlateInScene
intPlateCenterX = int(intPlateCenterX)
intPlateCenterY = int(intPlateCenterY)
ptCenterOfTextArea.x = int(intPlateCenterX)
if intPlateCenterY < (sceneHeight * 0.75):
ptCenterOfTextArea.y = int(round(intPlateCenterY)) + int(round(plateHeight * 1.6))
else:
ptCenterOfTextArea.y = int(round(intPlateCenterY)) - int(round(plateHeight * 1.6))
# end if
textSizeWidth, textSizeHeight = textSize
ptLowerLeftTextOrigin.x = int(ptCenterOfTextArea.x - (textSizeWidth / 2))
ptLowerLeftTextOrigin.y = int(ptCenterOfTextArea.y + (textSizeHeight / 2))
cv2.putText(imgOriginalScene, licPlate.strChars, (ptLowerLeftTextOrigin.x, ptLowerLeftTextOrigin.y), intFontFace, fltFontScale, SCALAR_YELLOW, intFontThickness)
# end function
###################################################################################################
if __name__ == "__main__":
main()