def __init__(self):
        # self.modelArg = "datasets/experiment_faster_rcnn/2018_08_02/exported_model/frozen_inference_graph.pb"
        self.modelArg = "datasets/experiment_ssd/2018_07_25_14-00/exported_model/frozen_inference_graph.pb"
        self.labelsArg = "datasets/records/classes.pbtxt"
        self.num_classesArg = 37
        self.min_confidenceArg = 0.5

        # initialize the model
        self.model = tf.Graph()
        # create a context manager that makes this model the default one for
        # execution
        with self.model.as_default():
            # initialize the graph definition
            self.graphDef = tf.GraphDef()

            # load the graph from disk
            with tf.gfile.GFile(self.modelArg, "rb") as f:
                self.serializedGraph = f.read()
                self.graphDef.ParseFromString(self.serializedGraph)
                tf.import_graph_def(self.graphDef, name="")

        # load the class labels from disk
        self.labelMap = label_map_util.load_labelmap(self.labelsArg)
        self.categories = label_map_util.convert_label_map_to_categories(
            self.labelMap, max_num_classes=self.num_classesArg,
            use_display_name=True)
        self.categoryIdx = label_map_util.create_category_index(self.categories)

        # create a plateFinder
        self.plateFinder = PlateFinder(self.min_confidenceArg, self.categoryIdx,
                                       rejectPlates=False, charIOUMax=0.3)

        # create plate displayer
        self.plateDisplay = PlateDisplay()
Esempio n. 2
0
    # load the graph from disk
    with tf.gfile.GFile(args["model"], "rb") as f:
        serializedGraph = f.read()
        graphDef.ParseFromString(serializedGraph)
        tf.import_graph_def(graphDef, name="")

# load the class labels from disk
labelMap = label_map_util.load_labelmap(args["labels"])
categories = label_map_util.convert_label_map_to_categories(
    labelMap, max_num_classes=args["num_classes"], use_display_name=True)
categoryIdx = label_map_util.create_category_index(categories)

# create a plateFinder
plateFinder = PlateFinder(args["min_confidence"],
                          categoryIdx,
                          rejectPlates=False,
                          charIOUMax=0.3)

# create plate displayer
plateDisplay = PlateDisplay()

# create a session to perform inference
with model.as_default():
    with tf.Session(graph=model) as sess:
        # create a predicter, used to predict plates and chars
        predicter = Predicter(model, sess, categoryIdx)

        imagePaths = paths.list_images(args["imagePath"])
        frameCnt = 0
        start_time = time.time()
        # Loop over all the images
class DetectVehicleNumberPlate:
    def __init__(self):
        # self.modelArg = "datasets/experiment_faster_rcnn/2018_08_02/exported_model/frozen_inference_graph.pb"
        self.modelArg = "datasets/experiment_ssd/2018_07_25_14-00/exported_model/frozen_inference_graph.pb"
        self.labelsArg = "datasets/records/classes.pbtxt"
        self.num_classesArg = 37
        self.min_confidenceArg = 0.5

        # initialize the model
        self.model = tf.Graph()
        # create a context manager that makes this model the default one for
        # execution
        with self.model.as_default():
            # initialize the graph definition
            self.graphDef = tf.GraphDef()

            # load the graph from disk
            with tf.gfile.GFile(self.modelArg, "rb") as f:
                self.serializedGraph = f.read()
                self.graphDef.ParseFromString(self.serializedGraph)
                tf.import_graph_def(self.graphDef, name="")

        # load the class labels from disk
        self.labelMap = label_map_util.load_labelmap(self.labelsArg)
        self.categories = label_map_util.convert_label_map_to_categories(
            self.labelMap, max_num_classes=self.num_classesArg,
            use_display_name=True)
        self.categoryIdx = label_map_util.create_category_index(self.categories)

        # create a plateFinder
        self.plateFinder = PlateFinder(self.min_confidenceArg, self.categoryIdx,
                                       rejectPlates=False, charIOUMax=0.3)

        # create plate displayer
        self.plateDisplay = PlateDisplay()

    def predictImages(self, imagePathArg, pred_stagesArg, croppedImagepath, numPlateOrg):
        # create a session to perform inference
        with numPlateOrg.model.as_default():
            with tf.Session(graph=numPlateOrg.model) as sess:
                # create a predicter, used to predict plates and chars
                predicter = Predicter(numPlateOrg.model, sess, numPlateOrg.categoryIdx)
                # load the image from disk
                # print("[INFO] Loading image \"{}\"".format(imagePaths))
                image = cv2.imread(imagePathArg)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                # image = ImageEnhance.Sharpness(imagePathArg)
                # If prediction stages == 2, then perform prediction on full image, find the plates, crop the plates from the image,
                # and then perform prediction on the plate images

                if pred_stagesArg == 2:
                    # Perform inference on the full image, and then select only the plate boxes
                    boxes, scores, labels = predicter.predictPlates(image, preprocess=False)
                    licensePlateFound_pred, plateBoxes_pred, plateScores_pred = self.plateFinder.findPlatesOnly(
                        boxes,
                        scores,
                        labels)
                    imageLabelled = self.getBoundingBox(image, plateBoxes_pred, imagePathArg, croppedImagepath)

                else:
                    print("[ERROR] --pred_stages {}. The number of prediction stages must be either 1 or 2".format(
                        pred_stagesArg))
                    quit()

                return imageLabelled

    def getBoundingBox(self, image, plateBoxes, imagePath, croppedImagepath):
        (H, W) = image.shape[:2]

        for plateBox in plateBoxes:
            # Draw the plate box rectangle in red
            # scale the bounding box from the range [0, 1] to [W, H]
            (startY, startX, endY, endX) = plateBox
            startX = int(startX * W)
            startY = int(startY * H)
            endX = int(endX * W)
            endY = int(endY * H)
            # croppedimage = crop(imagePath, (startX, startY, endX, endY), croppedImagepath)

            try:
                image_obj = Image.open(imagePath)

                cropped_image = image_obj.crop((startX, startY, endX, endY))
                cropped_image = cropped_image.convert("L")
                # cropped_image = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2GRAY)
                cropped_image.save(croppedImagepath)
                return cropped_image
            except Exception as e:
                print(e)
Esempio n. 4
0
def predictImages(modelArg, labelsArg, imagePathArg, num_classesArg,
                  min_confidenceArg, image_displayArg, pred_stagesArg):

    # initialize the model
    model = tf.Graph()

    # create a context manager that makes this model the default one for
    # execution
    with model.as_default():
        # initialize the graph definition
        graphDef = tf.GraphDef()

        # load the graph from disk
        with tf.gfile.GFile(modelArg, "rb") as f:
            serializedGraph = f.read()
            graphDef.ParseFromString(serializedGraph)
            tf.import_graph_def(graphDef, name="")

    # load the class labels from disk
    labelMap = label_map_util.load_labelmap(labelsArg)
    categories = label_map_util.convert_label_map_to_categories(
        labelMap, max_num_classes=num_classesArg, use_display_name=True)
    categoryIdx = label_map_util.create_category_index(categories)

    # create a plateFinder
    plateFinder = PlateFinder(min_confidenceArg,
                              categoryIdx,
                              rejectPlates=False,
                              charIOUMax=0.3)

    # create plate displayer
    plateDisplay = PlateDisplay()

    # create a session to perform inference
    with model.as_default():
        with tf.Session(graph=model) as sess:
            # create a predicter, used to predict plates and chars
            predicter = Predicter(model, sess, categoryIdx)

            imagePaths = paths.list_images(imagePathArg)
            frameCnt = 0
            start_time = time.time()
            # Loop over all the images
            for imagePath in imagePaths:
                frameCnt += 1

                # load the image from disk
                print("[INFO] Loading image \"{}\"".format(imagePath))
                image = cv2.imread(imagePath)
                (H, W) = image.shape[:2]

                # If prediction stages == 2, then perform prediction on full image, find the plates, crop the plates from the image,
                # and then perform prediction on the plate images
                if pred_stagesArg == 2:
                    # Perform inference on the full image, and then select only the plate boxes
                    boxes, scores, labels = predicter.predictPlates(
                        image, preprocess=True)
                    licensePlateFound_pred, plateBoxes_pred, plateScores_pred = plateFinder.findPlatesOnly(
                        boxes, scores, labels)
                    # loop over the plate boxes, find the chars inside the plate boxes,
                    # and then scrub the chars with 'processPlates', resulting in a list of final plateBoxes, char texts, char boxes, char scores and complete plate scores
                    plates = []
                    for plateBox in plateBoxes_pred:
                        boxes, scores, labels = predicter.predictChars(
                            image, plateBox)
                        chars = plateFinder.findCharsOnly(
                            boxes, scores, labels, plateBox, image.shape[0],
                            image.shape[1])
                        if len(chars) > 0:
                            plates.append(chars)
                        else:
                            plates.append(None)
                    plateBoxes_pred, charTexts_pred, charBoxes_pred, charScores_pred, plateAverageScores_pred = plateFinder.processPlates(
                        plates, plateBoxes_pred, plateScores_pred)

                # If prediction stages == 1, then predict the plates and characters in one pass
                elif pred_stagesArg == 1:
                    # Perform inference on the full image, and then find the plate text associated with each plate
                    boxes, scores, labels = predicter.predictPlates(
                        image, preprocess=False)
                    licensePlateFound_pred, plateBoxes_pred, charTexts_pred, charBoxes_pred, charScores_pred, plateScores_pred = plateFinder.findPlates(
                        boxes, scores, labels)
                else:
                    print(
                        "[ERROR] --pred_stages {}. The number of prediction stages must be either 1 or 2"
                        .format(pred_stagesArg))
                    quit()

                # Print plate text
                for charText in charTexts_pred:
                    print("    Found: ", charText)

                # Display the full image with predicted plates and chars
                if image_displayArg == True:
                    imageLabelled = plateDisplay.labelImage(
                        image, plateBoxes_pred, charBoxes_pred, charTexts_pred)
                    cv2.imshow("Labelled Image", imageLabelled)
                    cv2.waitKey(0)

            # print some performance statistics
            curTime = time.time()
            processingTime = curTime - start_time
            fps = frameCnt / processingTime
            print(
                "[INFO] Processed {} frames in {:.2f} seconds. Frame rate: {:.2f} Hz"
                .format(frameCnt, processingTime, fps))
            platesReturn = []
            for i, plateBox in enumerate(plateBoxes_pred):
                #platesReturn[i] = { 'plateBoxLoc': plateBox, 'plateText': charTexts_pred[i], 'charBoxLocs': charBoxes_pred[i]}
                platesReturn.append({
                    'plateText':
                    charTexts_pred[i],
                    'plateBoxLoc':
                    list(plateBox),
                    'charBoxLocs':
                    list([list(x) for x in charBoxes_pred[i]])
                })

            #results = results.encode('utf-8')
            return {"numPlates": len(platesReturn), "plates": platesReturn}
Esempio n. 5
0
    # load the graph from disk
    with tf.gfile.GFile(args["model"], "rb") as f:
        serializedGraph = f.read()
        graphDef.ParseFromString(serializedGraph)
        tf.import_graph_def(graphDef, name="")

# load the class labels from disk
labelMap = label_map_util.load_labelmap(args["labels"])
categories = label_map_util.convert_label_map_to_categories(
    labelMap, max_num_classes=args["num_classes"], use_display_name=True)
categoryIdx = label_map_util.create_category_index(categories)

# create a plateFinder
plateFinder = PlateFinder(args["min_confidence"],
                          categoryIdx,
                          rejectPlates=True)

# create a plate xml extractor
plateXmlExtract = PlateXmlExtract(args["labels"])

# create a plate comparator
plateCompare = PlateCompare()

# create plate displayer
plateDisplay = PlateDisplay()

# create a session to perform inference
with model.as_default():
    with tf.Session(graph=model) as sess:
        # create a predicter, used to predict plates and chars
Esempio n. 6
0
categories = label_map_util.convert_label_map_to_categories(
    labelMap, max_num_classes=conf["num_classes"], use_display_name=True)
categoryIdx = label_map_util.create_category_index(categories)

# open the logFile, create if it does not exist, otherwise open in append mode
logFilePath = "{}/{}".format(conf["output_image_path"], conf["log_file_name"])
if (os.path.isdir(conf["output_image_path"]) != True):
    os.makedirs(conf["output_image_path"])
if os.path.exists(logFilePath) == False:
    logFile = open(logFilePath, "w")
else:
    logFile = open(logFilePath, "a")

# create a plateFinder and load the plate history utility
plateFinder = PlateFinder(conf["min_confidence"],
                          categoryIdx,
                          rejectPlates=reject_poor_quality_plates,
                          charIOUMax=conf["charIOUMax"])
folderController = FolderControl()
plateHistory = PlateHistory(
    conf["output_image_path"],
    logFile,
    saveAnnotatedImage=conf["saveAnnotatedImage"] == "true")
quit = False
plateLogLatency = conf["plateLogLatency"] * conf["videoFrameRate"]
platesReadyForLog = False
myPaths = paths.list_files(conf["input_video_path"],
                           validExts=(".h264", "mp4", "mov", "ts"))

# loop over all the detected video files
for videoPath in sorted(myPaths):
    print("[INFO] reading video file {}...".format(videoPath))