def predict(self, image):
        # Input image or frame
        self.image = image

        for coordinate in self.extractLP():
            self.candidates = []
            # convert (x_min, y_min, width, height) to coordinate(top left, top right, bottom left, bottom right)
            pts = order_points(coordinate)

            # crop number plate
            LpRegion = perspective.four_point_transform(self.image, pts)

            # segmentation
            self.segmentation(LpRegion)

            # recognize characters
            self.recognizeChar()

            # format and display license plate
            license_plate = self.format()

            # draw labels
            self.image = draw_labels_and_boxes(self.image, license_plate, coordinate)

        cv2.imwrite('example.png', self.image)
        return self.image
Beispiel #2
0
    def predict(self, image):
        # Input image or frame
        self.image = image

        for coordinate in self.extractLP():  # detect license plate by yolov3
            self.candidates = []

            # convert (x_min, y_min, width, height) to coordinate(top left, top right, bottom left, bottom right)
            pts = order_points(coordinate)

            # crop number plate used by bird's eyes view transformation
            LpRegion = perspective.four_point_transform(self.image, pts)
            # cv2.imwrite('step1.png', LpRegion)
            # segmentation
            self.segmentation(LpRegion)

            # recognize characters
            self.recognizeChar()

            # format and display license plate
            license_plate = self.format()
            if len(license_plate) == 0:
                license_plate = "khong nhan dien duoc . sorry !"
            # draw labels
            self.image = draw_labels_and_boxes(self.image, license_plate,
                                               coordinate)
        return (self.image, license_plate)
Beispiel #3
0
    def predict(self, image):
        """
        Hàm dự đoán giá trị của biển số xe
        :param image:
        :return:
        Ảnh được chú thích vùng biển số và giá trị
        Chuỗi ký tự biển số xe
        """
        # Ảnh đầu vào
        self.image = image

        # Xét các vùng biển số detect được bằng YOLOv3 Tiny
        for coordinate in self.extractLP():
            # Khởi tạo candidates để lưu giá trị biển số và tọa độ cần chú thích trong ảnh
            self.candidates = []

            # Chuyển đổi (x_min, y_min, width, height) thành dạng (top left, top right, bottom left, bottom right)
            pts = order_points(coordinate)

            # Cắt ảnh biển số xe dùng bird's eyes view transformation
            LpRegion = perspective.four_point_transform(self.image, pts)

            # Xử lý trường hợp biển số 1 dòng và 2 dòng
            # Chọn ngưỡng tỷ lệ chiều ngang / chiều dọc là 1.5
            # Nếu tỷ lệ này > 1.5 => Biển số 1 dòng
            # Ngược lại => Biển số 2 dòng
            if (LpRegion.shape[1] / LpRegion.shape[0] > 1.5):
                # Tỷ lệ scale
                scale_ratio = 40 / LpRegion.shape[0]
                (w, h) = (int(LpRegion.shape[1] * scale_ratio),
                          int(LpRegion.shape[0] * scale_ratio))
            else:
                # Tỷ lệ scale
                scale_ratio = 100 / LpRegion.shape[0]
                (w, h) = (int(LpRegion.shape[1] * scale_ratio),
                          int(LpRegion.shape[0] * scale_ratio))

            # Resize ảnh vùng biển số về kích thước chuẩn
            # Đối với biển số 2 dòng: chiều cao = 40px
            # Đối với biển số 1 dòng: chiều cao = 100px
            LpRegion = cv2.resize(LpRegion, (w, h))

            # Phân đoạn từng ký tự
            self.segmentation(LpRegion)

            # Nhận diện các ký tự
            self.recognizeChar()

            # Định dạng các ký tự biển số
            self.lpNumber = self.format()

            # Vẽ bounding box và giá trị biển số vào ảnh
            self.image = draw_labels_and_boxes(self.image, self.lpNumber,
                                               coordinate)

        # Trả về ảnh dự đoán và giá trị biển số xe
        return self.image, self.lpNumber
Beispiel #4
0
def save_a_img(link_img, output):
    model = detectNumberPlate()
    file_name = os.path.basename(link_img)
    img = cv2.imread(link_img)
    coordinates = model.detect(img)
    for coordinate in coordinates:
        pts = order_points(coordinate)
        ls_region = perspective.four_point_transform(img, pts)
        cv2.imwrite(os.path.join(output, file_name), ls_region)
        return ls_region
Beispiel #5
0
def save_img(data_dir, output_dir):
    model = detectNumberPlate() #initizial class to detect image
    list_file = glob.glob(data_dir + "/*.jpg") #take list file
    for file in list_file: #take each file in list file
        file_name = os.path.basename(file) #name file match with list file original
        img = cv2.imread(file) #read file

        coordinates = model.detect(img) #detect file
        for coordinate in coordinates:  # detect license plate by yolov3
            # candidates = []
            # convert (x_min, y_min, width, height) to coordinate(top left, top right, bottom left, bottom right)
            pts = order_points(coordinate)
            # crop number plate used by bird's eyes view transformation
            lp_region = perspective.four_point_transform(img, pts)
            cv2.imwrite(os.path.join(output_dir, file_name), lp_region)
    def predict(self, image, name):
        # Input image or frame
        self.image = image

        all_license_plates = []

        for coordinate in self.extractLP():  # detect license plate by yolov3
            self.candidates = []

            # convert (x_min, y_min, width, height) to coordinate(top left, top right, bottom left, bottom right)
            pts = order_points(coordinate)

            # crop number plate used by bird's eyes view transformation
            LpRegion = perspective.four_point_transform(self.image, pts)
            # height, width, _ = LpRegion.shape
            # LpRegion = LpRegion[:, int(width * 0.1):int(width * 0.9)]
            img_path = os.path.join('output/lp/{}.jpg'.format(name))
            cv2.imwrite(img_path, LpRegion)

            license_plate = self.ocr.predict(LpRegion)

            # # segmentation
            # self.segmentation(LpRegion, name)
            #
            # # recognize characters
            # self.recognizeChar()
            #
            # # format and display license plate
            # license_plate = self.format()

            if license_plate is not None and len(license_plate) > 4:
                # draw labels
                self.image = draw_labels_and_boxes(self.image, license_plate,
                                                   coordinate)
                all_license_plates.append(license_plate)

        # cv2.imwrite('example.png', self.image)
        return self.image, all_license_plates