def detectCharsInPlates(listOfPossiblePlates): intPlateCounter = 0 imgContours = None contours = [] if len(listOfPossiblePlates) == 0: return listOfPossiblePlates for possiblePlate in listOfPossiblePlates: possiblePlate.imgGrayscale, possiblePlate.imgThresh = refining.preprocess( possiblePlate.imgPlate) if product.showSteps == True: cv2.imshow("5a", possiblePlate.imgPlate) cv2.imshow("5b", possiblePlate.imgGrayscale) cv2.imshow("5c", possiblePlate.imgThresh) possiblePlate.imgThresh = cv2.resize(possiblePlate.imgThresh, (0, 0), fx=1.6, fy=1.6) thresholdValue, possiblePlate.imgThresh = cv2.threshold( possiblePlate.imgThresh, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU) if product.showSteps == True: cv2.imshow("5d", possiblePlate.imgThresh) listOfPossibleCharsInPlate = findPossibleCharsInPlate( possiblePlate.imgGrayscale, possiblePlate.imgThresh) if product.showSteps == True: height, width, numChannels = possiblePlate.imgPlate.shape imgContours = np.zeros((height, width, 3), np.uint8) del contours[:] for possibleChar in listOfPossibleCharsInPlate: contours.append(possibleChar.contour) cv2.drawContours(imgContours, contours, -1, product.SCALAR_WHITE) cv2.imshow("6", imgContours) listOfListsOfMatchingCharsInPlate = findListOfListsOfMatchingChars( listOfPossibleCharsInPlate) if product.showSteps == True: 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) cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed)) cv2.imshow("7", imgContours) if (len(listOfListsOfMatchingCharsInPlate) == 0): if product.showSteps == True: 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) possiblePlate.strChars = "" continue for i in range(0, len(listOfListsOfMatchingCharsInPlate)): listOfListsOfMatchingCharsInPlate[i].sort( key=lambda matchingChar: matchingChar.intCenterX) listOfListsOfMatchingCharsInPlate[i] = removeInnerOverlappingChars( listOfListsOfMatchingCharsInPlate[i]) if product.showSteps == True: 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) cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed)) cv2.imshow("8", imgContours) intLenOfLongestListOfChars = 0 intIndexOfLongestListOfChars = 0 for i in range(0, len(listOfListsOfMatchingCharsInPlate)): if len(listOfListsOfMatchingCharsInPlate[i] ) > intLenOfLongestListOfChars: intLenOfLongestListOfChars = len( listOfListsOfMatchingCharsInPlate[i]) intIndexOfLongestListOfChars = i longestListOfMatchingCharsInPlate = listOfListsOfMatchingCharsInPlate[ intIndexOfLongestListOfChars] if product.showSteps == True: imgContours = np.zeros((height, width, 3), np.uint8) del contours[:] for matchingChar in longestListOfMatchingCharsInPlate: contours.append(matchingChar.contour) cv2.drawContours(imgContours, contours, -1, product.SCALAR_WHITE) cv2.imshow("9", imgContours) possiblePlate.strChars = recognizeCharsInPlate( possiblePlate.imgThresh, longestListOfMatchingCharsInPlate) if product.showSteps == True: 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) if product.showSteps == True: print( "\nchar detection complete, click on any image and press a key to continue . . .\n" ) cv2.waitKey(0) return listOfPossiblePlates
def detectPlatesInScene(imgOriginalScene): listOfPossiblePlates = [] 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) cv2.destroyAllWindows() if product.showSteps == True: cv2.imshow("0", imgOriginalScene) imgGrayscaleScene, imgThreshScene = refining.preprocess(imgOriginalScene) if product.showSteps == True: cv2.imshow("1a", imgGrayscaleScene) cv2.imshow("1b", imgThreshScene) listOfPossibleCharsInScene = findPossibleCharsInScene(imgThreshScene) if product.showSteps == True: print("step 2 - len(listOfPossibleCharsInScene) = " + str(len(listOfPossibleCharsInScene))) imgContours = np.zeros((height, width, 3), np.uint8) contours = [] for possibleChar in listOfPossibleCharsInScene: contours.append(possibleChar.contour) cv2.drawContours(imgContours, contours, -1, product.SCALAR_WHITE) cv2.imshow("2b", imgContours) listOfListsOfMatchingCharsInScene = char_detection.findListOfListsOfMatchingChars( listOfPossibleCharsInScene) if product.showSteps == True: print("step 3 - listOfListsOfMatchingCharsInScene.Count = " + str(len(listOfListsOfMatchingCharsInScene))) imgContours = np.zeros((height, width, 3), np.uint8) for listOfMatchingChars in listOfListsOfMatchingCharsInScene: intRandomBlue = random.randint(0, 255) intRandomGreen = random.randint(0, 255) intRandomRed = random.randint(0, 255) contours = [] for matchingChar in listOfMatchingChars: contours.append(matchingChar.contour) cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed)) cv2.imshow("3", imgContours) for listOfMatchingChars in listOfListsOfMatchingCharsInScene: possiblePlate = extractPlate(imgOriginalScene, listOfMatchingChars) if possiblePlate.imgPlate is not None: listOfPossiblePlates.append(possiblePlate) print("\n" + str(len(listOfPossiblePlates)) + " possible plates found") # 13 with MCLRNF1 image if product.showSteps == True: print("\n") cv2.imshow("4a", imgContours) for i in range(0, len(listOfPossiblePlates)): p2fRectPoints = cv2.boxPoints( listOfPossiblePlates[i].rrLocationOfPlateInScene) cv2.line(imgContours, tuple(p2fRectPoints[0]), tuple(p2fRectPoints[1]), product.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), product.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), product.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), product.SCALAR_RED, 2) cv2.imshow("4a", imgContours) print("possible plate " + str(i) + ", click on any image and press a key to continue . . .") cv2.imshow("4b", listOfPossiblePlates[i].imgPlate) cv2.waitKey(0) print( "\nplate detection complete, click on any image and press a key to begin char recognition . . .\n" ) cv2.waitKey(0) return listOfPossiblePlates