forked from muchlisinadi/ALPR-Indonesia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectPlates.py
141 lines (94 loc) · 5.8 KB
/
DetectPlates.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
# DetectPlates.py
import math
import cv2
import numpy as np
import DetectChars
import PossibleChar
import PossiblePlate
import Preprocess
# module level variables ##########################################################################
PLATE_WIDTH_PADDING_FACTOR = 1.1
PLATE_HEIGHT_PADDING_FACTOR = 1.5
# 1.3 dan 1.5
####################################################################################################
def detectPlatesInScene(imgOriginalScene):
listOfPossiblePlates = [] # this will be the return value
height, width, numChannels = imgOriginalScene.shape
imgGrayscaleScene = np.zeros((height, width, 1), np.uint8)
imgThreshScene = np.zeros((height, width, 1), np.uint8)
imgContours = np.zeros((height, width, 3), np.uint8)
imgGrayscaleScene, imgThreshScene = Preprocess.preprocess(
imgOriginalScene) # preprocess to get grayscale and threshold images
# find all possible chars in the scene,
# this function first finds all contours, then only includes contours that could be chars (without comparison to other chars yet)
listOfPossibleCharsInScene = findPossibleCharsInScene(imgThreshScene)
# given a list of all possible chars, find groups of matching chars
# in the next steps each group of matching chars will attempt to be recognized as a plate
listOfListsOfMatchingCharsInScene = DetectChars.findListOfListsOfMatchingChars(listOfPossibleCharsInScene)
for listOfMatchingChars in listOfListsOfMatchingCharsInScene: # for each group of matching chars
possiblePlate = extractPlate(imgOriginalScene, listOfMatchingChars) # attempt to extract plate
if possiblePlate.imgPlate is not None: # if plate was found
listOfPossiblePlates.append(possiblePlate) # add to list of possible plates
# end if
# end for
# print("\n" + str(len(listOfPossiblePlates)) + " possible plates found") # 13 with MCLRNF1 image
return listOfPossiblePlates
# end function
###################################################################################################
def findPossibleCharsInScene(imgThresh):
listOfPossibleChars = [] # this will be the return value
intCountOfPossibleChars = 0
imgThreshCopy = imgThresh.copy()
contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE) # find all contours
height, width = imgThresh.shape
imgContours = np.zeros((height, width, 3), np.uint8)
for i in range(0, len(contours)): # for each contour
possibleChar = PossibleChar.PossibleChar(contours[i])
if DetectChars.checkIfPossibleChar(
possibleChar): # if contour is a possible char, note this does not compare to other chars (yet) . . .
intCountOfPossibleChars = intCountOfPossibleChars + 1 # increment count of possible chars
listOfPossibleChars.append(possibleChar) # and add to list of possible chars
# end if
# end for
return listOfPossibleChars
# end function
###################################################################################################
def extractPlate(imgOriginal, listOfMatchingChars):
possiblePlate = PossiblePlate.PossiblePlate() # this will be the return value
listOfMatchingChars.sort(
key=lambda matchingChar: matchingChar.intCenterX) # sort chars from left to right based on x position
# calculate the center point of the plate
fltPlateCenterX = (listOfMatchingChars[0].intCenterX + listOfMatchingChars[
len(listOfMatchingChars) - 1].intCenterX) / 2.0
fltPlateCenterY = (listOfMatchingChars[0].intCenterY + listOfMatchingChars[
len(listOfMatchingChars) - 1].intCenterY) / 2.0
ptPlateCenter = fltPlateCenterX, fltPlateCenterY
# calculate plate width and height
intPlateWidth = int((listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX + listOfMatchingChars[
len(listOfMatchingChars) - 1].intBoundingRectWidth - listOfMatchingChars[
0].intBoundingRectX) * PLATE_WIDTH_PADDING_FACTOR)
intTotalOfCharHeights = 0
for matchingChar in listOfMatchingChars:
intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
# end for
fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)
intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)
# calculate correction angle of plate region
fltOpposite = listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY - listOfMatchingChars[0].intCenterY
fltHypotenuse = DetectChars.distanceBetweenChars(listOfMatchingChars[0],
listOfMatchingChars[len(listOfMatchingChars) - 1])
fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)
# pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
possiblePlate.rrLocationOfPlateInScene = (
tuple(ptPlateCenter), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg)
# final steps are to perform the actual rotation
# get the rotation matrix for our calculated correction angle
rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter), fltCorrectionAngleInDeg, 1.0)
height, width, numChannels = imgOriginal.shape # unpack original image width and height
imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height)) # rotate the entire image
imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight), tuple(ptPlateCenter))
possiblePlate.imgPlate = imgCropped # copy the cropped plate image into the applicable member variable of the possible plate
return possiblePlate
# end function