Skip to content

Commit 946ff1c

Browse files
Merge pull request #9 from mastys100/master
Python 3.x.x compatibility
2 parents 58f7203 + 5a6e686 commit 946ff1c

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

DetectChars.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# DetectChars.py
2+
import os
23

34
import cv2
45
import numpy as np
@@ -49,15 +50,15 @@ def loadKNNDataAndTrainKNN():
4950
try:
5051
npaClassifications = np.loadtxt("classifications.txt", np.float32) # read in training classifications
5152
except: # if file could not be opened
52-
print "error, unable to open classifications.txt, exiting program\n" # show error message
53+
print("error, unable to open classifications.txt, exiting program\n") # show error message
5354
os.system("pause")
5455
return False # and return False
5556
# end try
5657

5758
try:
5859
npaFlattenedImages = np.loadtxt("flattened_images.txt", np.float32) # read in training images
5960
except: # if file could not be opened
60-
print "error, unable to open flattened_images.txt, exiting program\n" # show error message
61+
print("error, unable to open flattened_images.txt, exiting program\n") # show error message
6162
os.system("pause")
6263
return False # and return False
6364
# end try
@@ -144,7 +145,8 @@ def detectCharsInPlates(listOfPossiblePlates):
144145
if (len(listOfListsOfMatchingCharsInPlate) == 0): # if no groups of matching chars were found in the plate
145146

146147
if Main.showSteps == True: # show steps ###############################################
147-
print "chars found in plate number " + str(intPlateCounter) + " = (none), click on any image and press a key to continue . . ."
148+
print("chars found in plate number " + str(
149+
intPlateCounter) + " = (none), click on any image and press a key to continue . . .")
148150
intPlateCounter = intPlateCounter + 1
149151
cv2.destroyWindow("8")
150152
cv2.destroyWindow("9")
@@ -211,15 +213,16 @@ def detectCharsInPlates(listOfPossiblePlates):
211213
possiblePlate.strChars = recognizeCharsInPlate(possiblePlate.imgThresh, longestListOfMatchingCharsInPlate)
212214

213215
if Main.showSteps == True: # show steps ###################################################
214-
print "chars found in plate number " + str(intPlateCounter) + " = " + possiblePlate.strChars + ", click on any image and press a key to continue . . ."
216+
print("chars found in plate number " + str(
217+
intPlateCounter) + " = " + possiblePlate.strChars + ", click on any image and press a key to continue . . .")
215218
intPlateCounter = intPlateCounter + 1
216219
cv2.waitKey(0)
217220
# end if # show steps #####################################################################
218221

219222
# end of big for loop that takes up most of the function
220223

221224
if Main.showSteps == True:
222-
print "\nchar detection complete, click on any image and press a key to continue . . .\n"
225+
print("\nchar detection complete, click on any image and press a key to continue . . .\n")
223226
cv2.waitKey(0)
224227
# end if
225228

DetectPlates.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def detectPlatesInScene(imgOriginalScene):
4343
listOfPossibleCharsInScene = findPossibleCharsInScene(imgThreshScene)
4444

4545
if Main.showSteps == True: # show steps #######################################################
46-
print "step 2 - len(listOfPossibleCharsInScene) = " + str(len(listOfPossibleCharsInScene)) # 131 with MCLRNF1 image
46+
print("step 2 - len(listOfPossibleCharsInScene) = " + str(
47+
len(listOfPossibleCharsInScene))) # 131 with MCLRNF1 image
4748

4849
imgContours = np.zeros((height, width, 3), np.uint8)
4950

@@ -62,7 +63,8 @@ def detectPlatesInScene(imgOriginalScene):
6263
listOfListsOfMatchingCharsInScene = DetectChars.findListOfListsOfMatchingChars(listOfPossibleCharsInScene)
6364

6465
if Main.showSteps == True: # show steps #######################################################
65-
print "step 3 - listOfListsOfMatchingCharsInScene.Count = " + str(len(listOfListsOfMatchingCharsInScene)) # 13 with MCLRNF1 image
66+
print("step 3 - listOfListsOfMatchingCharsInScene.Count = " + str(
67+
len(listOfListsOfMatchingCharsInScene))) # 13 with MCLRNF1 image
6668

6769
imgContours = np.zeros((height, width, 3), np.uint8)
6870

@@ -91,10 +93,10 @@ def detectPlatesInScene(imgOriginalScene):
9193
# end if
9294
# end for
9395

94-
print "\n" + str(len(listOfPossiblePlates)) + " possible plates found" # 13 with MCLRNF1 image
96+
print("\n" + str(len(listOfPossiblePlates)) + " possible plates found") # 13 with MCLRNF1 image
9597

9698
if Main.showSteps == True: # show steps #######################################################
97-
print "\n"
99+
print("\n")
98100
cv2.imshow("4a", imgContours)
99101

100102
for i in range(0, len(listOfPossiblePlates)):
@@ -107,13 +109,13 @@ def detectPlatesInScene(imgOriginalScene):
107109

108110
cv2.imshow("4a", imgContours)
109111

110-
print "possible plate " + str(i) + ", click on any image and press a key to continue . . ."
112+
print("possible plate " + str(i) + ", click on any image and press a key to continue . . .")
111113

112114
cv2.imshow("4b", listOfPossiblePlates[i].imgPlate)
113115
cv2.waitKey(0)
114116
# end for
115117

116-
print "\nplate detection complete, click on any image and press a key to begin char recognition . . .\n"
118+
print("\nplate detection complete, click on any image and press a key to begin char recognition . . .\n")
117119
cv2.waitKey(0)
118120
# end if # show steps #########################################################################
119121

@@ -148,8 +150,8 @@ def findPossibleCharsInScene(imgThresh):
148150
# end for
149151

150152
if Main.showSteps == True: # show steps #######################################################
151-
print "\nstep 2 - len(contours) = " + str(len(contours)) # 2362 with MCLRNF1 image
152-
print "step 2 - intCountOfPossibleChars = " + str(intCountOfPossibleChars) # 131 with MCLRNF1 image
153+
print("\nstep 2 - len(contours) = " + str(len(contours))) # 2362 with MCLRNF1 image
154+
print("step 2 - intCountOfPossibleChars = " + str(intCountOfPossibleChars)) # 131 with MCLRNF1 image
153155
cv2.imshow("2a", imgContours)
154156
# end if # show steps #########################################################################
155157

Main.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ def main():
2323
blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training
2424

2525
if blnKNNTrainingSuccessful == False: # if KNN training was not successful
26-
print "\nerror: KNN traning was not successful\n" # show error message
26+
print("\nerror: KNN traning was not successful\n") # show error message
2727
return # and exit program
2828
# end if
2929

30-
imgOriginalScene = cv2.imread("1.png") # open image
30+
imgOriginalScene = cv2.imread("LicPlateImages/1.png") # open image
3131

3232
if imgOriginalScene is None: # if image was not read successfully
33-
print "\nerror: image not read from file \n\n" # print error message to std out
33+
print("\nerror: image not read from file \n\n") # print error message to std out
3434
os.system("pause") # pause so user can see error message
3535
return # and exit program
3636
# end if
@@ -42,7 +42,7 @@ def main():
4242
cv2.imshow("imgOriginalScene", imgOriginalScene) # show scene image
4343

4444
if len(listOfPossiblePlates) == 0: # if no plates were found
45-
print "\nno license plates were detected\n" # inform user no plates were found
45+
print("\nno license plates were detected\n") # inform user no plates were found
4646
else: # else
4747
# if we get in here list of possible plates has at leat one plate
4848

@@ -56,14 +56,14 @@ def main():
5656
cv2.imshow("imgThresh", licPlate.imgThresh)
5757

5858
if len(licPlate.strChars) == 0: # if no chars were found in the plate
59-
print "\nno characters were detected\n\n" # show message
59+
print("\nno characters were detected\n\n") # show message
6060
return # and exit program
6161
# end if
6262

6363
drawRedRectangleAroundPlate(imgOriginalScene, licPlate) # draw red rectangle around plate
6464

65-
print "\nlicense plate read from image = " + licPlate.strChars + "\n" # write license plate text to std out
66-
print "----------------------------------------"
65+
print("\nlicense plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out
66+
print("----------------------------------------")
6767

6868
writeLicensePlateCharsOnImage(imgOriginalScene, licPlate) # write license plate text on the image
6969

0 commit comments

Comments
 (0)