Esempio n. 1
0
def pipeline(foto):
    alpr = Alpr("us", "/etc/openalpr/openalpr.conf", "openalpr/runtime_data/")
    if not alpr.is_loaded():
        print("Error loading OpenALPR")
        sys.exit(1)

    alpr.set_top_n(7)
    alpr.set_default_region("md")

    results = alpr.recognize_ndarray(foto)

    i = 0
    for plate in results['results']:
        i += 1
        #print("Plate #%d" % i)
        #print("   %12s %12s" % ("Plate", "Confidence"))
        for candidate in plate['candidates']:
            #	prefix = "-"
            #	if candidate['matches_template']:
            #		prefix = "*"
            if len(candidate["plate"]
                   ) == 6 and candidate["confidence"] >= 50.0:
                print("Plate: " + str(candidate["plate"]) + " Confidence:" +
                      str(candidate["confidence"]))

        #	print("  %s %12s%12f" % (prefix, candidate['plate'], candidate['confidence']))

    # Call when completely done to release memory
    alpr.unload()
Esempio n. 2
0
def recognize_license_plate(image, obj_meta, confidence):

    #silly way to detect license plates
    if (obj_meta.class_id == 1):
        #Importing openalpr
        print('Importing alpr')
        alpr_engine = Alpr("eu", "/etc/openalpr/openalpr_rai.conf",
                           "/usr/share/openalpr/runtime_data")

        if not alpr_engine.is_loaded():
            print("Error loading OpenALPR")
            sys.exit(1)

        alpr_engine.set_top_n(10)
        alpr_engine.set_default_region("fi")

        frame_number = random.randint(0, 100)
        rect_params = obj_meta.rect_params
        top = int(rect_params.top)
        left = int(rect_params.left)
        width = int(rect_params.width)
        height = int(rect_params.height)
        lp_cutout = image[top:top + height, left:left + width, :]

        cv2.imwrite(
            "/opt/nvidia/deepstream/deepstream-5.0/sources/python/apps/deepstream-test10-helloworld6/"
            + str(frame_number) + "_lpcut.jpg", lp_cutout)
        cv2.imwrite(
            "/opt/nvidia/deepstream/deepstream-5.0/sources/python/apps/deepstream-test10-helloworld6/"
            + str(frame_number) + "_image.jpg", image)

        results = alpr_engine.recognize_ndarray(lp_cutout)
        print(results)
        alpr_engine.unload()
Esempio n. 3
0
class ALPRContainer(rpc.ModelContainerBase):
    def __init__(self):
        self.alpr = Alpr(country='us',
                         config_file='/etc/openalpr/openalpr.conf',
                         runtime_dir='/usr/share/openalpr/runtime_data')

    def predict_floats(self, inputs):
        int_inputs = (np.array(inputs) * 255).astype(np.uint8)
        reshaped_input = [arr.reshape(224, 224, 3) for arr in int_inputs]
        results = [self.alpr.recognize_ndarray(inp) for inp in reshaped_input]
        serialized = [json.dumps(result) for result in results]
        return serialized
Esempio n. 4
0
    def plate_detection(self, frame):
        alpr = Alpr('eu', '/etc/openalpr/openalpr.conf',
                    '/usr/share/openalpr/runtime_data')

        if not alpr.is_loaded():
            raise AlprException("Error loading openALPR")

        alpr.set_top_n(1)

        results = alpr.recognize_ndarray(frame)

        if results['results'] != []:
            self.plate_as_text(results)
class OpenALPRProcessor:
    def __init__(self, config, share_data):
        self.__alpr = Alpr("eu-by", config, share_data)
        self.__counter = 0

    def process_next_frame(self, frame):
        self.__counter += self.__counter
        frame['detections']['plates'] = []
        for idx, roi in enumerate(frame['detections']['rois']):
            results = self.__alpr.recognize_ndarray(
                frame['image'][roi[0]:roi[0] + roi[2], roi[1]:roi[1] + roi[3]])
            if len(results['results']):
                frame['detections']['plates'].append(results['results'])
        return frame
Esempio n. 6
0
def detect(frame):
    # Configure ALPR setting according to config file
    alpr = Alpr("us", "/usr/share/openalpr/config/openalpr.defaults.conf",
                "/usr/share/openalpr/runtime_data/")

    if not alpr.is_loaded():
        print("Error loading OpenALPR")
        sys.exit(1)

    # Get the top result from ALPR for each plate
    alpr.set_top_n(1)
    alpr.set_default_region("tx")

    # Loads results from the openALPR library
    results = alpr.recognize_ndarray(frame)
    result = results['results']

    alpr.unload()

    return result
Esempio n. 7
0
def read_number_plate(im):
    #initialize the openalpr class instance
    alpr = Alpr("us", "../DATA/runtime_data/config/us.conf",
                "../DATA/runtime_data")
    if not alpr.is_loaded():
        print("Error loading OpenALPR")
        sys.exit(1)

    alpr.set_top_n(20)
    alpr.set_default_region("ca")
    results = alpr.recognize_ndarray(im)
    i = 0
    # extracting the json files
    plate = results['results'][0]
    candidate = plate['candidates'][0]
    plate_coordinates = results['results'][0]['coordinates']

    alpr.unload()
    out = im[plate_coordinates[0]['y']:plate_coordinates[2]['y'] + 20,
             plate_coordinates[0]['x']:plate_coordinates[1]['x'] + 20]
    return candidate['plate'], candidate['confidence'], out
Esempio n. 8
0
def main():
    alpr = Alpr('eu', 'eu.conf', 'openalpr/runtime_data')
    if not alpr.is_loaded():
        print('Error loading OpenALPR')
        sys.exit(1)
    alpr.set_top_n(3)
    #alpr.set_default_region('new')

    cap = open_cam_rtsp(RTSP_SOURCE)
    if not cap.isOpened():
        alpr.unload()
        sys.exit('Failed to open video file!')
    cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_AUTOSIZE)
    cv2.setWindowTitle(WINDOW_NAME, 'OpenALPR video test')

    _frame_number = 0
    while True:
        ret_val, frame = cap.read()
        if not ret_val:
            print('VidepCapture.read() failed. Exiting...')
            break

        _frame_number += 1
        if _frame_number % FRAME_SKIP != 0:
            continue
        cv2.imshow(WINDOW_NAME, frame)

        results = alpr.recognize_ndarray(frame)
        for i, plate in enumerate(results['results']):
            best_candidate = plate['candidates'][0]
            print('Plate #{}: {:7s} ({:.2f}%)'.format(
                i, best_candidate['plate'].upper(),
                best_candidate['confidence']))

        if cv2.waitKey(1) == 27:
            break

    cv2.destroyAllWindows()
    cap.release()
    alpr.unload()
def recognize_license_plate(image, obj_meta, confidence):

    #silly way to detect license plates
    if (True):  #obj_meta.class_id == 1):
        #Importing openalpr
        alpr_engine = Alpr("eu", "/etc/openalpr/openalpr.conf",
                           "/usr/share/openalpr/runtime_data")

        if not alpr_engine.is_loaded():
            print("Error loading OpenALPR")
            sys.exit(1)

        alpr_engine.set_top_n(10)
        alpr_engine.set_default_region("fi")

        #frame_number = random.randint(0, 100)
        rect_params = obj_meta.rect_params
        top = int(rect_params.top)
        left = int(rect_params.left)
        width = int(rect_params.width)
        height = int(rect_params.height)
        lp_cutout = image[top:top + height, left:left + width, :]

        #cv2.imwrite("/opt/nvidia/deepstream/deepstream-5.0/sources/python/apps/deepstream-test12-helloworld8/"+str(frame_number)+"_lpcut.jpg",lp_cutout)
        #cv2.imwrite("/opt/nvidia/deepstream/deepstream-5.0/sources/python/apps/deepstream-test10-helloworld8/"+str(frame_number)+"_image.jpg",image)

        results = alpr_engine.recognize_ndarray(lp_cutout)

        if 'results' in results:
            dets = results['results']
            #if (len(dets) > 0 and len(dets[0]['plate'])==6):
            #    plate_tracker[track.track_id] = dets[0]['plate']

            print(dets)

        alpr_engine.unload()
                    label = "{}: {:.2f}%".format('car', confidence * 100)
                    cv2.rectangle(image, (startX, startY), (endX, endY),
                                  COLORS, 2)
                    y = startY - 15 if startY - 15 > 15 else startY + 15
                    cv2.putText(image, label, (startX, y),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS, 2)

                    if startX < 0: startX = 0
                    if endX < 0: endX = 0
                    if startY < 0: startY = 0
                    if endY < 0: endY = 0

                    img2 = image[startY:endY, startX:endX]
                    #cv2.imshow('test',img2)
                    #cv2.imwrite("img{}.jpg".format(i), img2)
                    results = alpr.recognize_ndarray(img2)
                    if len(results['results']) > 0:
                        print("|||||||||||||||||||||||||||||||")
                        print("plate        --> {}".format(
                            results['results'][0]['candidates'][0]['plate']))
                        print("confidence   --> {}".format(
                            results['results'][0]['candidates'][0]
                            ['confidence']))

                    fps = "{:3.0f} FPS".format(1 / (time.time() - start_time))
                    cv2.putText(image, fps, (w - 85, h - 50),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.6, COLORS2, 2)

        cv2.imshow('frame', image)

    if cv2.waitKey(1) & 0xFF == ord('q'):
Esempio n. 11
0
class detectionBox:
    def __init__(self, camera_name, name, area, video_stream_reference, config,
                 runtime, db_reference):
        self.camera_name = camera_name
        self.name = name
        self.area = area  # bounding box for the search
        self.stream = video_stream_reference  # reference to the video feed
        self.old_detected_rect = []

        # threads cannot share alpr object, needs its own
        self.alpr = Alpr("us", config, runtime)
        self.alpr.set_top_n(1)

        self.fps = FPS().start_timer()

        self.stopped = False

        # stores license plate objects
        self.license_plate_list = []

        self.licensePlateService = licensePlateService(self,
                                                       db_reference).start()

    def start(self):
        Thread(target=self.update, args=()).start()
        return self

    def update(self):  # main method, do processing here
        while True:
            if self.stopped:
                return

            self.fps.update()

            # grab the most recent frame from the video thread
            frame = self.stream.read()
            frame_copy = copy.deepcopy(frame)
            cropped_frame = frame[int(self.area[1]):int(self.area[1] +
                                                        self.area[3]),
                                  int(self.area[0]):int(self.area[0] +
                                                        self.area[2])]

            # run the detector
            results = self.alpr.recognize_ndarray(cropped_frame)

            detected_rect = []
            # finds all rects in frame and stores in detectedRect
            if results['results']:
                for plate in results['results']:

                    # offset so points are relative to the frame, not cropped frame, use to find bounding rect
                    left_bottom = (plate['coordinates'][3]['x'] + self.area[0],
                                   plate['coordinates'][3]['y'] + self.area[1])
                    right_bottom = (plate['coordinates'][2]['x'] +
                                    self.area[0],
                                    plate['coordinates'][2]['y'] +
                                    self.area[1])
                    right_top = (plate['coordinates'][1]['x'] + self.area[0],
                                 plate['coordinates'][1]['y'] + self.area[1])
                    left_top = (plate['coordinates'][0]['x'] + self.area[0],
                                plate['coordinates'][0]['y'] + self.area[1])

                    all_points = np.array(
                        [left_bottom, right_bottom, left_top, right_top])
                    bounding_rect = cv2.boundingRect(all_points)  # X, Y, W, H

                    detected_rect.append(bounding_rect)

                    # convert lpr results into a license plate object and store in license_plate_list
                    plate_number = plate['plate']
                    #plate_image = frame[int(bounding_rect[1]):int(bounding_rect[1] + bounding_rect[3]), int(bounding_rect[0]):int(bounding_rect[0] + bounding_rect[2])]
                    plate_image = cv2.resize(frame_copy, (720, 480),
                                             interpolation=cv2.INTER_AREA)
                    plate_datetime = datetime.datetime.now()
                    plate_time = get_system_uptime()
                    plate_confidence = plate['confidence']
                    new_plate = licensePlate(plate_number, plate_image,
                                             plate_time, self.camera_name,
                                             self.name, plate_datetime,
                                             plate_confidence)
                    self.license_plate_list.append(new_plate)
                    self.licensePlateService.notify(
                    )  # help out the poor thread

                self.old_detected_rect = detected_rect  # this way, detectedRect will be erased and operated on but oldDetectedRect will always have something in it

            else:
                # no results
                self.old_detected_rect = []

    def draw(self, frame):
        # return frame with drawings on it
        # draw plates detected and bounding boxes
        # is this necessary? The bounding boxes of the search areas should not overlap
        # should check for overlapping bounding boxes in constructor

        # draw plates detected
        # print(len(self.oldDetectedRect))
        for rect in self.old_detected_rect:
            cv2.rectangle(frame, rect, (0, 255, 0), 2)

        # draw search box and name
        cv2.putText(frame, self.name, (self.area[0], self.area[1] - 5),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (15, 15, 255), 2)
        cv2.rectangle(frame, self.area, (0, 0, 255), 2)

        # return drawn frame
        return frame

    def stop(self):
        self.fps.stop()
        print(self.name, "FPS: ", self.fps.fps())
        self.licensePlateService.stop()
        self.stopped = True
Esempio n. 12
0
alpr = Alpr("us", "openalpr/config/openalpr.conf.defaults",
            "openalpr/runtime_data")
if not alpr.is_loaded():
    print("Error loading OpenALPR")
    sys.exit(1)

alpr.set_top_n(20)
alpr.set_default_region("fl")
'''
video = cv2.VideoCapture(0)
val, frame = video.read()
results = alpr.recognize_ndarray(frame)
'''
img = cv2.imread("img3.jpg")
frame = img
results = alpr.recognize_ndarray(frame)

print(results)

i = 0
for plate in results['results']:
    i += 1
    print("Plate #%d" % i)
    print("   %12s %12s" % ("Plate", "Confidence"))
    for candidate in plate['candidates']:
        prefix = "-"
        if candidate['matches_template']:
            prefix = "*"

        print("  %s %12s%12f" %
              (prefix, candidate['plate'], candidate['confidence']))
Esempio n. 13
0
                            ylp0 = min(ylp0, frame.shape[1])
                            ylp3 = min(ylp3, frame.shape[1])

                            # cv2.line(imgOriginalScene, tuple(points[0]), tuple(points[1]), (0, 255, 0), 2)
                            # cv2.line(imgOriginalScene, tuple(points[1]), tuple(points[2]), (0, 255, 0), 2)
                            # cv2.line(imgOriginalScene, tuple(points[2]), tuple(points[3]), (0, 255, 0), 2)
                            # cv2.line(imgOriginalScene, tuple(points[3]), tuple(points[0]), (0, 255, 0), 2)

                            # cv2.imshow('LP', imgOriginalScene)

                            lp_image = imgOriginalScene[int(ylp1):int(ylp3), int(xlp1):int(xlp3), :]
                            # cv2.imwrite('cars/car_' + str(indexIDs[i]) + '_lp.png', lp_image)
                            # cv2.imshow('LP_IMG', lp_image)

                            """--------------------------------"""
                            results = alpr.recognize_ndarray(lp_image)
                            i = 0
                            for plate in results['results']:
                                candidates = sorted(plate['candidates'], key=lambda c: c['confidence'], reverse=True)
                                candidate = candidates[0]
                                #     print(candidate['confidence'], candidate['matches_template'], candidate['plate'])
                                license_plate = candidate['plate']
                                # for c in candidates:
                                #     print(c['confidence'], c['matches_template'], c['plate'], sep='; ')
                            """--------------------------------"""
                        """-----------------------"""

                """ID"""
                # text = "{}".format(indexIDs[i])
                # cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
                i += 1
Esempio n. 14
0
        #print "   %12s %12s" % ("Plate", "Confidence")
        for candidate in plate['candidates']:
            prefix = "-"
            if candidate['matches_template']:
                    prefix = "*"
            print "  %s %12s%12f"%(prefix, candidate['plate'], candidate['confidence'])
            break
    resized = cv2.resize(frame,(0,0),fx=0.3,fy=0.3)
    cv2.imshow("frame",resized)
    cv2.waitKey(1)
cap.release()
'''

gray = cv2.imread("/home/frank/Tmp/2.jpg", 0)
s = time.time()
results = alpr.recognize_ndarray(gray)
#print results['results']

i = 0
for plate in results['results']:
    i += 1
    #print "Plate #%d" % i
    #print "   %12s %12s" % ("Plate", "Confidence")
    for candidate in plate['candidates']:
        prefix = "-"
        if candidate['matches_template']:
            prefix = "*"
        print("  %s %12s%12f" %
              (prefix, candidate['plate'], candidate['confidence']))
        break
e = time.time()
Esempio n. 15
0
class image_converter:
    def __init__(self):

        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("camera/image_raw", Image,
                                          self.callback)
        self.alpr = Alpr("eu", "/etc/openalpr/openalpr.conf",
                         "/usr/local/home/u180107/openalpr/runtime_data")
        self.alpr.set_detect_region(True)
        self.currentConfidence = 0
        self.currentDistance = 0

    def distanceByArea(self, cv_image):

        results = self.alpr.recognize_ndarray(cv_image)
        for i, plate in enumerate(results['results']):
            best_candidate = plate['coordinates']
            corner1 = (best_candidate[0]['x'], best_candidate[0]['y'])
            corner2 = (best_candidate[2]['x'], best_candidate[2]['y'])
            platePixelWidth = abs(
                int(best_candidate[2]['x']) - int(best_candidate[0]['x']))
            platePixelHeight = abs(
                int(best_candidate[2]['y']) - int(best_candidate[0]['y']))
            platePixelArea = platePixelHeight * platePixelWidth

            # The distance equation required that the camera be calibrated, currently they're 'close enough'
            distance = round(52000 * pow(platePixelArea, -0.368), 2)
            detected = plate['candidates'][0]
            if self.currentConfidence < detected['confidence'] or abs(
                    distance - self.currentDistance) > 200:
                self.currentConfidence = detected['confidence']
                # print('Plate #{}: {:7s} ({:.2f}%) - Plate distance: {}mm'.format(i, detected['plate'].upper(), detected['confidence'],distance))
            self.currentDistance = distance

            cv2.rectangle(cv_image, corner1, corner2, (0, 255, 0), 2)
            cv2.putText(cv_image, ('distance = %dmm' % distance),
                        (i * 330, 460), font, 1, (0, 255, 0), 2, cv2.LINE_AA)

        return cv_image

    def distanceByHomography(self, cv_image):
        cam_mtx = np.array([[767.92632836, 0, 325.7824046],
                            [0, 770.98907555, 157.44636998], [0, 0, 1]])
        dist_coeffs = np.zeros((5, 1))
        results = self.alpr.recognize_ndarray(cv_image)
        for i, plate in enumerate(results['results']):
            best_candidate = plate['coordinates']
            worldPoints = np.float32([[0, 0, 0], [510, 0, 0], [510, 110, 0],
                                      [0, 110, 0]])
            imagePoints = np.float32([(best_candidate[a]['x'],
                                       best_candidate[a]['y'])
                                      for a in range(0, 4)])
            # print(imgPoints)
            # print(worldPoints)
            imgPoints = np.ascontiguousarray(imagePoints[:, :2].reshape(
                4, 1, 2))
            _ret, rvec, tvec = cv2.solvePnP(worldPoints,
                                            imgPoints,
                                            cam_mtx,
                                            dist_coeffs,
                                            flags=cv2.SOLVEPNP_P3P)
            print("Rotation Vector:\n {0}".format(rvec))
            print("Translation Vector:\n {0}".format(tvec))

            end_point2D, jacobian = cv2.projectPoints(
                np.array([(0.0, 0.0, 1000.0)]), rvec, tvec, cam_mtx,
                dist_coeffs)

            for p in imagePoints:
                cv2.circle(cv_image, (int(p[0]), int(p[1])), 3, (0, 0, 255),
                           -1)

            p1 = (int(imagePoints[0][0]), int(imagePoints[0][1]))
            p2 = (int(end_point2D[0][0][0]), int(end_point2D[0][0][1]))

            cv2.line(cv_image, p1, p2, (255, 0, 0), 2)

        return cv_image

    def callback(self, data):
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
            # cv2.imshow(WINDOW_NAME, cv_image)
        except CvBridgeError as e:
            print(e)

        # cv_image_labeled = self.distanceByArea(cv_image)
        cv_image_labeled = self.distanceByHomography(cv_image)

        cv2.imshow(WINDOW_NAME, cv_image_labeled)
        cv2.waitKey(3)
Esempio n. 16
0
        def main():
            alpr = Alpr('fr', 'fr.conf',
                        '/usr/local/share/openalpr/runtime_data')
            if not alpr.is_loaded():
                print('Error loading OpenALPR')
                sys.exit(1)
            alpr.set_top_n(3)
            #alpr.set_default_region('new')

            cap = open_cam_rtsp(RTSP_SOURCE)
            if not cap.isOpened():
                alpr.unload()
                #sys.exit('Failed to open video file!')
                self.label_display.set_text(
                    "Output Log : Failed to open video file")
            #cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
            #cv2.setWindowTitle(WINDOW_NAME, 'Gate Camera test')
            #cv2.resizeWindow(WINDOW_NAME, 256, 256)

            conn = sqlite3.connect('stu3.db')
            c = conn.cursor()

            #c.execute('create table pr1 (ID, Name, NP)')

            conn.commit()

            _frame_number = 0
            while True:
                ret_val, frame = cap.read()
                if not ret_val:
                    self.label_display.set_text(
                        "Output Log : VidepCapture.read() failed. Exiting...")
                    break

                _frame_number += 1
                if _frame_number % FRAME_SKIP != 0:
                    continue
                #cv2.imshow(WINDOW_NAME, frame)
                #cv2.resizeWindow(WINDOW_NAME, frame, 256, 256)

                results = alpr.recognize_ndarray(frame)
                for i, plate in enumerate(results['results']):
                    best_candidate = plate['candidates'][0]

                    J = ('{}'.format(best_candidate['plate'].upper(),
                                     best_candidate))
                    print str(J)

                    rows = c.execute("select plate from NP")
                    conn.text_factory = lambda x: unicode(x, 'utf-8', 'ignore')
                    for row in rows:
                        if row[0] == str(J):
                            self.status_log.set_text(str(J))
                            print("Matched")
                            arduino = serial.Serial('/dev/ttyACM0', 9600)
                            command = str(85)
                            arduino.write(command)
                            reachedPos = str(arduino.readline())

                            ct = datetime.datetime.now()

                            data_input = '''INSERT INTO logs(plate,entrytime,inside) VALUES(?,?,?);'''
                            boolean = 1
                            data_tuple = (J, ct, boolean)
                            c.execute(data_input, data_tuple)
                            conn.commit()

                            print("------------------------")

                        else:
                            print("------------------------")
                            #print(type(row[0]))

                if cv2.waitKey(1) == 27:
                    break

            cv2.destroyAllWindows()
            cap.release()
            alpr.unload()
Esempio n. 17
0
def main():
    writer = None
    VIDEO_SOURCE = '/home/ankita/Downloads/video1.mp4'
    WINDOW_NAME = 'openalpr'
    FRAME_SKIP = 15
    alpr = Alpr('us', 'us.conf', '/usr/local/share/openalpr/runtime_data')
    if not alpr.is_loaded():
        print('Error loading OpenALPR')
        # sys.exit(1)
    alpr.set_top_n(3)
    # alpr.set_default_region('new')

    cap = cv2.VideoCapture(VIDEO_SOURCE)
    if not cap.isOpened():
        alpr.unload()
        print('Failed to load video file')
        #sys.exit('Failed to open video file!')
    cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_AUTOSIZE)
    cv2.setWindowTitle(WINDOW_NAME, 'OpenALPR video test')

    _frame_number = 0

    currentFrame = 0

    # Get current width of frame
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)  # float
    # Get current height of frame
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float

    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*"XVID")
    out = cv2.VideoWriter('color_output.avi', fourcc, 2.0, (int(width), int(height)))

    while True:
        ret_val, frame = cap.read()
        if not ret_val:
            print('VidepCapture.read() failed. Exiting...')
            break

        _frame_number += 1
        if _frame_number % FRAME_SKIP != 0:
            continue

        results = alpr.recognize_ndarray(frame)

        for i, plate in enumerate(results['results']):
            list1 = []
            best_candidate = plate['candidates'][0]
            min_coord = plate['coordinates'][0]
            max_coord = plate['coordinates'][2]
            x_min = int(min_coord['x'])
            y_min = int(min_coord['y'])
            x_max = int(max_coord['x'])
            y_max = int(max_coord['y'])
            cv2.resize(frame, (500, 500), fx=0, fy=0,
                       interpolation=cv2.INTER_CUBIC)
            cv2.rectangle(frame, (x_min, y_min),
                          (x_max, y_max), (255, 255, 0), 5)
            #cropimg = cv2.getRectSubPix(
             #  frame, (x_min-10, y_min), (x_min, y_min))
            #type(cropimg[0:])
            #x = cropimg[0:][0][0]
            #list1 = x.tolist()
            #list1 = list(map(int,list1))
            #print(list1)
            #print(type(list1))
            #act,pred = get_colour_name(tuple(list1))
            #print(pred)
            
            cv2.putText(frame, 'NP: '+str(best_candidate['plate']).upper(), (x_min, y_min), cv2.FONT_HERSHEY_SIMPLEX, 0.85, [255, 255, 0], 2)
            if(best_candidate['confidence'] > 50):
                print('Plate #{}: {:7s} ({:.2f}%)'.format(i, best_candidate['plate'].upper(), best_candidate['confidence']))
            list1 = []
            cv2.imshow(WINDOW_NAME, frame)

            
            out.write(frame)

        if cv2.waitKey(1) == 27:
            break

    cv2.destroyAllWindows()
    out.release()
    cap.release()
    alpr.unload()
Esempio n. 18
0
def main():

    lastSaveTime = datetime.now(
    )  # initialize last-image-save-time to prog. start time

    alpr = Alpr("us", CFILE, "/usr/share/openalpr/runtime_data/")
    if not alpr.is_loaded():
        print('Error loading OpenALPR')
        sys.exit(1)
    alpr.set_top_n(1)

    cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_AUTOSIZE)
    cv2.setWindowTitle(WINDOW_NAME, 'OpenALPR video test')
    #  =========== now have a loaded instance of alpr; do stuff here

    cam = cv2.VideoCapture(vidname)  # open a video file or stream
    img_w = int(cam.get(3))  # input image width
    img_h = int(cam.get(4))  # input image height
    print("Image w,h = %d %d" % (img_w, img_h))

    prlist = re.compile(r"\L<plates>",
                        plates=[
                            '7WXY20Z', '7WXYZ02', 'WXY202', '7WXY202',
                            'WXY20Z', 'WXYZ02', 'WXYZ0Z', '7WXY2Q2'
                        ])
    save = True  # always save the first image, for reference
    lastPstring = ""  # haven't seen any plates yet
    lastGarageDoor = True  # assume we're starting with door closed
    lastBright = 0.0  # average brightness of previous frame
    lastXcent = 0
    lastYcent = 0
    mvec = 0

    # ================== MAIN LOOP =====================================
    while (True):
        ret, img = cam.read()
        if not ret:
            print('VidepCapture.read() failed. Exiting...')
            sys.exit(1)

        ttnow = datetime.now()  # local real time when this frame was received
        avgbright = img.mean()  # average of all pixels (float)
        # print(avgbright)

        cv2.imshow(WINDOW_NAME, img)  # show camera image

        results = alpr.recognize_ndarray(img)
        # print(".",end='')

        # print(results)
        jsonRes = (json.dumps(results, indent=2))
        jsonS = json.loads(jsonRes)

        rlist = results['results']
        pcount = len(rlist)  # how many plates we have found
        # print("Length = %d" % len(rlist) )
        if (pcount < 1):
            # print("No plates found, bright: %5.2f" % avgbright )
            pstring = ""  # null plate (nothing found)
        else:
            for i, plate in enumerate(rlist):
                cor1x = int(jsonS["results"][0]["coordinates"][0]["x"])
                cor1y = int(jsonS["results"][0]["coordinates"][0]["y"])
                cor2x = int(jsonS["results"][0]["coordinates"][2]["x"])
                cor2y = int(jsonS["results"][0]["coordinates"][2]["y"])
                xcent = (cor1x + cor2x) / 2
                ycent = (cor1y + cor2y) / 2
                dx = xcent - lastXcent
                dy = ycent - lastYcent
                mvec = math.sqrt(dx * dx + dy * dy)  # motion vector in pixels
                pcrop = img[cor1y:cor2y,
                            cor1x:cor2x]  # crop of image containing plate
                cv2.imshow("plate", pcrop)  # show just the plate

                p = plate['candidates'][0]
                pstring = p['plate'].upper()  # characters on license plate
                # if (pstring != "7WXY202") and (pstring != "WXY202"):

        if prlist.search(pstring):  # is this the usual garage-door view?
            garageDoor = True
        else:
            garageDoor = False

        # here: pstring holds the LP characters, or ""

        bRatio = (avgbright + 0.01) / (lastBright + 0.01)
        if (bRatio < 1.0):
            bRatio = 1 / bRatio

        if (bRatio > bRatioThresh):
            bChange = True
        else:
            bChange = False

        if (mvec > mThresh):
            motion = True
        else:
            motion = False

        tnows = ttnow.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]  # time to msec
        if bChange:
            print("Brightness change: %5.3f , %s" % (avgbright, tnows))

        # save = not garageDoor   # save image if normal garage-door view not seen
        save = motion  # save image if motion of plate detected

        if (pstring == ""):
            save = False  # but don't save image if no plate
            if (pstring != lastPstring):
                print("# Nothing: brightness %5.3f , %s" % (avgbright, tnows))

        if save:
            print("%d,%d , %d,%d , " %
                  (cor1x, img_h - cor1y, cor2x, img_h - cor2y),
                  end='')
            print('%s , %5.2f , %5.2f , %5.2f , %s' %
                  (pstring, p['confidence'], avgbright, mvec, tnows))
            tnowf = ttnow.strftime(
                "%Y-%m-%d_%H%M%S_%f")[:-3]  # filename time to msec
            fname3 = fPrefix + tnowf + "_" + str(i) + ".jpg"  # full-size image
            # cv.imwrite(fname3, img) # save current image
            cv2.imwrite(fname3, pcrop)  # save current image
            # print("saved: " + fname3)
            tSince = (ttnow - lastSaveTime).total_seconds()
            # print("seconds since last save = %5.3f" % tSince)
            if (tSince > fullInterval):
                fname4 = fPrefix2 + tnowf + "_" + str(
                    i) + ".jpg"  # full-size image
                cv2.imwrite(fname4, img)  # save full image
                lastSaveTime = ttnow

            save = False
            lastXcent = xcent
            lastYcent = ycent

        lastPstring = pstring  # remember what we saw before
        lastGarageDoor = garageDoor
        lastBright = avgbright  # remember last avg brightness

        c = cv2.waitKey(1) & 0xFF  # get input char if any
        if c == ord('q'):
            sys.exit(1)

# close up and quit
    alpr.unload()
Esempio n. 19
0
class Lector():
    OPEN_ALPR_CONF_PATH = '/opt/openalpr/config/openalpr.conf.user'
    RUNTIME_DATA_PATH = '/opt/openalpr/runtime_data'

    estado = None
    alpr = None

    estado_config = None

    def __init__(self):
        self.estado = 'off'
        self.alpr = Alpr("us", self.OPEN_ALPR_CONF_PATH,
                         self.RUNTIME_DATA_PATH)
        self.alpr.set_top_n(1)

    def iniciar(self, cam_socket):

        if self.estado == 'off':
            self.estado = 'on'
            placas_candidatas = []
            tiempo_post_lectura = 1
            tiempo_ultima_lectura = 0
            # camara              = cv2.VideoCapture(0)
            camara = cv2.VideoCapture(
                '/home/stalinscj/_dux_test/test/vehiculoTest/portonOriginalSinBorde_640x360.mp4'
            )

            leyendo = False
            success = True
            # video = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*'MJPG'), 20, (640,360))
            while self.estado == 'on' and success == True:
                success, frame = camara.read()

                if not success:
                    break

                if time.time() - tiempo_ultima_lectura > tiempo_post_lectura:
                    if leyendo == True:
                        video.release()
                        threading.Thread(target=self.guardar_video,
                                         args=(video_nombre, )).start()
                    leyendo = False

                else:
                    if leyendo == False:
                        video_nombre = "media/{0}.mp4".format(uuid.uuid4().int
                                                              & (1 << 64) - 1)
                        #MP4V
                        # 0x7634706d
                        # video = cv2.VideoWriter(video_nombre, cv2.VideoWriter_fourcc(*'MP4V'), 20, (640,360))
                        video = cv2.VideoWriter(video_nombre, 0x7634706d, 20,
                                                (640, 360))

                    leyendo = True
                    video.write(frame)

                if not leyendo and placas_candidatas:
                    placa = self.get_placa(placas_candidatas)
                    threading.Thread(target=self.procesar_envio,
                                     args=(
                                         placa,
                                         cam_socket,
                                         video_nombre,
                                     )).start()
                    del placas_candidatas[:]

                placa_candidata = self.buscar_placa(frame)

                if placa_candidata:
                    placas_candidatas.append(placa_candidata)
                    tiempo_ultima_lectura = time.time()

                cam_socket.send(
                    text_data=json.dumps({
                        'tipo': 'frame',
                        'img_src': self.img_to_base64(frame)
                    }))

    def detener(self, cam_socket):
        if self.estado == 'on':
            self.estado = 'off'
            frame = cv2.imread(
                os.path.join(settings.STATIC_ROOT, 'core/img/live.png'))
            time.sleep(2)
            cam_socket.send(
                text_data=json.dumps({
                    'tipo': 'frame',
                    'img_src': self.img_to_base64(frame)
                }))

    def img_to_base64(self, img):
        success, img_codificada = cv2.imencode('.png', img)
        if success:
            b64_src = 'data:image/png;base64,'
            img_src = b64_src + base64.b64encode(img_codificada).decode()
            return img_src

        return ''

    def buscar_placa(self, frame):
        buscador = self.alpr.recognize_ndarray(frame)
        resultado = buscador['results']

        if (resultado):
            placa = resultado[0]['plate']
            if self.validar_placa(placa):
                confianza = resultado[0]['confidence']
                coordenadas = resultado[0]['coordinates']
                x0 = coordenadas[0]['x']
                y0 = coordenadas[0]['y']
                x2 = coordenadas[2]['x']
                y2 = coordenadas[2]['y']
                img_placa = frame[y0:y2, x0:x2]
                placa_candidata = (placa, confianza, img_placa, frame)
                cv2.rectangle(frame, (x0 - 5, y0 - 5), (x2 + 5, y2 + 5),
                              (0, 255, 0), 3)

                return placa_candidata

        return None

    def validar_placa(self, placa):
        if len(placa) > 5 and len(placa) < 8:
            return True
        else:
            return False

    def filtrar_placas(self, placas_candidatas):
        flag = False
        for placa in placas_candidatas:
            if len(placa[0]) > 6:
                flag = True
                break

        if flag:
            for placa in placas_candidatas:
                if len(placa[0]) < 7:
                    placas_candidatas.remove(placa)

    def get_placa(self, placas_candidatas):

        self.filtrar_placas(placas_candidatas)

        max_confianza = 0

        for i in range(0, len(placas_candidatas)):
            confianza = placas_candidatas[i][1]

            if (confianza > max_confianza):
                max_confianza = confianza
                placa = placas_candidatas[i]

        return placa

    def procesar_envio(self, placa, cam_socket, video_nombre):
        placa_str = placa[0]
        placa_img = placa[3]
        placa_img_mini = placa[2]

        config = ConfigParser()

        config.read('dux/config.ini')

        peaje = Peaje.objects.filter(pk=config.get('peaje', 'id')).first()

        lectura = Lectura.objects.create(
            peaje=peaje,
            matricula=placa_str,
            direccion=config.get('camaras', '0'),
            imagen=self.img_to_base64(placa_img),
            imagen_mini=self.img_to_base64(placa_img_mini),
            video=video_nombre)

        solicitud = MatriculaSolicitada.objects.filter(matricula=placa_str,
                                                       activo=True).first()

        avisos = 0
        idAlerta = ''
        if solicitud:
            alerta = Alerta.nueva(solicitud, lectura)
            idAlerta = alerta.pk
            avisos = alerta.notificacion_set.all().count()

        cam_socket.send(
            text_data=json.dumps({
                'tipo': 'tupla',
                'fecha': time.strftime("%d/%m/%Y %H:%M:%S"),
                'placa': placa_str,
                'img_src': self.img_to_base64(placa_img_mini),
                'avisos': avisos,
                'alerta_id': idAlerta
            }))

    def iniciar_config(self, cam_socket):
        self.estado_config = 'on'
        camara = cv2.VideoCapture(0)
        success = True
        while self.estado_config == 'on' and success == True:
            success, frame = camara.read()

            if not success:
                break
            cam_socket.send(
                text_data=json.dumps({'img_src': self.img_to_base64(frame)}))

    def terminar_config(self, cam_socket):
        if self.estado_config == 'on':
            self.estado_config = 'off'
            frame = cv2.imread(
                os.path.join(settings.STATIC_ROOT, 'core/img/live.png'))
            time.sleep(2)
            cam_socket.send(
                text_data=json.dumps({'img_src': self.img_to_base64(frame)}))

    def guardar_video(self, video_nombre):
        os.system(
            "ffmpeg -i {0}.mp4 -loglevel quiet -an -vcodec libx264 -crf 23 {0}tmp.mp4"
            .format(video_nombre[:-4]))
        os.system("rm -f " + video_nombre)
        os.system("mv {0}tmp.mp4 {0}.mp4".format(video_nombre[:-4]))
    def checkRedLightCrossed(self, img):
        global count
        for v in vehicles:
            if v.crossed == False and len(v.points) >= 2:
                x1, y1 = v.points[0]
                x2, y2 = v.points[-1]
                if y1 > yl3 and y2 < yl3:
                    count += 1
                    v.crossed = True
                    bimg = img[int(v.rect[1]):int(v.rect[1] + v.rect[3]),
                               int(v.rect[0]):int(v.rect[0] + v.rect[2])]
                    frame2 = bimg
                    self.bimg = bimg
                    img2 = Image.fromarray(frame2)
                    w, h = img2.size
                    asprto = w / h
                    frame2 = cv2.resize(frame2, (250, int(250 / asprto)))
                    cv2image2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2RGBA)
                    alpr = Alpr(
                        "us", r"C:\OpenALPR\Agent\etc\openalpr\openalpr.conf",
                        r"C:\OpenALPR\Agent\usr\share\openalpr\runtime_data")
                    if not alpr.is_loaded():
                        print('Error loading OpenALPR')
                        sys.exit(1)
                    alpr.set_top_n(3)
                    results = alpr.recognize_ndarray(frame2)

                    ###########################################################################

                    resize = cv2.resize(cv2image2, (301, 351),
                                        interpolation=cv2.INTER_LINEAR)
                    image = qimage2ndarray.array2qimage(resize)
                    self.label_detect.setPixmap(QPixmap.fromImage(image))
                    ###########################################################################
                    for i, plate in enumerate(results['results']):
                        X1 = plate['coordinates'][0]['x']
                        Y1 = plate['coordinates'][0]['y']
                        X2 = plate['coordinates'][2]['x']
                        Y2 = plate['coordinates'][2]['y']
                        rimg = cv2image2[Y1:Y2, X1:X2]
                        frame3 = rimg
                        resize = cv2.resize(frame3, (301, 41),
                                            interpolation=cv2.INTER_LINEAR)
                        self.image = qimage2ndarray.array2qimage(resize)
                        self.label_numberplate.setPixmap(
                            QPixmap.fromImage(self.image))

                        self.best_candidate = plate['candidates'][0]
                        # print('Plate #{}: {:7s} ({:.2f}%)'.format(i, best_candidate['plate'].upper(),
                        #                                           best_candidate['confidence']))
                        self.label_number_plate.setText(
                            'number plate:   ' +
                            str(self.best_candidate['plate'].upper()))
                        detect = self.lineEdit_detect.text()

                        if detect == str(self.best_candidate['plate'].upper()):
                            self.label_DETECD_SERACH.show()
                            print("detect")  #ayd2030
                        else:
                            self.label_DETECD_SERACH.hide()

                    ###########################################################################
                    self.name_eligal = r'detection\d- ' + self.best_candidate[
                        'plate'].upper() + '.jpg'
                    self.num_plate = r'numberplate\NP- ' + self.best_candidate[
                        'plate'].upper() + '.jpg'
                    cv2.imwrite(self.name_eligal, bimg)
                    cv2.imwrite(self.num_plate, resize)
                    MainApp.dis(self)
class VehicleID:
    region, configuration, runtime = 'us', 'C:\\OpenALPR\\Agent\\etc\\openalpr\\openalpr.conf', 'C:\\OpenALPR\\Agent\\usr\\share\\openalpr\\runtime_data'
    frame, small_frame, detect, authorized_users, plate, queue = None, None, True, dict(
    ), None, queue.Queue(1000)

    def __init__(self):
        super().__init__()
        self.alpr = Alpr(self.region, self.configuration,
                         self.runtime)  # init alpr engine
        # self.camera = cv2.VideoCapture("D:\\others\\20191125_141145.mp4")  # record frames from video source
        self.camera = cv2.VideoCapture(1)  # record frames from video source

    def main(self):
        while True:
            self.get_frame()
            self.license_plate_recognition()

    def get_frame(self):
        _, frame = self.camera.read()  # read from video source
        self.frame = cv2.UMat(frame).get()  # use tapi to take advantage of gpu
        self.small_frame = cv2.UMat(
            cv2.resize(frame, (0, 0), fx=0.25,
                       fy=0.25)).get()  # 1/4 scale for faster processing

    def license_plate_recognition(self):
        if self.small_frame is not None:
            result = self.alpr.recognize_ndarray(self.small_frame).get(
                'results')  # use alpr engine to detect license plate
            if self.detect and result:
                self.plate, confidence, _, _, _, _, _, _, coordinates, vehicle_region, _ = result[
                    0].values(
                    )  # deserialize results of license plate recognition
                tl, tr, br, bl = coordinates
                cv2.UMat(
                    cv2.rectangle(
                        self.frame, (tl['x'] * 4, tl['y'] * 4),
                        (br['x'] * 4, br['y'] * 4), (0, 255, 255),
                        8))  # plot coordinates of license plate onto frame
                cv2.UMat(
                    cv2.rectangle(self.frame, (bl['x'] * 4, bl['y'] * 4),
                                  (br['x'] * 4, br['y'] * 4 + 25),
                                  (0, 255, 255), cv2.FILLED))
                cv2.UMat(
                    cv2.putText(self.frame, self.plate,
                                (bl['x'] * 4 + 40, bl['y'] * 4 + 15),
                                cv2.QT_FONT_NORMAL, 0.7, (0, 0, 0), 2))
                self.authorize_vehicle(
                )  # search database for vehicle plate and obtain registered owner if present

    def authorize_vehicle(self):
        user = User.query.filter_by(
            license_plate=self.plate).first()  # query user that owns vehicle
        if user:
            self.authorized_users[user.id] = [
                user.id, user.name, user.license_plate
            ]  # load user ont authorized list on gate entry
            socketio.emit('message', self.authorized_users)

    def print_frame(self):
        while True:
            if self.frame is not None:
                yield b'--frame\r\n' b'Content-Type: text/plain\r\n\r\n' + cv2.imencode(
                    '.jpg', self.frame)[1].tostring() + b'\r\n'
Esempio n. 22
0
alpr2.set_top_n(200)
video = cv2.VideoCapture(0)
ultimaPlaca = ""
placa = ""

placeHolder = ""
placaTeste1 = ""
placaTeste2 = ""
placaTeste3 = ""

while (True):

    _, frame = video.read()
    cv2.imshow('CAM', frame)

    results = alpr.recognize_ndarray(frame)
    for plate in results['results']:
        for candidate in plate['candidates']:
            if (re.search('^[A-Z]{3}[0-9]{4}', candidate['plate'])):
                placeHolder = candidate['plate']
                break
    resultados = alpr2.recognize_ndarray(frame)
    for plate in resultados['results']:
        for candidate in plate['candidates']:
            if re.search('^[A-Z]{3}[0-9]{4}',
                         re.sub('[\W_]+', '', candidate['plate'])):
                placeHolder = re.sub('[\W_]+', '', candidate['plate'])
                break

    if (placeHolder != ""):
        if (placaTeste1 == ""):
Esempio n. 23
0
# Image Input
plate_image = cv.imread('res/b3052smd.jpg')

# Resize the Image
scale_percent = 30
width = int(plate_image.shape[1] * scale_percent / 100)
height = int(plate_image.shape[0] * scale_percent / 100)

# dsize
dsize = (width, height)

# Transform
plate_image = cv.resize(plate_image, dsize)

# Plate Recognition
LPR_Result = alpr.recognize_ndarray(plate_image)

# Print all result
pprint.pprint(LPR_Result)

# Check the Result is Empty or Not
if (LPR_Result['results'] != []):

    Plate_Result = LPR_Result['results'][0]

    return_plate = Plate_Result['plate']
    return_confidence = Plate_Result['confidence']
    return_coordinate = Plate_Result['coordinates']

    # Convert coordinates into bounding box
    top_left = return_coordinate[0]
Esempio n. 24
0
def main():
    alpr = Alpr('country', 'country.conf',
                '/usr/local/share/openalpr/runtime_data')
    if not alpr.is_loaded():
        print('Error loading OpenALPR')
        sys.exit(1)
    alpr.set_top_n(3)
    #alpr.set_default_region('new')

    cap = open_cam_rtsp(RTSP_SOURCE)
    if not cap.isOpened():
        alpr.unload()
        sys.exit('Failed to open video file!')
    cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
    cv2.setWindowTitle(WINDOW_NAME, 'Gate Camera test')

    _frame_number = 0
    while True:
        ret_val, frame = cap.read()
        if not ret_val:
            print('VidepCapture.read() failed. Exiting...')
            break

        _frame_number += 1
        if _frame_number % FRAME_SKIP != 0:
            continue
        cv2.imshow(WINDOW_NAME, frame)

        results = alpr.recognize_ndarray(frame)
        for i, plate in enumerate(results['results']):
            best_candidate = plate['candidates'][0]
            with open('output.txt', 'w') as x:

                J = ('{:7s}'.format(best_candidate['plate'].upper(),
                                    best_candidate))
                print >> x, str(J)

            with open("allowed.txt") as f, open("output.txt") as d:
                for line in d:
                    list_of_lists = []
                    inner_list = [elt.strip() for elt in line.split(',')]
                    list_of_lists.append(inner_list)

                for line in f:
                    allowed = []
                    inner_list2 = [elt.strip() for elt in line.split(',')]
                    allowed.append(inner_list2)

                if list_of_lists == allowed:
                    print(True)
                    arduino = serial.Serial('/dev/ttyACM0', 9600)
                    command = str(85)
                    arduino.write(command)
                    reachedPos = str(arduino.readline())

                else:
                    print(False)

        if cv2.waitKey(1) == 27:
            break

    cv2.destroyAllWindows()
    cap.release()
    alpr.unload()
Esempio n. 25
0
class image_converter:
    def __init__(self):
        self.publisher = rospy.Publisher('object_pose', Point, queue_size=10)
        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("camera/image_raw", Image,
                                          self.callback)
        self.alpr = Alpr("eu", "/etc/openalpr/openalpr.conf",
                         "/usr/local/home/u180107/openalpr/runtime_data")
        self.alpr.set_detect_region(True)
        self.currentConfidence = 0
        self.currentDistance = 0
        self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
                         30, 0.001)
        self.objp = np.float32([[0, 0, 0], [7, 0, 0], [0, 5, 0], [7, 5, 0]])
        self.axis = np.float32([[3, 0, 0], [0, 3, 0], [0, 0,
                                                       -3]]).reshape(-1, 3)
        with np.load('calibration.npz') as X:
            self.mtx, self.dist = [X[i] for i in ('mtx', 'dist')]

    def draw(self, img, corners, imgpts):
        corner = tuple(corners[0].ravel())
        img = cv2.line(img, corner, tuple(imgpts[0].ravel()), (255, 0, 0), 5)
        img = cv2.line(img, corner, tuple(imgpts[1].ravel()), (0, 255, 0), 5)
        img = cv2.line(img, corner, tuple(imgpts[2].ravel()), (0, 0, 255), 5)
        return img

    def getPose(self, cv_image):

        gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
        ret, corners = cv2.findChessboardCorners(gray, (8, 6), None,
                                                 cv2.CALIB_CB_FAST_CHECK)

        if ret == True:
            result = np.array(
                [corners[0], corners[7], corners[40], corners[-1]])
            for res in result:
                cv2.circle(cv_image, (res[0][0], res[0][1]), 5, (0, 0, 255), 3)

            # print(result)
            corners2 = cv2.cornerSubPix(gray, result, (11, 11), (-1, -1),
                                        self.criteria)

            ret, rvecs, tvecs, inliers = cv2.solvePnPRansac(
                self.objp, corners2, self.mtx, self.dist, cv2.SOLVEPNP_P3P)

            imgpts, jac = cv2.projectPoints(self.axis, rvecs, tvecs, self.mtx,
                                            self.dist)
            img = self.draw(cv_image, corners2, imgpts)
            platePoint = Point(float(tvecs[0]), float(tvecs[1]),
                               float(tvecs[2]))
            self.publisher.publish(platePoint)
            # cv2.imshow('img', img)
            return (img)

            successful_frames += 1
            cv2.waitKey(1)
        else:
            # cv2.imshow('img', cv_image)
            return (cv_image)
            cv2.waitKey(1)

    def distanceByArea(self, cv_image):
        results = self.alpr.recognize_ndarray(cv_image)
        for i, plate in enumerate(results['results']):
            best_candidate = plate['coordinates']
            corner1 = (best_candidate[0]['x'], best_candidate[0]['y'])
            corner2 = (best_candidate[2]['x'], best_candidate[2]['y'])
            platePixelWidth = abs(
                int(best_candidate[2]['x']) - int(best_candidate[0]['x']))
            platePixelHeight = abs(
                int(best_candidate[2]['y']) - int(best_candidate[0]['y']))
            platePixelArea = platePixelHeight * platePixelWidth

            # The distance equation required that the camera be calibrated, currently they're 'close enough'
            distance = round(52000 * pow(platePixelArea, -0.368), 2)
            detected = plate['candidates'][0]
            if self.currentConfidence < detected['confidence'] or abs(
                    distance - self.currentDistance) > 200:
                self.currentConfidence = detected['confidence']
                # print('Plate #{}: {:7s} ({:.2f}%) - Plate distance: {}mm'.format(i, detected['plate'].upper(), detected['confidence'],distance))
            self.currentDistance = distance

            cv2.rectangle(cv_image, corner1, corner2, (0, 255, 0), 2)
            cv2.putText(cv_image, ('distance = %dmm' % distance),
                        (i * 330, 460), font, 1, (0, 255, 0), 2, cv2.LINE_AA)

        return cv_image

    def distanceByHomography(self, cv_image):
        cam_mtx = np.array([[767.92632836, 0, 325.7824046],
                            [0, 770.98907555, 157.44636998], [0, 0, 1]])
        dist_coeffs = np.zeros((5, 1))
        results = self.alpr.recognize_ndarray(cv_image)
        for i, plate in enumerate(results['results']):
            best_candidate = plate['coordinates']
            worldPoints = np.float32([[0, 0, 0], [510, 0, 0], [510, 110, 0],
                                      [0, 110, 0]])
            imagePoints = np.float32([(best_candidate[a]['x'],
                                       best_candidate[a]['y'])
                                      for a in range(0, 4)])
            # print(imgPoints)
            # print(worldPoints)
            imgPoints = np.ascontiguousarray(imagePoints[:, :2].reshape(
                4, 1, 2))
            _ret, rvec, tvec = cv2.solvePnP(worldPoints,
                                            imgPoints,
                                            cam_mtx,
                                            dist_coeffs,
                                            flags=cv2.SOLVEPNP_P3P)
            print("Rotation Vector:\n {0}".format(rvec))
            print("Translation Vector:\n {0}".format(tvec))

            end_point2D, jacobian = cv2.projectPoints(
                np.array([(0.0, 0.0, 1000.0)]), rvec, tvec, cam_mtx,
                dist_coeffs)

            for p in imagePoints:
                cv2.circle(cv_image, (int(p[0]), int(p[1])), 3, (0, 0, 255),
                           -1)

            p1 = (int(imagePoints[0][0]), int(imagePoints[0][1]))
            p2 = (int(end_point2D[0][0][0]), int(end_point2D[0][0][1]))

            cv2.line(cv_image, p1, p2, (255, 0, 0), 2)

        return cv_image

    def callback(self, data):
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
        except CvBridgeError as e:
            print(e)

        cv_image_labeled = self.getPose(cv_image)
        cv2.imshow(WINDOW_NAME, cv_image_labeled)
        cv2.waitKey(1)