-
Notifications
You must be signed in to change notification settings - Fork 314
/
DetectChars.py
446 lines (326 loc) · 23.4 KB
/
DetectChars.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# DetectChars.py
import os
import cv2
import numpy as np
import math
import random
import Main
import Preprocess
import PossibleChar
# module level variables ##########################################################################
kNearest = cv2.ml.KNearest_create()
# constants for checkIfPossibleChar, this checks one possible char only (does not compare to another char)
MIN_PIXEL_WIDTH = 2
MIN_PIXEL_HEIGHT = 8
MIN_ASPECT_RATIO = 0.25
MAX_ASPECT_RATIO = 1.0
MIN_PIXEL_AREA = 80
# constants for comparing two chars
MIN_DIAG_SIZE_MULTIPLE_AWAY = 0.3
MAX_DIAG_SIZE_MULTIPLE_AWAY = 5.0
MAX_CHANGE_IN_AREA = 0.5
MAX_CHANGE_IN_WIDTH = 0.8
MAX_CHANGE_IN_HEIGHT = 0.2
MAX_ANGLE_BETWEEN_CHARS = 12.0
# other constants
MIN_NUMBER_OF_MATCHING_CHARS = 3
RESIZED_CHAR_IMAGE_WIDTH = 20
RESIZED_CHAR_IMAGE_HEIGHT = 30
MIN_CONTOUR_AREA = 100
###################################################################################################
def loadKNNDataAndTrainKNN():
allContoursWithData = [] # declare empty lists,
validContoursWithData = [] # we will fill these shortly
try:
npaClassifications = np.loadtxt("classifications.txt", np.float32) # read in training classifications
except: # if file could not be opened
print("error, unable to open classifications.txt, exiting program\n") # show error message
os.system("pause")
return False # and return False
# end try
try:
npaFlattenedImages = np.loadtxt("flattened_images.txt", np.float32) # read in training images
except: # if file could not be opened
print("error, unable to open flattened_images.txt, exiting program\n") # show error message
os.system("pause")
return False # and return False
# end try
npaClassifications = npaClassifications.reshape((npaClassifications.size, 1)) # reshape numpy array to 1d, necessary to pass to call to train
kNearest.setDefaultK(1) # set default K to 1
kNearest.train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications) # train KNN object
return True # if we got here training was successful so return true
# end function
###################################################################################################
def detectCharsInPlates(listOfPossiblePlates):
intPlateCounter = 0
imgContours = None
contours = []
if len(listOfPossiblePlates) == 0: # if list of possible plates is empty
return listOfPossiblePlates # return
# end if
# at this point we can be sure the list of possible plates has at least one plate
for possiblePlate in listOfPossiblePlates: # for each possible plate, this is a big for loop that takes up most of the function
possiblePlate.imgGrayscale, possiblePlate.imgThresh = Preprocess.preprocess(possiblePlate.imgPlate) # preprocess to get grayscale and threshold images
if Main.showSteps == True: # show steps ###################################################
cv2.imshow("5a", possiblePlate.imgPlate)
cv2.imshow("5b", possiblePlate.imgGrayscale)
cv2.imshow("5c", possiblePlate.imgThresh)
# end if # show steps #####################################################################
# increase size of plate image for easier viewing and char detection
possiblePlate.imgThresh = cv2.resize(possiblePlate.imgThresh, (0, 0), fx = 1.6, fy = 1.6)
# threshold again to eliminate any gray areas
thresholdValue, possiblePlate.imgThresh = cv2.threshold(possiblePlate.imgThresh, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
if Main.showSteps == True: # show steps ###################################################
cv2.imshow("5d", possiblePlate.imgThresh)
# end if # show steps #####################################################################
# find all possible chars in the plate,
# this function first finds all contours, then only includes contours that could be chars (without comparison to other chars yet)
listOfPossibleCharsInPlate = findPossibleCharsInPlate(possiblePlate.imgGrayscale, possiblePlate.imgThresh)
if Main.showSteps == True: # show steps ###################################################
height, width, numChannels = possiblePlate.imgPlate.shape
imgContours = np.zeros((height, width, 3), np.uint8)
del contours[:] # clear the contours list
for possibleChar in listOfPossibleCharsInPlate:
contours.append(possibleChar.contour)
# end for
cv2.drawContours(imgContours, contours, -1, Main.SCALAR_WHITE)
cv2.imshow("6", imgContours)
# end if # show steps #####################################################################
# given a list of all possible chars, find groups of matching chars within the plate
listOfListsOfMatchingCharsInPlate = findListOfListsOfMatchingChars(listOfPossibleCharsInPlate)
if Main.showSteps == True: # show steps ###################################################
imgContours = np.zeros((height, width, 3), np.uint8)
del contours[:]
for listOfMatchingChars in listOfListsOfMatchingCharsInPlate:
intRandomBlue = random.randint(0, 255)
intRandomGreen = random.randint(0, 255)
intRandomRed = random.randint(0, 255)
for matchingChar in listOfMatchingChars:
contours.append(matchingChar.contour)
# end for
cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed))
# end for
cv2.imshow("7", imgContours)
# end if # show steps #####################################################################
if (len(listOfListsOfMatchingCharsInPlate) == 0): # if no groups of matching chars were found in the plate
if Main.showSteps == True: # show steps ###############################################
print("chars found in plate number " + str(
intPlateCounter) + " = (none), click on any image and press a key to continue . . .")
intPlateCounter = intPlateCounter + 1
cv2.destroyWindow("8")
cv2.destroyWindow("9")
cv2.destroyWindow("10")
cv2.waitKey(0)
# end if # show steps #################################################################
possiblePlate.strChars = ""
continue # go back to top of for loop
# end if
for i in range(0, len(listOfListsOfMatchingCharsInPlate)): # within each list of matching chars
listOfListsOfMatchingCharsInPlate[i].sort(key = lambda matchingChar: matchingChar.intCenterX) # sort chars from left to right
listOfListsOfMatchingCharsInPlate[i] = removeInnerOverlappingChars(listOfListsOfMatchingCharsInPlate[i]) # and remove inner overlapping chars
# end for
if Main.showSteps == True: # show steps ###################################################
imgContours = np.zeros((height, width, 3), np.uint8)
for listOfMatchingChars in listOfListsOfMatchingCharsInPlate:
intRandomBlue = random.randint(0, 255)
intRandomGreen = random.randint(0, 255)
intRandomRed = random.randint(0, 255)
del contours[:]
for matchingChar in listOfMatchingChars:
contours.append(matchingChar.contour)
# end for
cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed))
# end for
cv2.imshow("8", imgContours)
# end if # show steps #####################################################################
# within each possible plate, suppose the longest list of potential matching chars is the actual list of chars
intLenOfLongestListOfChars = 0
intIndexOfLongestListOfChars = 0
# loop through all the vectors of matching chars, get the index of the one with the most chars
for i in range(0, len(listOfListsOfMatchingCharsInPlate)):
if len(listOfListsOfMatchingCharsInPlate[i]) > intLenOfLongestListOfChars:
intLenOfLongestListOfChars = len(listOfListsOfMatchingCharsInPlate[i])
intIndexOfLongestListOfChars = i
# end if
# end for
# suppose that the longest list of matching chars within the plate is the actual list of chars
longestListOfMatchingCharsInPlate = listOfListsOfMatchingCharsInPlate[intIndexOfLongestListOfChars]
if Main.showSteps == True: # show steps ###################################################
imgContours = np.zeros((height, width, 3), np.uint8)
del contours[:]
for matchingChar in longestListOfMatchingCharsInPlate:
contours.append(matchingChar.contour)
# end for
cv2.drawContours(imgContours, contours, -1, Main.SCALAR_WHITE)
cv2.imshow("9", imgContours)
# end if # show steps #####################################################################
possiblePlate.strChars = recognizeCharsInPlate(possiblePlate.imgThresh, longestListOfMatchingCharsInPlate)
if Main.showSteps == True: # show steps ###################################################
print("chars found in plate number " + str(
intPlateCounter) + " = " + possiblePlate.strChars + ", click on any image and press a key to continue . . .")
intPlateCounter = intPlateCounter + 1
cv2.waitKey(0)
# end if # show steps #####################################################################
# end of big for loop that takes up most of the function
if Main.showSteps == True:
print("\nchar detection complete, click on any image and press a key to continue . . .\n")
cv2.waitKey(0)
# end if
return listOfPossiblePlates
# end function
###################################################################################################
def findPossibleCharsInPlate(imgGrayscale, imgThresh):
listOfPossibleChars = [] # this will be the return value
contours = []
imgThreshCopy = imgThresh.copy()
# find all contours in plate
contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours: # for each contour
possibleChar = PossibleChar.PossibleChar(contour)
if checkIfPossibleChar(possibleChar): # if contour is a possible char, note this does not compare to other chars (yet) . . .
listOfPossibleChars.append(possibleChar) # add to list of possible chars
# end if
# end if
return listOfPossibleChars
# end function
###################################################################################################
def checkIfPossibleChar(possibleChar):
# this function is a 'first pass' that does a rough check on a contour to see if it could be a char,
# note that we are not (yet) comparing the char to other chars to look for a group
if (possibleChar.intBoundingRectArea > MIN_PIXEL_AREA and
possibleChar.intBoundingRectWidth > MIN_PIXEL_WIDTH and possibleChar.intBoundingRectHeight > MIN_PIXEL_HEIGHT and
MIN_ASPECT_RATIO < possibleChar.fltAspectRatio and possibleChar.fltAspectRatio < MAX_ASPECT_RATIO):
return True
else:
return False
# end if
# end function
###################################################################################################
def findListOfListsOfMatchingChars(listOfPossibleChars):
# with this function, we start off with all the possible chars in one big list
# the purpose of this function is to re-arrange the one big list of chars into a list of lists of matching chars,
# note that chars that are not found to be in a group of matches do not need to be considered further
listOfListsOfMatchingChars = [] # this will be the return value
for possibleChar in listOfPossibleChars: # for each possible char in the one big list of chars
listOfMatchingChars = findListOfMatchingChars(possibleChar, listOfPossibleChars) # find all chars in the big list that match the current char
listOfMatchingChars.append(possibleChar) # also add the current char to current possible list of matching chars
if len(listOfMatchingChars) < MIN_NUMBER_OF_MATCHING_CHARS: # if current possible list of matching chars is not long enough to constitute a possible plate
continue # jump back to the top of the for loop and try again with next char, note that it's not necessary
# to save the list in any way since it did not have enough chars to be a possible plate
# end if
# if we get here, the current list passed test as a "group" or "cluster" of matching chars
listOfListsOfMatchingChars.append(listOfMatchingChars) # so add to our list of lists of matching chars
listOfPossibleCharsWithCurrentMatchesRemoved = []
# remove the current list of matching chars from the big list so we don't use those same chars twice,
# make sure to make a new big list for this since we don't want to change the original big list
listOfPossibleCharsWithCurrentMatchesRemoved = list(set(listOfPossibleChars) - set(listOfMatchingChars))
recursiveListOfListsOfMatchingChars = findListOfListsOfMatchingChars(listOfPossibleCharsWithCurrentMatchesRemoved) # recursive call
for recursiveListOfMatchingChars in recursiveListOfListsOfMatchingChars: # for each list of matching chars found by recursive call
listOfListsOfMatchingChars.append(recursiveListOfMatchingChars) # add to our original list of lists of matching chars
# end for
break # exit for
# end for
return listOfListsOfMatchingChars
# end function
###################################################################################################
def findListOfMatchingChars(possibleChar, listOfChars):
# the purpose of this function is, given a possible char and a big list of possible chars,
# find all chars in the big list that are a match for the single possible char, and return those matching chars as a list
listOfMatchingChars = [] # this will be the return value
for possibleMatchingChar in listOfChars: # for each char in big list
if possibleMatchingChar == possibleChar: # if the char we attempting to find matches for is the exact same char as the char in the big list we are currently checking
# then we should not include it in the list of matches b/c that would end up double including the current char
continue # so do not add to list of matches and jump back to top of for loop
# end if
# compute stuff to see if chars are a match
fltDistanceBetweenChars = distanceBetweenChars(possibleChar, possibleMatchingChar)
fltAngleBetweenChars = angleBetweenChars(possibleChar, possibleMatchingChar)
fltChangeInArea = float(abs(possibleMatchingChar.intBoundingRectArea - possibleChar.intBoundingRectArea)) / float(possibleChar.intBoundingRectArea)
fltChangeInWidth = float(abs(possibleMatchingChar.intBoundingRectWidth - possibleChar.intBoundingRectWidth)) / float(possibleChar.intBoundingRectWidth)
fltChangeInHeight = float(abs(possibleMatchingChar.intBoundingRectHeight - possibleChar.intBoundingRectHeight)) / float(possibleChar.intBoundingRectHeight)
# check if chars match
if (fltDistanceBetweenChars < (possibleChar.fltDiagonalSize * MAX_DIAG_SIZE_MULTIPLE_AWAY) and
fltAngleBetweenChars < MAX_ANGLE_BETWEEN_CHARS and
fltChangeInArea < MAX_CHANGE_IN_AREA and
fltChangeInWidth < MAX_CHANGE_IN_WIDTH and
fltChangeInHeight < MAX_CHANGE_IN_HEIGHT):
listOfMatchingChars.append(possibleMatchingChar) # if the chars are a match, add the current char to list of matching chars
# end if
# end for
return listOfMatchingChars # return result
# end function
###################################################################################################
# use Pythagorean theorem to calculate distance between two chars
def distanceBetweenChars(firstChar, secondChar):
intX = abs(firstChar.intCenterX - secondChar.intCenterX)
intY = abs(firstChar.intCenterY - secondChar.intCenterY)
return math.sqrt((intX ** 2) + (intY ** 2))
# end function
###################################################################################################
# use basic trigonometry (SOH CAH TOA) to calculate angle between chars
def angleBetweenChars(firstChar, secondChar):
fltAdj = float(abs(firstChar.intCenterX - secondChar.intCenterX))
fltOpp = float(abs(firstChar.intCenterY - secondChar.intCenterY))
if fltAdj != 0.0: # check to make sure we do not divide by zero if the center X positions are equal, float division by zero will cause a crash in Python
fltAngleInRad = math.atan(fltOpp / fltAdj) # if adjacent is not zero, calculate angle
else:
fltAngleInRad = 1.5708 # if adjacent is zero, use this as the angle, this is to be consistent with the C++ version of this program
# end if
fltAngleInDeg = fltAngleInRad * (180.0 / math.pi) # calculate angle in degrees
return fltAngleInDeg
# end function
###################################################################################################
# if we have two chars overlapping or to close to each other to possibly be separate chars, remove the inner (smaller) char,
# this is to prevent including the same char twice if two contours are found for the same char,
# for example for the letter 'O' both the inner ring and the outer ring may be found as contours, but we should only include the char once
def removeInnerOverlappingChars(listOfMatchingChars):
listOfMatchingCharsWithInnerCharRemoved = list(listOfMatchingChars) # this will be the return value
for currentChar in listOfMatchingChars:
for otherChar in listOfMatchingChars:
if currentChar != otherChar: # if current char and other char are not the same char . . .
# if current char and other char have center points at almost the same location . . .
if distanceBetweenChars(currentChar, otherChar) < (currentChar.fltDiagonalSize * MIN_DIAG_SIZE_MULTIPLE_AWAY):
# if we get in here we have found overlapping chars
# next we identify which char is smaller, then if that char was not already removed on a previous pass, remove it
if currentChar.intBoundingRectArea < otherChar.intBoundingRectArea: # if current char is smaller than other char
if currentChar in listOfMatchingCharsWithInnerCharRemoved: # if current char was not already removed on a previous pass . . .
listOfMatchingCharsWithInnerCharRemoved.remove(currentChar) # then remove current char
# end if
else: # else if other char is smaller than current char
if otherChar in listOfMatchingCharsWithInnerCharRemoved: # if other char was not already removed on a previous pass . . .
listOfMatchingCharsWithInnerCharRemoved.remove(otherChar) # then remove other char
# end if
# end if
# end if
# end if
# end for
# end for
return listOfMatchingCharsWithInnerCharRemoved
# end function
###################################################################################################
# this is where we apply the actual char recognition
def recognizeCharsInPlate(imgThresh, listOfMatchingChars):
strChars = "" # this will be the return value, the chars in the lic plate
height, width = imgThresh.shape
imgThreshColor = np.zeros((height, width, 3), np.uint8)
listOfMatchingChars.sort(key = lambda matchingChar: matchingChar.intCenterX) # sort chars from left to right
cv2.cvtColor(imgThresh, cv2.COLOR_GRAY2BGR, imgThreshColor) # make color version of threshold image so we can draw contours in color on it
for currentChar in listOfMatchingChars: # for each char in plate
pt1 = (currentChar.intBoundingRectX, currentChar.intBoundingRectY)
pt2 = ((currentChar.intBoundingRectX + currentChar.intBoundingRectWidth), (currentChar.intBoundingRectY + currentChar.intBoundingRectHeight))
cv2.rectangle(imgThreshColor, pt1, pt2, Main.SCALAR_GREEN, 2) # draw green box around the char
# crop char out of threshold image
imgROI = imgThresh[currentChar.intBoundingRectY : currentChar.intBoundingRectY + currentChar.intBoundingRectHeight,
currentChar.intBoundingRectX : currentChar.intBoundingRectX + currentChar.intBoundingRectWidth]
imgROIResized = cv2.resize(imgROI, (RESIZED_CHAR_IMAGE_WIDTH, RESIZED_CHAR_IMAGE_HEIGHT)) # resize image, this is necessary for char recognition
npaROIResized = imgROIResized.reshape((1, RESIZED_CHAR_IMAGE_WIDTH * RESIZED_CHAR_IMAGE_HEIGHT)) # flatten image into 1d numpy array
npaROIResized = np.float32(npaROIResized) # convert from 1d numpy array of ints to 1d numpy array of floats
retval, npaResults, neigh_resp, dists = kNearest.findNearest(npaROIResized, k = 1) # finally we can call findNearest !!!
strCurrentChar = str(chr(int(npaResults[0][0]))) # get character from results
strChars = strChars + strCurrentChar # append current char to full string
# end for
if Main.showSteps == True: # show steps #######################################################
cv2.imshow("10", imgThreshColor)
# end if # show steps #########################################################################
return strChars
# end function