def main(image): CnnClassifier = DetectChars.loadCNNClassifier() # attempt KNN training #response = str(input('Do you want to see the Intermediate images: ')) """ if response == 'Y' or response == 'y': showSteps = True else: showSteps = False """ if CnnClassifier == False: # if KNN training was not successful print( "\nerror: CNN traning was not successful\n") # show error message return # and exit program imgOriginalScene = cv2.imread(image) # open image h, w = imgOriginalScene.shape[:2] # As the image may be blurr so we sharpen the image. #kernel_shapening4 = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]]) #imgOriginalScene = cv2.filter2D(imgOriginalScene,-1,kernel_shapening4) #imgOriginalScene = cv2.resize(imgOriginalScene,(1000,600),interpolation = cv2.INTER_LINEAR) imgOriginalScene = cv2.resize(imgOriginalScene, (0, 0), fx=1.4, fy=1.4, interpolation=cv2.INTER_LINEAR) #imgOriginalScene = cv2.fastNlMeansDenoisingColored(imgOriginalScene,None,10,10,7,21) #imgOriginal = imgOriginalScene.copy() 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 program listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates. We get a list of # combinations of contours that may be a plate. listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates if showSteps == True: cv2.imshow("imgOriginalScene", imgOriginalScene) # show scene image if len(listOfPossiblePlates) == 0: # if no plates were found print("\nno license plates were detected\n" ) # inform user no plates were found response = ' ' return response, imgOriginalScene 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 showSteps == True: cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.waitKey(0) if len(licPlate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return ' ', imgOriginalScene # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from ", image, " :", licPlate.strChars, "\n") print("----------------------------------------") if showSteps == True: writeLicensePlateCharsOnImage( imgOriginalScene, licPlate) # write license plate text on the image cv2.imshow("imgOriginalScene", imgOriginalScene) # re-show scene image cv2.imwrite("imgOriginalScene.png", imgOriginalScene) # write image out to file cv2.waitKey(0) # hold windows open until user presses a key return licPlate.strChars, licPlate.imgPlate
def main(image): CnnClassifier = DetectChars.loadCNNClassifier() # load CNN if CnnClassifier == False: # if CNN fails print("\nerror: CNN traning was not successful\n") # error return imgOriginalScene = cv2.imread(image) # open image #cv2.imshow(" Original image", imgOriginalScene) cv2.imwrite( os.path.join( 'C:/Users/Siddhant Rao/Desktop/ALPR-master/Main Program/output', 'original-img.jpg'), imgOriginalScene) cv2.waitKey(0) imgOriginalScene = cv2.resize(imgOriginalScene, (0, 0), fx=1.4, fy=1.4, interpolation=cv2.INTER_LINEAR) # cv2.imshow("Resized Original image", imgOriginalScene) cv2.waitKey(0) cv2.imwrite( os.path.join( 'C:/Users/Siddhant Rao/Desktop/ALPR-master/Main Program/output', 'resize-img.jpg'), imgOriginalScene) if imgOriginalScene is None: print("\nerror: image not read from file \n\n") os.system("pause") return # combinations of contours that may be a plate. listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # detect chars in plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) if len(listOfPossiblePlates) == 0: # if no plates were found print("\nno license plates were detected\n" ) # inform user no plates were found response = ' ' return response, imgOriginalScene else: # sort from most number of chars to least number of chars) listOfPossiblePlates.sort( key=lambda possiblePlate: len(possiblePlate.strChars), reverse=True) # if 1st plate is the actual plate licPlate = listOfPossiblePlates[0] if len(licPlate.strChars) == 0: print("\nno characters were detected\n\n") return ' ', imgOriginalScene drawRedRectangleAroundPlate(imgOriginalScene, licPlate) print("\nlicense plate read from ", image, " :", licPlate.strChars, "\n") print("----------------------------------------") # writeLicensePlateCharsOnImage(imgOriginalScene, licPlate) # cv2.imwrite( os.path.join( 'C:/Users/Siddhant Rao/Desktop/ALPR-master/Main Program/output', 'Detected-img.jpg'), imgOriginalScene) #cv2.waitKey(0) return licPlate.strChars, licPlate.imgPlate