def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print "\nerror: KNN traning was not successful\n" # show error message return # and exit program # end if imgOriginalScene = cv2.imread("1.png") # open image 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates) # detect chars in plates 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 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] cv2.imshow("imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print "\nno characters were detected\n\n" # show message return # and exit program # end if drawRedRectangleAroundPlate(imgOriginalScene, licPlate) # draw red rectangle around plate print "\nlicense plate read from image = " + licPlate.strChars + "\n" # write license plate text to std out print "----------------------------------------" 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 # end if else cv2.waitKey(0) # hold windows open until user presses a key return
def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # treniranje KNN-a if blnKNNTrainingSuccessful == False: # Ukoliko nije uspesno izvrseno treniranje vraca se poruka o tome print ("\nerror: KNN traning was not successful\n") return imgOriginalScene = cv2.imread("6.png") # otvaranje slike if imgOriginalScene is None: # ukoliko slika nije uspesno otvorena vraca se poruka o tome print("\nerror: image not read from file \n\n") os.system("pause") return possiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # detektovanje samih tablica possiblePlates = DetectChars.detectCharsInPlates(possiblePlates) # detektovanje karaktera na tablicama cv2.imshow("Original image", imgOriginalScene) if len(possiblePlates) == 0: print("\nno license plates were detected\n") else: possiblePlates.sort(key = lambda possiblePlate: len(possiblePlate.strChars), reverse = True) licPlate = possiblePlates[0] if len(licPlate.strChars) == 0: print("\nno characters were detected\n\n") return # end if Draw.drawRedRectangleAroundPlate(imgOriginalScene, licPlate) print("\nlicense plate read from image = " + licPlate.strChars + "\n") print("----------------------------------------") Draw.writeLicensePlateCharsOnImage(imgOriginalScene, licPlate) cv2.imshow("Result", imgOriginalScene) cv2.imwrite("result.png", imgOriginalScene) cv2.waitKey(0) return
def findPossibleCharsInScene(imgThresh): listOfPossibleChars = [] intCountOfPossibleChars = 0 imgThreshCopy = imgThresh.copy() imgContours, contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) height, width = imgThresh.shape imgContours = np.zeros((height, width, 3), np.uint8) for i in range(0, len(contours)): if Main.showSteps == True: cv2.drawContours(imgContours, contours, i, Main.SCALAR_WHITE) possibleChar = PossibleChar.PossibleChar(contours[i]) if DetectChars.checkIfPossibleChar(possibleChar): intCountOfPossibleChars = intCountOfPossibleChars + 1 listOfPossibleChars.append(possibleChar) if Main.showSteps == True: print "\nstep 2 - len(contours) = " + str(len(contours)) print "step 2 - intCountOfPossibleChars = " + str(intCountOfPossibleChars) cv2.imshow("2a", imgContours) return listOfPossibleChars
def findPossibleCharsInScene(imgThresh): listOfPossibleChars = [] # this will be the return value intCountOfPossibleChars = 0 imgThreshCopy = imgThresh.copy() imgContours, 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 if Main.showSteps == True: # show steps ################################################### cv2.drawContours(imgContours, contours, i, Main.SCALAR_WHITE) # end if # show steps ##################################################################### 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 if Main.showSteps == True: # show steps ####################################################### print "\nstep 2 - len(contours) = " + str(len(contours)) # 2362 with MCLRNF1 image print "step 2 - intCountOfPossibleChars = " + str(intCountOfPossibleChars) # 131 with MCLRNF1 image cv2.imshow("2a", imgContours) # end if # show steps ######################################################################### return listOfPossibleChars
def detectPlatesInScene(imgOriginalScene): possiblePlates = [] 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() imgGrayscaleScene, imgThreshScene = Preprocess.preprocess(imgOriginalScene) possibleCharsInScene = findPossibleCharsInScene(imgThreshScene) listOfListsOfMatchingCharsInScene = DetectChars.findListOfListsOfMatchingChars(possibleCharsInScene) for matchingChars in listOfListsOfMatchingCharsInScene: possiblePlate = extractPlate(imgOriginalScene, matchingChars) if possiblePlate.imgPlate is not None: possiblePlates.append(possiblePlate) print "\n" + str(len(possiblePlates)) + " possible plates found" return possiblePlates
def findPlate(imgOriginalScene): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print "\nerror: KNN traning was not successful\n" # show error message return # and exit program # end if 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 # end if plateList = DetectPlates.detectPlatesInScene(imgOriginalScene) # detect plates plateList = DetectChars.detectCharsInPlates(plateList) # detect chars in plates if len(plateList) == 0: # if no plates were found return # inform user no plates were found else: # else plateList.sort(key = lambda possiblePlate: len(possiblePlate.strChars), reverse = True) plateNo = plateList[0] if len(plateNo.strChars) == 0: # if no chars were found in the plate return # and exit program # end if stringArray = [] return plateNo
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
def extractPlate(imgOriginal, listOfMatchingChars): possiblePlate = PossiblePlate.PossiblePlate() listOfMatchingChars.sort(key = lambda matchingChar: matchingChar.intCenterX) 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 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 fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars) intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR) 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) possiblePlate.rrLocationOfPlateInScene = ( tuple(ptPlateCenter), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg ) rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter), fltCorrectionAngleInDeg, 1.0) height, width, numChannels = imgOriginal.shape imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height)) imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight), tuple(ptPlateCenter)) possiblePlate.imgPlate = imgCropped return possiblePlate
def findPossibleCharsInScene(imgThresh): possibleChars = [] possibleCharsCount = 0 imgThreshCopy = imgThresh.copy() imgContours, contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) height, width = imgThresh.shape imgContours = np.zeros((height, width, 3), np.uint8) for i in range(0, len(contours)): possibleChar = PossibleChar.PossibleChar(contours[i]) if DetectChars.checkIfPossibleChar(possibleChar): possibleCharsCount = possibleCharsCount + 1 possibleChars.append(possibleChar) return possibleChars
def detectPlatesInScene(imgOriginalScene, location): listOfRawPossiblePlates = [] # 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) cv2.destroyAllWindows() if Main.showSteps == True: # show steps ####################################################### cv2.imshow("0", imgOriginalScene) # end if # show steps ######################################################################### imgGrayscaleScene, imgThreshScene = Preprocess.preprocess( imgOriginalScene) # preprocess to get grayscale and threshold images if Main.showSteps == True: # show steps ####################################################### cv2.imshow("1a", imgGrayscaleScene) cv2.imshow("1b", imgThreshScene) # end if # show steps ######################################################################### # 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) listOfPossibleCharsInScene.sort(key=lambda Char: Char.intCenterX) if Main.showSteps == True: # show steps ####################################################### print("step 2 - len(listOfPossibleCharsInScene) = " + str(len(listOfPossibleCharsInScene))) # 131 with MCLRNF1 image imgContours = np.zeros((height, width, 3), np.uint8) contours = [] for possibleChar in listOfPossibleCharsInScene: contours.append(possibleChar.contour) # end for cv2.drawContours(imgContours, contours, -1, Main.SCALAR_WHITE) # picture 2a - list of all contours # list of possible chars cv2.imshow("2b", imgContours) # end if # show steps ######################################################################### # 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) if Main.showSteps == True: # show steps ####################################################### print("step 3 - listOfListsOfMatchingCharsInScene.Count = " + str( len(listOfListsOfMatchingCharsInScene))) # 13 with MCLRNF1 image 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) # end for cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed)) # end for cv2.imshow("3", imgContours) # end if # show steps ######################################################################### for listOfMatchingChars in listOfListsOfMatchingCharsInScene: # for each group of matching chars possiblePlate = extractPlate( listOfMatchingChars) # attempt to extract plate listOfRawPossiblePlates.append( possiblePlate) # add to list of possible plates # end if # end for listOfPossiblePlates = groupPossiblePlates(imgOriginalScene, listOfRawPossiblePlates) print("\n" + str(len(listOfPossiblePlates)) + " possible plates found") # 13 with MCLRNF1 image if Main.showSteps == True: # show steps ####################################################### 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]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), Main.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) # end for print( "\nplate detection complete, click on any image and press a key to begin char recognition . . .\n" ) cv2.waitKey(0) # end if # show steps ######################################################################### if Main.save == True: for i in range(0, len(listOfPossiblePlates)): # save plate fileName = location.split("/")[-1].split(".") plateFolder = "outputs/" + fileName[0] if not os.path.isdir(plateFolder): os.makedirs(plateFolder) extractedPlateName = fileName[0] + "/plate_" + str( i) + "." + fileName[1] resized_plate = cv2.resize(listOfPossiblePlates[i].imgPlate, SHAPE_OF_POSSIBLE_PLATE) cv2.imwrite("outputs/" + extractedPlateName, resized_plate) return listOfPossiblePlates
def main(): def fire(): import pyrebase config = { "apiKey": "AIzaSyBRHhL6vH-qNeOjXyiPiJ3p5JkyNz1KdlU", "authDomain": "registrationuser-2f9aa.firebaseapp.com", "databaseURL": "https://registrationuser-2f9aa.firebaseio.com", "projectId:": "registrationuser-2f9aa", "storageBucket": "registrationuser-2f9aa.appspot.com", "messagingSenderId": "280469289706" } firebase = pyrebase.initialize_app(config) vechile_number = [] db = firebase.database() a = 0 while (1): users = db.child("carin").get() temp = dict(users.val()) key_data = list(users.val()) if (len(key_data) != a): for i in range(len(key_data)): vechile_number.insert(i, temp[key_data[i]]['carvechile']) for j in range(len(key_data)): print(vechile_number[j]) print("-----------------------------------------------") a = len(key_data) if (x in vechile_number): print("open") break else: car_driver = input("Driver name:") car_mobile = input("Mobile number:") car_vechile = x data = { 'cardriver': car_driver, 'carmobile': car_mobile, 'carvechile': car_vechile } db.child("carin").push(data) print("edit successfully") blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() if blnKNNTrainingSuccessful == False: print("\nerror: KNN traning was not successful\n") return # end if ## imgOriginalScene = cv2.imread("10.png") #camera.capture("imgOriginalScene") #(grabbed, imgOriginalScene) = camera.read() ## camera.capture('/home/pi/Desktop/clone/imgOriginalScene') ## camera.stop_preview() # res=urllib.request.urlopen('http://192.168.137.216:8080/shot.jpg') # data=np.array(bytearray(res.read()),dtype=np.uint8) # imgOriginalScene=cv2.imdecode(data,-1) for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): image = frame.array if imgOriginalScene is None: print("\nerror: image not read from file \n\n") os.system("pause") return # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) cv2.imshow("imgOriginalScene", imgOriginalScene) if len(listOfPossiblePlates) == 0: print("\nno license plates were detected\n") else: # else listOfPossiblePlates.sort( key=lambda possiblePlate: len(possiblePlate.strChars), reverse=True) licPlate = listOfPossiblePlates[0] cv2.imshow("imgPlate", licPlate.imgPlate) cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: print("\nno characters were detected\n\n") return # end if drawRedRectangleAroundPlate(imgOriginalScene, licPlate) # vechile_number = [ ] x = licPlate.strChars print("license plate is" + x + "\n") fire() #print ("\nlicense plate read from image to data is = " + x + "\n") # print ("----------------------------------------") writeLicensePlateCharsOnImage(imgOriginalScene, licPlate) cv2.imshow("imgOriginalScene", imgOriginalScene) cv2.imwrite("imgOriginalScene.png", imgOriginalScene) # end if else cv2.waitKey(0) return
def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print "\nerror: KNN traning was not successful\n" # show error message return # and exit program # end if imgOriginalScene = cv2.imread("2.png") # open image 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates 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 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] cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print "\nno characters were detected\n\n" # show message return # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print "\nlicense plate read from image = " + licPlate.strChars + "\n" # write license plate text to std out print "----------------------------------------" nplate = licPlate.strChars import mysql.connector config = { 'user': '******', #create your own user id and password 'password': '******', 'host': 'localhost', #choose your own localhost 'database': 'alprs', 'raise_on_warnings': True, } cnx = mysql.connector.connect(**config) cursor = cnx.cursor() #add_numberplate = ("INSERT INTO numberplates " # "(numberplate) " # "VALUES (%s)") # Insert new employee #cursor.execute() #cursor.execute(add_numberplate) cursor.execute("INSERT INTO nplates (numberplate) VALUES (%s)", (nplate, )) # Make sure data is committed to the database cnx.commit() cursor.close() cnx.close() #print numberplate #con = mysql.connector.connect(user='******', password='', host='localhost', database='alpr') #cur = con.cursor() #cur.execute("insert into alprdetails" "(numberplate)" "values (%s)") #con.commit() #cur.close() #con.close() print "-------------added into database---------------------------" 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 # end if else cv2.waitKey(0) # hold windows open until user presses a key return
def main(car_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(car_image) # open image plt.imshow(imgOriginalScene) h, w = imgOriginalScene.shape[:2] imgOriginalScene = cv2.resize(imgOriginalScene, (0, 0), fx=1.4, fy=1.4, interpolation=cv2.INTER_CUBIC) #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: Image.fromarray(imgOriginalScene, 'RGB').show() # 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: Image.fromarray(licPlate.imgPlate).show( ) # show crop of plate and threshold of plate 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 """ # Uncomment this if want to check for individual plate print("\nlicense plate read from ", image," :",licPlate.strChars,"\n") print("----------------------------------------") """ if showSteps == True: writeLicensePlateCharsOnImage( imgOriginalScene, licPlate) # write license plate text on the image Image.fromarray(imgOriginalScene).show() # re-show scene image cv2.imwrite("imgOriginalScene.png", imgOriginalScene) # write image out to file input('Press any key to continue...' ) # hold windows open until user presses a key return licPlate.strChars, licPlate.imgPlate
def main(): # argument for input video/image/calibration ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help = "path to video file") ap.add_argument("-i", "--image", help = "Path to the image") ap.add_argument("-c", "--calibration", help = "image or video or camera") args = vars(ap.parse_args()) if args.get("calibration", True): imgOriginalScene = cv2.imread(args["calibration"]) if imgOriginalScene is None: print(" Please check again the path of image or argument !") imgOriginalScene = imutils.resize(imgOriginalScene, width = 720) cal.calibration(imgOriginalScene) return if args.get("video", True): camera = cv2.VideoCapture(args["video"]) if camera is None: print(" Please check again the path of video or argument !") loop = True elif args.get("image", True): imgOriginalScene = cv2.imread(args["image"]) if imgOriginalScene is None: print(" Please check again the path of image or argument !") loop = False else: camera = cv2.VideoCapture(0) loop = True # add knn library for detect chars blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print("\nerror: KNN traning was not successful\n") # show error message return count = 0 # not very important, just iterating for license array haha license = [] VER = np.zeros(VERIF) for x in VER: license.append("") numlicense = "" knn = 0 # Looping for Video while (loop): # grab the current frame (grabbed, frame) = camera.read() if args.get("video") and not grabbed: break # resize the frame and convert it to grayscale imgOriginalScene = imutils.resize(frame, width = 620) imgGrayscale, imgThresh = pp.preprocess(imgOriginalScene) cv2.imshow("threshold", imgThresh) imgOriginalScene = imutils.transform (imgOriginalScene) imgOriginalScene, licenses = searching(imgOriginalScene,loop) # only save 5 same license each time license[count+1] = licenses if (license[count] == license[count+1]): license[count]=license[count+1] count = count + 1 elif (license[count] != license[count+1]): coba = license[count+1] count = 0 license[count] = coba if count == (VERIF-1): if (license[VERIF-1] == ""): print("no characters were detected\n") else: #if number license same, not be saved if numlicense == license[VERIF-1]: print("still = " + numlicense + "\n") else: numlicense = license[VERIF-1] print("A new license plate read from image = " + license[VERIF-1] + "\n") cv2.imshow(license[VERIF-1], imgOriginalScene) namefile = "hasil/"+ license[VERIF-1] + ".png" cv2.imwrite(namefile, imgOriginalScene) count = 0 #print(license) # re-show scene image #imgOriginalScene = cv2.blur(imgOriginalScene,(12,12)) cv2.putText(imgOriginalScene,"Press 's' to save frame to be 'save.png', for calibrating",(10,30),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255,255,255),1,bottomLeftOrigin = False) #drawRedRectangleAroundPlate(imgOriginalScene, imgOriginalScene) cv2.rectangle(imgOriginalScene,((imgOriginalScene.shape[1]/2-230),(imgOriginalScene.shape[0]/2-80)),((imgOriginalScene.shape[1]/2+230),(imgOriginalScene.shape[0]/2+80)),SCALAR_GREEN,3) cv2.imshow("imgOriginalScene", imgOriginalScene) #cv2.imshow("ori", frame) key = cv2.waitKey(5) & 0xFF if key == ord('s'): knn = str(knn) savefileimg = "calib_knn/img_"+ knn +".png" savefileThr = "calib_knn/Thr_"+ knn +".png" #cv2.saveimage("save.png", imgOriginalScene) cv2.imwrite(savefileimg, frame) cv2.imwrite(savefileThr, imgThresh) print("image save !") knn = int(knn) knn = knn + 1 if key == 27: # if the 'q' key is pressed, stop the loop break camera.release() # cleanup the camera and close any open windows # For image only if (loop == False): imgOriginalScene = imutils.resize(imgOriginalScene, width = 720) cv2.imshow("original",imgOriginalScene) imgGrayscale, imgThresh = pp.preprocess(imgOriginalScene) cv2.imshow("threshold",imgThresh) imgOriginalScene = imutils.transform (imgOriginalScene) imgOriginalScene,license = searching(imgOriginalScene,loop) #imgOriginalScene = imutils.detransform(imgOriginalScene) cv2.waitKey(0) cv2.waitKey(0) cv2.destroyAllWindows() return
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
def main(): file = open('testfile.txt', 'r') global s s = file.readline() print(s) blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print( "\nerror: KNN traning was not successful\n") # show error message return # and exit program # end if imgOriginalScene = cv2.imread(s) # open image 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates 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 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) #print(listOfPossiblePlates) # 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] cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out print("----------------------------------------") 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 # end if else cv2.waitKey(0) # hold windows open until user presses a key return
def main(): # Recupera valores do treinamento de KNN blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # Verifica se o treinamento falhou if not blnKNNTrainingSuccessful: print "\nErro: Ocorreu um erro no treinamento de KNN\n" return # Abre a imagem imgOriginalScene = cv2.imread("images/placas07.jpg") # Verifica se a imagem original foi encontrada if imgOriginalScene is None: print "\nErro: Não foi possível ler a imagem de entrada \n\n" os.system("pause") return # Detecta placas listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # Detecta caracteres na placa listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates) # Mostra imagem original cv2.imshow("imgOriginalScene", imgOriginalScene) # Verifica se foram encontradas placas if len(listOfPossiblePlates) == 0: print "\nNenhuma placa encontrada\n" else: # Ordena a lista de possiveis placas em ordem decrescente (Mais caracteres para menos caracteres) listOfPossiblePlates.sort(key=lambda possiblePlate: len(possiblePlate.strChars), reverse=True) # Inicia com a primeira placa licPlate = listOfPossiblePlates[0] # Mostra a placa e sua versao binarisada cv2.imshow("imgPlate", licPlate.imgPlate) #cv2.imshow("imgThresh", licPlate.imgThresh) # Verifica se existem caracteres nas placas if len(licPlate.strChars) == 0: print "\nNão foram encontrados caracteres\n\n" return # Desenha um retangulo ao redor da placa drawRedRectangleAroundPlate(imgOriginalScene, licPlate) # Printa o texto da placa print "\nPlaca lida da imagem = " + licPlate.strChars + "\n" print "----------------------------------------" # Escreve texto da placa na imagem writeLicensePlateCharsOnImage(imgOriginalScene, licPlate) # Exibe a imagem original novamente cv2.imshow("imgOriginalScene", imgOriginalScene) # Escreve a imagem num arquivo de saida cv2.imwrite("imgOriginalScene.png", imgOriginalScene) # Espera interacao do usuario cv2.waitKey(0) return
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 lpno = licPlate.strChars print(lpno) #print("\nlicense plate read from ", image," :",licPlate.strChars,"\n") #print("----------------------------------------") #text_file = open("Output.txt", "w") #text_file.append(lpno) #text_file.close() 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 run(file): # open file imgOriginalScene = cv2.imread(file) 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates print("1 done") listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates print("2 done") 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 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] largest_width = listOfPossiblePlates[0].rrLocationOfPlateInScene[1][ 0] # the longest string length with largest area is the plate longest_length = len(listOfPossiblePlates[0].strChars) for pla in listOfPossiblePlates: if len(pla.strChars) == longest_length: if pla.rrLocationOfPlateInScene[1][0] > largest_width: licPlate = pla largest_width = pla.rrLocationOfPlateInScene[1][0] cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message # cv2.waitKey(0) return # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out print("----------------------------------------") 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 # end if else cv2.waitKey(0) # hold windows open until user presses a key return
def processImage(filename): jackpot = False imgOriginalScene = cv2.imread(filename) # open image 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates 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 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] print(licPlate.strChars) new_plate = licPlate.strChars plate_exists = Car.objects(plate_number=new_plate) print(plate_exists.first()) if plate_exists.first() is None: enter_car = Car(plate_number=new_plate, enter=datetime.now(), price=0) enter_car.save() # This will perform an insert cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out print("----------------------------------------") 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
def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print("\nerror: KNN traning was not successful\n" ) # show error message return # and exit program # end if list1 = ['sample-b.png', 'sample-a.png', 'sample-c.png'] for list in list1: imgOriginalScene = cv2.imread(list) # open image 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates 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 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] cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars ) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from image = " + licPlate.strChars + "\n") data = { 'license_plate_number': licPlate.strChars, 'timestamp': datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") } # publish_json_with_image(data, 'example.jpg' ,'http://localhost:3000/api/publish_license') # publish_json_with_cv_mat2(data, licPlate.imgThresh, 'http://localhost:3000/api/publish_license', 'afsfsdafdsf') publish_json_with_cv_mat_with_auth( data, imgOriginalScene, 'http://192.41.170.195:3000/api/publish_license', 'twDjXKSO9wwXA0jGYytdNwtt') target = open('out.txt', 'a') target.write(licPlate.strChars + '\n') target.close() # write license plate text to std out print("----------------------------------------") writeLicensePlateCharsOnImage( imgOriginalScene, licPlate) # write license plate text on the image cv2.imshow("imgOriginalScene", imgOriginalScene) # re-show scene image # publish_json_with_cv_mat_with_auth(data,imgOriginalScene, 'http://192.41.170.195:3000/api/publish_license', 'twDjXKSO9wwXA0jGYytdNwtt') print "Procedure Done!" # return cv2.imwrite("imgOriginalScene.png", imgOriginalScene) # write image out to file sleep(10) # end if else # cv2.waitKey(0) # hold windows open until user presses a key return
def calibration (image): WindowName1 = "Calibrating Position of image" WindowName2 = "Color Thresholding" WindowName3 = "Calibrating for Preprocess" #make window cv2.namedWindow(WindowName2) cv2.namedWindow(WindowName3) cv2.namedWindow(WindowName1) # Load saved data from calibrated value (w ,h, rotationx, rotationy, rotationz, panX, panY, stretchX, dist, G_S_F_W, G_S_F_H, A_T_B, A_T_W, T_V, Xtrans, Ytrans) = np.loadtxt("calibrated_value.txt") #convert from load data to xyzwd Xtrans = int(round( Xtrans+100 )) Ytrans = int(round( Ytrans+100 )) xValue = int(round( 100-(rotationx*20000.0))) yValue = int(round((rotationy*20000.0)+100)) zValue = int(round(100-(rotationz*100))) wValue = int(round(100 -((dist-1.0)*200.0))) dValue = int(round((stretchX-1.0)*-200.0 +100)) #make Trackbar cv2.createTrackbar('Xtrans',WindowName1,Xtrans,200,nothing) # for rotation in x axis cv2.createTrackbar('Ytrans',WindowName1,Ytrans,200,nothing) # for rotation in x axis cv2.createTrackbar("Xrot",WindowName1,xValue,200,nothing) # for rotation in x axis cv2.createTrackbar("Yrot",WindowName1,yValue,200,nothing) # for rotation in y axis cv2.createTrackbar("Zrot",WindowName1,zValue,200,nothing) # for rotation in z axis cv2.createTrackbar("ZOOM",WindowName1,wValue,200,nothing) # for Zooming the image cv2.createTrackbar("Strech",WindowName1,dValue,200,nothing) # for strech the image in x axis switch = '0 : OFF \n1 : ON' cv2.createTrackbar(switch, WindowName3,0,1,nothing) # switch to see the preprocess threshold, for more detail see Preprocess.py cv2.createTrackbar('G_S_F_W',WindowName3, int(G_S_F_W),50,nothing) #GAUSSIAN_SMOOTH_FILTER_SIZE_WEIGHT cv2.createTrackbar('G_S_F_H',WindowName3, int(G_S_F_H),50,nothing) #GAUSSIAN_SMOOTH_FILTER_SIZE_HEIGHT cv2.createTrackbar('A_T_B',WindowName3, int(A_T_B),50,nothing) #ADAPTIVE_THRESH_BLOCK_SIZE cv2.createTrackbar('A_T_W',WindowName3, int(A_T_W),50,nothing) #ADAPTIVE_THRESH_WEIGHT cv2.createTrackbar('T_V',WindowName3, int(T_V),255,nothing) #THRESHOLD_VALUE cv2.createTrackbar("RGBSwitch",WindowName2,0,1,nothing) cv2.createTrackbar('Ru',WindowName2,255,255,nothing) cv2.createTrackbar('Gu',WindowName2,255,255,nothing) cv2.createTrackbar('Bu',WindowName2,255,255,nothing) cv2.createTrackbar('Rl',WindowName2,0,255,nothing) cv2.createTrackbar('Gl',WindowName2,0,255,nothing) cv2.createTrackbar('Bl',WindowName2,50,255,nothing) # Allocate destination image backGround1 = np.ones((100,500)) backGround2 = np.ones((100,500)) backGround3 = np.ones((100,500)) # Loop for get trackbar pos and process it while True: # Get position in trackbar for change transform Xtrans = cv2.getTrackbarPos('Xtrans', WindowName1) Ytrans = cv2.getTrackbarPos('Ytrans', WindowName1) X = cv2.getTrackbarPos("Xrot", WindowName1) Y = cv2.getTrackbarPos("Yrot", WindowName1) Z = cv2.getTrackbarPos("Zrot", WindowName1) W = cv2.getTrackbarPos("ZOOM", WindowName1) D = cv2.getTrackbarPos("Strech", WindowName1) # Get position in trackbar for switch S = cv2.getTrackbarPos(switch,WindowName3) #switch for see the calibration threshold # Get the value from tracbar and make it ood and value more than 3 for calibrating threshold G_S_F_W = makeood(cv2.getTrackbarPos('G_S_F_W', WindowName3)) G_S_F_H = makeood(cv2.getTrackbarPos('G_S_F_H', WindowName3)) A_T_B = makeood(cv2.getTrackbarPos('A_T_B', WindowName3)) A_T_W = makeood(cv2.getTrackbarPos('A_T_W', WindowName3)) T_V = float (cv2.getTrackbarPos('T_V', WindowName3)) RGB = cv2.getTrackbarPos("RGBSwitch", WindowName2) Ru = cv2.getTrackbarPos('Ru', WindowName2) Gu = cv2.getTrackbarPos('Gu', WindowName2) Bu = cv2.getTrackbarPos('Bu', WindowName2) Rl = cv2.getTrackbarPos('Rl', WindowName2) Gl = cv2.getTrackbarPos('Gl', WindowName2) Bl = cv2.getTrackbarPos('Bl', WindowName2) lower = np.array([Bl,Gl,Rl],dtype=np.uint8) upper = np.array([Bu,Gu,Ru],dtype=np.uint8) Xtrans = (Xtrans - 100) Ytrans = (Ytrans - 100) rotationx = -(X - 100) / 20000.0 rotationy = (Y - 100) / 20000.0 rotationz = -(Z - 100) / 100.0 dist = 1.0 - (W - 100) / 200.0 stretchX = 1.0 + (D - 100) / -200.0 w = np.size(image, 1) h = np.size(image, 0) panX = 0 panY = 0 hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print("\nerror: KNN traning was not successful\n") # show error message return imaged = imutils.translate(image, Xtrans, Ytrans) # Apply transform M = imutils.getTransform (w, h, rotationx, rotationy, rotationz, panX, panY, stretchX, dist) imgOriginalScene = cv2.warpPerspective(imaged, M, (w,h),cv2.INTER_CUBIC or cv2.WARP_INVERSE_MAP) if (S == 1): imgGrayscale = pp.extractValue(imgOriginalScene) #imgGrayscale = np.invert(imgGrayscale) # last best use this imgMaxContrastGrayscale = pp.maximizeContrast(imgGrayscale) imgMaxContrastGrayscale = np.invert(imgMaxContrastGrayscale) height, width = imgGrayscale.shape imgBlurred = np.zeros((height, width, 1), np.uint8) imgBlurred = cv2.GaussianBlur(imgMaxContrastGrayscale, (G_S_F_H,G_S_F_W), 0) #imgBlurred = np.invert(imgBlurred) imgOriginalScene = cv2.adaptiveThreshold(imgBlurred, T_V , cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, A_T_B, A_T_W) #imgThresh = np.invert(imgThresh) #cv2.imshow("cobaaa", imgThresh) if (RGB == 1): imgOriginalScene = cv2.inRange(imgOriginalScene, lower, upper) # give definition for each initial on image or windows cv2.putText(imgOriginalScene,"Press 's' to save the value",(10,30),cv2.FONT_HERSHEY_SIMPLEX, 0.75,(255,255,255),2,bottomLeftOrigin = False) cv2.putText(imgOriginalScene,"Press 'o' to out the value",(10,60),cv2.FONT_HERSHEY_SIMPLEX, 0.75,(255,255,255),2,bottomLeftOrigin = False) cv2.putText(imgOriginalScene,"Press 'c' to check the result",(10,90),cv2.FONT_HERSHEY_SIMPLEX, 0.75,(255,255,255),2,bottomLeftOrigin = False) cv2.putText(imgOriginalScene,"Press 'esc' to close all windows",(10,120),cv2.FONT_HERSHEY_SIMPLEX, 0.75,(255,255,255),2,bottomLeftOrigin = False) cv2.putText(backGround1,"X for rotating the image in x axis",(10,10),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround1,"Y for rotating the image in y axis",(10,30),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround1,"Z for rotating the image in z axis",(10,50),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround1,"ZOOM for Zoom in or Zoom out the image",(10,70),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround1,"S for streching the image",(10,90),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround2,"R,G,B = Red,Green,Blue",(10,30),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround2,"u,l = Upper and lower",(10,50),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround3,"G_S_F_H = GAUSSIAN_SMOOTH_FILTER_SIZE_HEIGHT",(10,10),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround3,"G_S_F_H = GAUSSIAN_SMOOTH_FILTER_SIZE_WEIGHT",(10,30),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround3,"A_T_B = ADAPTIVE_THRESH_BLOCK_SIZE",(10,50),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround3,"A_T_W = ADAPTIVE_THRESH_WEIGHT",(10,70),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) cv2.putText(backGround3,"T_V = THRESHOLD_VALUE",(10,90),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),1,bottomLeftOrigin = False) # Show in window cv2.imshow("image", imgOriginalScene) cv2.imshow(WindowName1, backGround1) cv2.imshow(WindowName2, backGround2) cv2.imshow(WindowName3, backGround3) ch = cv2.waitKey(5) # chomand switch if ch == ord('c'): # press c to check the result of processing Main.searching(imgOriginalScene,True) cv2.imshow("check",imgOriginalScene) cv2.waitKey(0) return if S == 1 and ch == ord('p') : # press c to check the result of processing imgOriginalScene = np.invert(imgOriginalScene) cv2.imwrite("calib.png",imgOriginalScene) cv2.imshow("calib",imgOriginalScene) return if ch == ord('o'): # press o to see the value print("CAL_VAL =") print( w ,h, rotationx, rotationy, rotationz, panX, panY, stretchX, dist, G_S_F_W, G_S_F_H, A_T_B, A_T_W, T_V, Xtrans, Ytrans) if ch == ord('s'): # press s to save the value CAL_VAL = np.array([[w ,h, rotationx, rotationy, rotationz, panX, panY, stretchX, dist, G_S_F_W, G_S_F_H, A_T_B, A_T_W, T_V, Xtrans, Ytrans ]]) np.savetxt('calibrated_value.txt',CAL_VAL) print( w ,h, rotationx, rotationy, rotationz, panX, panY, stretchX, dist, G_S_F_W, G_S_F_H, A_T_B, A_T_W, T_V, Xtrans, Ytrans) print("Value saved !") if ch == 27: # press esc for exit the calibration break cv2.destroyAllWindows() return
2)) # calculate the lower left origin of the text area ptLowerLeftTextOriginY = int( ptCenterOfTextAreaY + (textSizeHeight / 2)) # based on the text area center, width, and height # write the text on the image cv2.putText(imgOriginalScene, licPlate.strChars, (ptLowerLeftTextOriginX, ptLowerLeftTextOriginY), intFontFace, fltFontScale, SCALAR_YELLOW, intFontThickness) # end function if __name__ == "__main__": blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print("\nerror: KNN traning was not successful\n") while (True): ret, frame = cap.read() imgOriginalScene = frame[0:240, 0:360] k = 0 cv2.imshow("ImgOriginalScene", imgOriginalScene) for i in range(0, 20): main_detect(imgOriginalScene, k) if (filter_list[0] != "No change"): licPlate = filter_list[index_list.index(max(index_list))] drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from image = " + licPlate.strChars +
def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print( "\nerror: KNN traning was not successful\n") # show error message return # and exit program # end if ## imgOriginalScene = cv2.imread("10.png") # open image (grabbed, imgOriginalScene) = camera.read() # grab an image from the camera 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates 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 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] cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate x = licPlate.strChars #print("license plate is" + x + "\n") import pyrebase config = { "apiKey": "AIzaSyBRHhL6vH-qNeOjXyiPiJ3p5JkyNz1KdlU", "authDomain": "registrationuser-2f9aa.firebaseapp.com", "databaseURL": "https://registrationuser-2f9aa.firebaseio.com", "projectId:": "registrationuser-2f9aa", "storageBucket": "registrationuser-2f9aa.appspot.com", "messagingSenderId": "280469289706" } firebase = pyrebase.initialize_app(config) vechile_number = [] db = firebase.database() a = 0 while (1): users = db.child("carin").get() temp = dict(users.val()) key_data = list(users.val()) if (len(key_data) != a): for i in range(len(key_data)): vechile_number.insert(i, temp[key_data[i]]['carvechile']) for j in range(len(key_data)): print(vechile_number[j]) print("-----------------------------------------------") a = len(key_data) print("\nlicense plate read from image to data is = " + x + "\n") # write license plate text to std out # print ("----------------------------------------") 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 # end if else cv2.waitKey(0) # hold windows open until user presses a key return
def main(imgOriginalScene, imgname): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print( "\nerror: KNN traning was not successful\n") # show error message return # and exit program # end if ########################################################################################################## ##imgOriginalScene = cv2.imread("Sample.jpg") # open image ########################################################################################################## 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 # end if imgOriginalScene = ScaleDown.scale800x600( imgOriginalScene) # to scale down the image ###imgOriginalScene = PowerLaw.gammacorrection(imgOriginalScene) # to increase brightness for dark images listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates# #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 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] #cv2.imshow("imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate #cv2.imshow("imgThresh", licPlate.imgThresh) #cv2.imwrite("Plate.png", licPlate.imgPlate) #cv2.imwrite("Thresh.png", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return # and exit program # end if strgs = imgname + " Detected Plate is " + licPlate.strChars + "\n" file = open('Plate.txt', 'a') #writing the detected plate in a txt file. file.write(strgs) drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out print("----------------------------------------") 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
def main(): ##Assign time interval into variable from config/config.ini file timeInterval = int( ConfigManager.ConfigSectionMap("Basic_Conf")['timeinterval']) ## calls main() function every 'timeInterval' seconds threading.Timer(timeInterval, main).start() cv2.useOptimized() # attempt KNN training blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # if KNN training was not successful if blnKNNTrainingSuccessful == False: # show error message print("\nerror: KNN traning was not successful\n") # and exit program return # end if ##open camera and capture image photo_path = CamManager.get_image() # open captured image from directory imgOriginalScene = cv2.imread("captured_img/last.png") # if image was not read successfully if imgOriginalScene is None: # print error message to std out print("\nerror: image not read from file \n\n") # pause so user can see error message os.system("pause") # and exit program return # end if # detect plates listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # detect chars in plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # if no plates were found if len(listOfPossiblePlates) == 0: # inform user no plates were found print("\nno plates were detected\n") else: # if we get in here list of possible plates has at least 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 no chars were found in the plate if len(licPlate.strChars) == 0: # show message print("\nno characters were detected\n\n") # and exit program return # end if # write license plate text to std out print("\ncharacters read from image = " + licPlate.strChars + "\n") print("----------------------------------------") # assign timestamp of measurement to variable 'mTime' mTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # insert data into database; timestamp - measured data - photo path DBManager.insert_data(mTime, int(licPlate.strChars), photo_path) # end if else return
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) cv2.destroyAllWindows() if Main.showSteps == True: # show steps ####################################################### cv2.imshow("0", imgOriginalScene) # end if # show steps ######################################################################### imgGrayscaleScene, imgThreshScene = Preprocess.preprocess( imgOriginalScene) # preprocess to get grayscale and threshold images if Main.showSteps == True: # show steps ####################################################### cv2.imshow("1a", imgGrayscaleScene) cv2.imshow("1b", imgThreshScene) # end if # show steps ######################################################################### # 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) if Main.showSteps == True: # show steps ####################################################### print("step 2 - len(listOfPossibleCharsInScene) = " + str(len(listOfPossibleCharsInScene))) # 131 with MCLRNF1 image imgContours = np.zeros((height, width, 3), np.uint8) contours = [] for possibleChar in listOfPossibleCharsInScene: contours.append(possibleChar.contour) # end for cv2.drawContours(imgContours, contours, -1, Main.SCALAR_WHITE) cv2.imshow("2b", imgContours) # end if # show steps ######################################################################### # 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) if Main.showSteps == True: # show steps ####################################################### print("step 3 - listOfListsOfMatchingCharsInScene.Count = " + str( len(listOfListsOfMatchingCharsInScene))) # 13 with MCLRNF1 image 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) # end for cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed)) # end for cv2.imshow("3", imgContours) # end if # show steps ######################################################################### 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 if Main.showSteps == True: # show steps ####################################################### 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]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), Main.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) # end for print( "\nplate detection complete, click on any image and press a key to begin char recognition . . .\n" ) cv2.waitKey(0) # end if # show steps ######################################################################### return listOfPossiblePlates
def main(): # argument for input video/image/calibration ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help="path to video file") ap.add_argument("-i", "--image", help="Path to the image") ap.add_argument("-c", "--calibration", help="image or video or camera") args = vars(ap.parse_args()) if args.get("calibration", True): imgOriginalScene = cv2.imread(args["calibration"]) if imgOriginalScene is None: print("Please check again the path of image or argument !") imgOriginalScene = imutils.resize(imgOriginalScene, width=640) cal.calibration(imgOriginalScene) return if args.get("video", True): camera = cv2.VideoCapture(args["video"]) if camera is None: print(" Please check again the path of video or argument !") loop = True elif args.get("image", True): imgOriginalScene = cv2.imread("tes_plat_indo/B6703WJF.jpg") if imgOriginalScene is None: print(" Please check again the path of image or argument !") loop = False else: # 0 digunakan untuk camera laptop, 1 untuk camera external camera = cv2.VideoCapture(0) loop = True # add knn library for detect chars blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print( "\nerror: KNN traning was not successful\n") # show error message return count = 0 # not very important, just iterating for license array haha license = [] VER = np.zeros(VERIF) for x in VER: license.append("") numlicense = "" susi = "" knn = 0 # Looping for Video while (loop): # grab the current frame (grabbed, frame) = camera.read() #frame = camera.read() if args.get("video") and not grabbed: break # resize the frame and convert it to grayscale imgOriginalScene = imutils.resize(frame) imgGrayscale, imgThresh = pp.preprocess(imgOriginalScene) cv2.imshow("threshold", imgThresh) # imgOriginalScene = imutils.transform (imgOriginalScene) imgOriginalScene, licenses = searching(imgOriginalScene, loop) # only save 5 same license each time license[count + 1] = licenses nums = license[VERIF - 1] if (license[count] == license[count + 1]): license[count] = license[count + 1] count = count + 1 elif (license[count] != license[count + 1]): coba = license[count + 1] count = 0 license[count] = coba if count == (VERIF - 1): global plat plat = " " plat = list(plat) numstring = "" numstring = list(numstring) alphastring = "" alphastring = list(alphastring) numbers = sum(c.isdigit() for c in nums) words = sum(c.isalpha() for c in nums) for i in nums: if i.isalpha(): alphastring.append(i) elif i.isdigit(): numstring.append(i) print(nums) print(numstring) print(alphastring) #add numbers a = 2 for b in numstring: plat[a] = b a += 1 #add front letter(s) c = 0 sumfront = sum(c.isalpha() for c in nums[0:2]) if (sumfront == 1): for d in nums[0:1]: plat[c] = d c += 1 elif (sumfront == 2): for d in nums[0:2]: plat[c] = d c += 1 #add back letter(s) e = -3 sumback = sum(e.isalpha() for e in nums[-3:]) if (sumback == 1): for f in nums[-1:]: plat[e] = f e += 1 elif (sumback == 2): for f in nums[-2:]: plat[e] = f e += 1 elif (sumback == 3): for f in nums[-3:]: plat[e] = f e += 1 plat = ''.join(plat) if (license[VERIF - 1] == ""): print("no characters were detected\n") else: #if number license same, not be saved if (numlicense == license[VERIF - 1]): print("still = " + numlicense + "\n") elif (len(nums) <= 9 and nums[0] >= 'A' and nums[0] <= 'Z' and numbers <= 4 and words <= 5): numlicense = license[VERIF - 1] print(license[VERIF - 1]) print(numlicense) print("A new license plate read from image = " + plat + "\n") insertdata = data(numlicense) print(insertdata) if check(numlicense): ts = time.time() timestamp = datetime.datetime.fromtimestamp( ts).strftime('%Y-%m-%d') timestamp2 = datetime.datetime.fromtimestamp( ts).strftime('%H:%M:%S') #Ganti Path sesuai dengan laptop masing2 namefile = "/Documents/" + license[ VERIF - 1] + timestamp + timestamp2 + ".png" cv2.imwrite(namefile, imgOriginalScene) count = 0 cv2.putText( imgOriginalScene, "Press 's' to save frame to be 'save.png', for calibrating", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, bottomLeftOrigin=False) #drawRedRectangleAroundPlate(imgOriginalScene, imgOriginalScene) #cv2.rectangle(imgOriginalScene,((imgOriginalScene.shape[1]/2-230),(imgOriginalScene.shape[0]/2-80)),((imgOriginalScene.shape[1]/2+230),(imgOriginalScene.shape[0]/2+80)),SCALAR_GREEN,3) cv2.rectangle(imgOriginalScene, ((int(imgOriginalScene.shape[1] / 2 - 230)), (int(imgOriginalScene.shape[0] / 2 - 80))), ((int(imgOriginalScene.shape[1] / 2 + 230)), (int(imgOriginalScene.shape[0] / 2 + 80))), SCALAR_GREEN, 3) cv2.imshow("imgOriginalScene", imgOriginalScene) key = cv2.waitKey(5) & 0xFF if key == ord('s'): knn = str(knn) savefileimg = "calib_knn/img_" + knn + ".png" savefileThr = "calib_knn/Thr_" + knn + ".png" #cv2.saveimage("save.png", imgOriginalScene) cv2.imwrite(savefileimg, frame) cv2.imwrite(savefileThr, imgThresh) print("image save !") knn = int(knn) knn = knn + 1 if key == 27: # if the 'q' key is pressed, stop the loop break camera.release() # cleanup the camera and close any open windows # For image only if (loop == False): imgOriginalScene = imutils.resize(imgOriginalScene, width=2000) cv2.imshow("original", imgOriginalScene) imgGrayscale, imgThresh = pp.preprocess(imgOriginalScene) cv2.imshow("threshold", imgThresh) #imgOriginalScene = imutils.transform (imgOriginalScene) imgOriginalScene, license = searching(imgOriginalScene, loop) #imgOriginalScene = imutils.detransform(imgOriginalScene) cv2.waitKey(0) cv2.waitKey(0) cv2.destroyAllWindows() return
def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print( "\nerror: KNN traning was not successful\n") # show error message return # and exit program # end if imgOriginalScene = cv2.imread( "LicPlateImages/gh/plates/c3.jpg") # open image # imgOriginalScene = imgOriginalScene[100:+50, 100:100+50] # cv2.imshow("cropped", crop_img) pytesseract.pytesseract.tesseract_cmd = r'C://Program Files (x86)//Tesseract-OCR//tesseract.exe' # print(pytesseract.image_to_string(Image.open('LicPlateImages/gh/plates/c3.jpg').convert('LA'))) 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates 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 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] cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) print(pytesseract.image_to_string(licPlate.imgThresh) ) #Reads the chars from the grayscaled image using tesseract if len(licPlate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out print("----------------------------------------") print("Tesseract OCR = " + pytesseract.image_to_string(licPlate.imgThresh)) print("----------------------------------------") 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 # end if else cv2.waitKey(0) # hold windows open until user presses a key return
fltFontScale, SCALAR_YELLOW, intFontThickness) # end function ################################################################################################### if __name__ == "__main__": filePath = "30.jpg" imgOriginalScene = cv2.imread(filePath) # open image listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene, filePath) # detect plates listOfPlates = DetectChars.detectCharsInPlates( imgOriginalScene, listOfPossiblePlates, filePath) # detect chars in plates print("Number of Plates Found:", len(listOfPlates)) # cv2.imshow("imgOriginalScene", imgOriginalScene) # show scene image if len(listOfPlates) == 0: # if no plates were found print("\nno license plates were detected\n" ) # inform user no plates were found else: # else # suppose the plate with the most recognized chars (the first plate in sorted by string length descending order) is the actual plate for i, licPlate in enumerate(listOfPlates): cv2.imshow( "imgPlate_" + str(i), licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh_" + str(i), licPlate.imgThresh)
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 Main.showSteps == True: cv2.imshow("0", imgOriginalScene) imgGrayscaleScene, imgThreshScene = Preprocess.preprocess(imgOriginalScene) if Main.showSteps == True: cv2.imshow("1a", imgGrayscaleScene) cv2.imshow("1b", imgThreshScene) listOfPossibleCharsInScene = findPossibleCharsInScene(imgThreshScene) if Main.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, Main.SCALAR_WHITE) cv2.imshow("2b", imgContours) listOfListsOfMatchingCharsInScene = DetectChars.findListOfListsOfMatchingChars( listOfPossibleCharsInScene) if Main.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") if Main.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]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), Main.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
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 listOfPossibleCharsInScene = findPossibleCharsInScene(imgThreshScene) if Main.showSteps == True: imgContours = np.zeros((height, width, 3), np.uint8) contours = [] for possibleChar in listOfPossibleCharsInScene: contours.append(possibleChar.contour) # end for cv2.drawContours(imgContours, contours, -1, Main.SCALAR_WHITE) cv2.imshow("2b", imgContours) cv2.waitKey(0) # 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) if Main.showSteps == True: # show steps ####################################################### 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) # end for cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed)) # end for cv2.imshow("3", imgContours) # end if # show steps ######################################################################### 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 if Main.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]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), Main.SCALAR_RED, 2) text = pytesseract.image_to_string(listOfPossiblePlates[i].imgPlate, lang='eng') # print("Number is:", text) print("\nplate detection complete...\n") cv2.waitKey(0) return listOfPossiblePlates
def cbPose(self, image_msg): narr = np.fromstring(image_msg.data, np.uint8) image = cv2.imdecode(narr, cv2.CV_LOAD_IMAGE_COLOR) ############################################################################################## # module level variables ########################################################################## SCALAR_BLACK = (0.0, 0.0, 0.0) SCALAR_WHITE = (255.0, 255.0, 255.0) SCALAR_YELLOW = (0.0, 255.0, 255.0) SCALAR_GREEN = (0.0, 255.0, 0.0) SCALAR_RED = (0.0, 0.0, 255.0) showSteps = False blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print "\nerror: KNN traning was not successful\n" # show error message return # and exit program # end if imgOriginalScene = image #cv2.imread("1.png") # open image #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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates) # detect chars in plates 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 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] cv2.imshow("imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print "\nno characters were detected\n\n" # show message return # and exit program # end if drawRedRectangleAroundPlate(imgOriginalScene, licPlate) # draw red rectangle around plate print "\nlicense plate read from image = " + licPlate.strChars + "\n" # write license plate text to std out print "----------------------------------------" 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 image = imgOriginalScene # end if else ############################################################################################## # print "Found {0} faces!".format(len(faces)) # for (x, y, w, h) in faces: # cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) # cv2.imshow("preview", image) # cv2.waitKey(0) # cv2.destroyAllWindows() #cv2.rectangle(image, (100, 100), (120, 140), (0, 255, 0), 2) #image_msg_out = self.bridge.cv2_to_imgmsg(mask, "mono8") image_msg_out = self.bridge.cv2_to_imgmsg(image, "bgr8") image_msg_out.header.stamp = image_msg.header.stamp self.pub_image_original.publish(image_msg_out) #image_msg_out = self.bridge.cv2_to_imgmsg(gray, "bgr8") #image_msg_out.header.stamp = image_msg.header.stamp #self.pub_image_gray.publish(image_msg_out) #face_cascade = cv2.CascadeClassifier('~/haarcascade_frontalface_default.xml') #eye_cascade = cv2.CascadeClassifier('~/haarcascade_eye.xml') #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #faces = face_cascade.detectMultiScale(gray, 1.3, 5) #for (x,y,w,h) in faces: # cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # roi_gray = gray[y:y+h, x:x+w] # roi_color = img[y:y+h, x:x+w] # self.eyes = eye_cascade.detectMultiScale(roi_gray) #for (ex,ey,ew,eh) in self.eyes: # cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2) # cv2.imwrite('hard.png',img) # cv2.destroyAllWindows() car_control_msg = Twist2DStamped()
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) cv2.destroyAllWindows() if Main.showSteps == True: # show steps ####################################################### cv2.imshow("0", imgOriginalScene) # end if # show steps ######################################################################### imgGrayscaleScene, imgThreshScene = Preprocess.preprocess(imgOriginalScene) # preprocess to get grayscale and threshold images if Main.showSteps == True: # show steps ####################################################### cv2.imshow("1a", imgGrayscaleScene) cv2.imshow("1b", imgThreshScene) # end if # show steps ######################################################################### # 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) if Main.showSteps == True: # show steps ####################################################### print "step 2 - len(listOfPossibleCharsInScene) = " + str(len(listOfPossibleCharsInScene)) # 131 with MCLRNF1 image imgContours = np.zeros((height, width, 3), np.uint8) contours = [] for possibleChar in listOfPossibleCharsInScene: contours.append(possibleChar.contour) # end for cv2.drawContours(imgContours, contours, -1, Main.SCALAR_WHITE) cv2.imshow("2b", imgContours) # end if # show steps ######################################################################### # 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) if Main.showSteps == True: # show steps ####################################################### print "step 3 - listOfListsOfMatchingCharsInScene.Count = " + str(len(listOfListsOfMatchingCharsInScene)) # 13 with MCLRNF1 image 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) # end for cv2.drawContours(imgContours, contours, -1, (intRandomBlue, intRandomGreen, intRandomRed)) # end for cv2.imshow("3", imgContours) # end if # show steps ######################################################################### 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 if Main.showSteps == True: # show steps ####################################################### 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]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), Main.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) # end for print "\nplate detection complete, click on any image and press a key to begin char recognition . . .\n" cv2.waitKey(0) # end if # show steps ######################################################################### return listOfPossiblePlates
def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() if blnKNNTrainingSuccessful == False: print("\nerror: KNN traning was not successful\n") return print("Enter file name(with extension)") klmn = input() imgOriginalScene = cv2.imread(klmn) if imgOriginalScene is None: print("\nerror: image not read from file \n\n") os.system("pause") return listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) cv2.imshow("imgOriginalScene", imgOriginalScene) if len(listOfPossiblePlates) == 0: print("\nno license plates were detected\n") else: listOfPossiblePlates.sort( key=lambda possiblePlate: len(possiblePlate.strChars), reverse=True) licPlate = listOfPossiblePlates[0] cv2.imshow("imgPlate", licPlate.imgPlate) cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: print("\nno characters were detected\n\n") return drawRedRectangleAroundPlate(imgOriginalScene, licPlate) print("\nlicense plate read from image = " + licPlate.strChars + "\n") file = open("num.txt", "w") file.write(licPlate.strChars) file.close() print("----------------------------------------") writeLicensePlateCharsOnImage(imgOriginalScene, licPlate) cv2.imshow("imgOriginalScene", imgOriginalScene) cv2.imwrite("imgOriginalScene.png", imgOriginalScene) cv2.waitKey(0) return
SCALAR_WHITE = (255.0, 255.0, 255.0) SCALAR_YELLOW = (0.0, 255.0, 255.0) SCALAR_GREEN = (0.0, 255.0, 0.0) SCALAR_RED = (0.0, 0.0, 255.0) showSteps = False def drawRedRectangleAroundPlate(imgOriginalScene, licPlate): p2fRectPoints = cv2.boxPoints(licPlate.rrLocationOfPlateInScene) # get 4 vertices of rotated rect cv2.line(imgOriginalScene, tuple(p2fRectPoints[0]), tuple(p2fRectPoints[1]), SCALAR_RED, 2) # draw 4 red lines cv2.line(imgOriginalScene, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), SCALAR_RED, 2) cv2.line(imgOriginalScene, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), SCALAR_RED, 2) cv2.line(imgOriginalScene, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), SCALAR_RED, 2) blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training imgOriginalScene = cv2.imread("Dataset/58.jpg") # open image listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates) # detect chars in plates cv2.imshow("imgOriginalScene", imgOriginalScene) # show scene image gray = cv2.cvtColor(imgOriginalScene, cv2.COLOR_BGR2GRAY) cv2.imshow("gray",gray) cv2.waitKey(1) if len(listOfPossiblePlates) == 0: # if no plates were found print("\nno license plates were detected\n") # inform user no plates were found else: # else 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] pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" text = pytesseract.image_to_string(licPlate.imgPlate)
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) if Main.showSteps == True: cv2.imshow("0", imgOriginalScene) imgGrayscaleScene, imgThreshScene = Preprocess.preprocess(imgOriginalScene) if Main.showSteps == True: cv2.imshow("1a", imgGrayscaleScene) cv2.imshow("1b", imgThreshScene) listOfPossibleCharsInScene = findPossibleCharsInScene(imgThreshScene) if Main.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, Main.SCALAR_WHITE) cv2.imshow("2b", imgContours) listOfListsOfMatchingCharsInScene = DetectChars.findListOfListsOfMatchingChars(listOfPossibleCharsInScene) if Main.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) if Main.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]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), Main.SCALAR_RED, 2) cv2.line(imgContours, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), Main.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
def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN() # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print "\nerror: KNN traning was not successful\n" # show error message return # and exit program # end if imgOriginalScene = cv2.imread("../frame.png") # open image 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 # end if dateTime = str(datetime.datetime.now().strftime('%d/%m/%Y'))+" "+strftime("%H:%M:%S", time.localtime()) listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates) # detect chars in plates ## msg = str(len(listOfPossiblePlates))+" plaka bulundu" ## server.sendmail("*****@*****.**", "*****@*****.**", msg) try: msg = MIMEMultipart() msg['Subject'] = dateTime msg['From'] = "*****@*****.**" msg['To'] = "*****@*****.**" fp = open('1.png', 'rb') img = MIMEImage(fp.read()) fp.close() msg.attach(img) except: print("MSG ERROR") # cv2.imshow("imgOriginalScene", imgOriginalScene) # show scene image # print("Saat: "+strftime("%H:%M:%S", time.localtime())) if len(listOfPossiblePlates) == 0: # if no plates were found ser.write('0\n') print "\nno license plates were detected\n" # inform user no plates were found ##text = "Plaka Bulunamadi\nCekilen Resim Asagidadir" else: # else ser.write('1\n') print("Plate Detected") ## # 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] ##file = open("plates.txt", "a") ##file.write(dateTime+" ") ##file.write(str(licPlate)+"\n") ##file.close() ## # cv2.imshow("imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate # cv2.imshow("imgThresh", licPlate.imgThresh) ## ## if len(licPlate.strChars) == 0: # if no chars were found in the plate ## print "\nno characters were detected\n\n" # show message ## return # and exit program ## # end if ## drawRedRectangleAroundPlate(imgOriginalScene, licPlate) # draw red rectangle around plate ## ## print "\nlicense plate read from image = " + licPlate.strChars + "\n" # write license plate text to std out ## print "----------------------------------------" 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 try: fp = open('imgOriginalScene.png', 'rb') img = MIMEImage(fp.read()) fp.close() msg.attach(img) text = "Plaka Bulundu\nCekilen Resim Orjinal ve Plakanin Gosterildigi Hali Asagidadir" except: print("IMG ERROR") try: msg.attach(MIMEText(text, 'plain')) ##server.sendmail("*****@*****.**", "*****@*****.**", msg.as_string()) except: print("SEND MAIL ERROR") ## # end if else ## cv2.waitKey(0) # hold windows open until user presses a key # print("Saat: "+strftime("%H:%M:%S", time.localtime())) return
def searching(imgOriginalScene, loop): licenses = "" if imgOriginalScene is None: # if image was not read successfully print("error: image not read from file \n" ) # print error message to std out os.system("pause") # pause so user can see error message return # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates #time.sleep(0.02) listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates #time.sleep(0.05) if (loop == False): cv2.imshow("imgOriginalScene", imgOriginalScene) if len(listOfPossiblePlates) == 0: if (loop == False): # if no plates were found print("no license plates were detected\n" ) # inform user no plates were found 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 (loop == False): cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate if (loop == False): print("no characters were detected\n") return # show message # end if drawRedRectangleAroundPlate(imgOriginalScene, licPlate) writeLicensePlateCharsOnImage(imgOriginalScene, licPlate) licenses = licPlate.strChars # if ((licenses[0] and licenses[len(licenses)-1]) == ('0' or '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9')): # licenses = "" # print("license plate False !! \n and ") # draw red rectangle around plate #print (licenses) #print(licPlate) if (loop == False): print("license plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out # write license plate text on the image if (loop == False): cv2.imshow("imgOriginalScene", imgOriginalScene) # re-show scene image cv2.imwrite("imgOriginalScene.png", imgOriginalScene) return imgOriginalScene, licenses
def recognize(path): train_success = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if not train_success: # if KNN training was not successful print("\nerror: KNN training was not successful\n") return None, None original_scene = cv2.imread(path) if original_scene is None: 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 None, None possible_plates = DetectPlates.detectPlatesInScene( original_scene) # detect plates possible_plates = DetectChars.detectCharsInPlates( possible_plates) # detect chars in plates # cv2.imshow("imgOriginalScene", original_scene) if len(possible_plates) == 0: print("\nno license plates were detected\n") 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) possible_plates.sort( key=lambda possible_plate: len(possible_plate.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 plate = possible_plates[0] # cv2.imshow("imgPlate", plate.imgPlate) # show crop of plate and threshold of plate cv2.imwrite("imgThresh.png", plate.imgThresh) if len(plate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message cv2.waitKey(0) return None, None highlight_plate(original_scene, plate) # draw red rectangle around plate print("\nlicense plate read from image = " + plate.strChars + "\n") # write license plate text to std out print("----------------------------------------") plate.strChars = process_number(plate.strChars) write_chars(original_scene, plate) # write license plate text on the image # cv2.imshow("imgOriginalScene", original_scene) # re-show scene image cv2.imwrite("imgOriginalScene.png", original_scene) # write image out to file return original_scene, plate.strChars cv2.waitKey(0) return None, None
def main(): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training if blnKNNTrainingSuccessful == False: # if KNN training was not successful print( "\nerror: KNN traning was not successful\n") # show error message return # and exit program # end if sample = [ filedialog.askopenfilename(initialdir="animals/", title="Select Image", filetype=(("png files", "*.png"), ("All Files", "*.*"))) ] print(sample) imgOriginalScene = cv2.imread(sample[0]) # open image 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 # end if listOfPossiblePlates = DetectPlates.detectPlatesInScene( imgOriginalScene) # detect plates listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # detect chars in plates 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 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] print(licPlate.strChars) x = (licPlate.strChars) sql = "INSERT INTO lnumber VALUES(%s,%s,%s,%s)" mycursor.execute(sql, (date, current.hour, current.minute, x)) mydb.commit() cv2.imshow( "imgPlate", licPlate.imgPlate) # show crop of plate and threshold of plate cv2.imshow("imgThresh", licPlate.imgThresh) if len(licPlate.strChars) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return # and exit program # end if drawRedRectangleAroundPlate( imgOriginalScene, licPlate) # draw red rectangle around plate print("\nlicense plate read from image = " + licPlate.strChars + "\n") # write license plate text to std out print("----------------------------------------") writeLicensePlateCharsOnImage( imgOriginalScene, licPlate) # write license plate text on the image cv2.imshow("imgOriginalScene", imgOriginalScene) # re-show scene image cv2.imwrite("imgOriginalScene2.png", imgOriginalScene) # write image out to file # end if else cv2.waitKey(0) # hold windows open until user presses a key return
def main(self): blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( ) # attempt KNN training (True ya da False) if blnKNNTrainingSuccessful == False: # if KNN training was not successful print("\n error: KNN traning was not successful \n" ) # show error message return # and exit program imgOriginalScene = cv2.imread(self.fname) # open image if imgOriginalScene is None: # if image was not read successfully print("\n error: 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 (plakaları algıla) listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates ) # detect chars in plates (Plakadaki karakterleri algıla) if len(listOfPossiblePlates ) == 0: # if no plates were found --> eğer plaka bulunmuyorsa print( "\n no license plates were detected\n" ) # inform user no plates were found (hiçbir plaka bulunamadı) else: # else listOfPossiblePlates.sort( key=lambda possiblePlate: len(possiblePlate.strChars), reverse=True) self.licPlate = listOfPossiblePlates[0] if len(self.licPlate.strChars ) == 0: # if no chars were found in the plate print("\nno characters were detected\n\n") # show message return # and exit program self.drawRedRectangleAroundPlate( imgOriginalScene, self.licPlate) # draw red rectangle around plate # print ("\n license plate read from image = " + self.licPlate.strChars + "\n") # write license plate text to std out self.writeLicensePlateCharsOnImage( imgOriginalScene, self.licPlate) # write license plate text on the image self.alan = self.licPlate.imgPlate self.plaka = self.licPlate.strChars self.karakterLabel.setText(self.plaka) self.karakterLabel.setReadOnly(True) self.karakterLabel.setAlignment(QtCore.Qt.AlignHCenter) qformat = QImage.Format_Indexed8 if len(self.alan.shape) == 3: if (self.alan.shape[2]) == 4: qformat = QImage.Format_RGBA8888 else: qformat = QImage.Format_RGB888 img = QImage(self.alan, self.alan.shape[1], self.alan.shape[0], self.alan.strides[0], qformat) img = img.rgbSwapped() self.plakaLabel.setPixmap(QPixmap.fromImage(img)) self.plakaLabel.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) plakaYaz(self.plaka) cv2.waitKey( 0 ) # hold windows open until user presses a key (Kullanıcı bir tuşa basana kadar pencereleri açık tutar)