import cv2
from lib_detection import load_model, detect_lp, im2single

# Đường dẫn ảnh, các bạn đổi tên file tại đây để thử nhé
img_path = "test/xemay.jpg"

# Load model LP detection
wpod_net_path = "wpod-net_update1.json"
wpod_net = load_model(wpod_net_path)

# Đọc file ảnh đầu vào
Ivehicle = cv2.imread(img_path)
cv2.imshow("Anh goc", Ivehicle)
cv2.waitKey(0)

# Kích thước lớn nhất và nhỏ nhất của 1 chiều ảnh
Dmax = 608
Dmin = 288

# Lấy tỷ lệ giữa W và H của ảnh và tìm ra chiều nhỏ nhất
ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2])
side = int(ratio * Dmin)
bound_dim = min(side, Dmax)

_, LpImg, lp_type = detect_lp(wpod_net,
                              im2single(Ivehicle),
                              bound_dim,
                              lp_threshold=0.5)

if (len(LpImg)):
def get_plate():
    try:
        cam = cv2.VideoCapture(0)
        ret, frame = cam.read()

        wpod_net_path = "wpod-net_update1.json"
        wpod_net = load_model(wpod_net_path)

        # Đọc file ảnh đầu vào
        Ivehicle = frame

        # Kích thước lớn nhất và nhỏ nhất của 1 chiều ảnh
        Dmax = 608
        Dmin = 288

        # Lấy tỷ lệ giữa W và H của ảnh và tìm ra chiều nhỏ nhất
        ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2])
        side = int(ratio * Dmin)
        bound_dim = min(side, Dmax)

        _, LpImg, lp_type = detect_lp(wpod_net,
                                      im2single(Ivehicle),
                                      bound_dim,
                                      lp_threshold=0.5)

        # Cau hinh tham so cho model SVM
        digit_w = 30  # Kich thuoc ki tu
        digit_h = 60  # Kich thuoc ki tu

        model_svm = cv2.ml.SVM_load('svm.xml')

        if (len(LpImg)):

            # Chuyen doi anh bien so
            LpImg[0] = cv2.convertScaleAbs(LpImg[0], alpha=(255.0))

            roi = LpImg[0]

            # Chuyen anh bien so ve gray
            gray = cv2.cvtColor(LpImg[0], cv2.COLOR_BGR2GRAY)

            # Ap dung threshold de phan tach so va nen
            binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)[1]

            # cv2.imshow("Anh bien so sau threshold", binary)
            # cv2.waitKey()

            # Segment kí tự
            kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
            thre_mor = cv2.morphologyEx(binary, cv2.MORPH_DILATE, kernel3)
            cont, _ = cv2.findContours(thre_mor, cv2.RETR_LIST,
                                       cv2.CHAIN_APPROX_SIMPLE)

            plate_info = ""

            for c in sort_contours(cont):
                (x, y, w, h) = cv2.boundingRect(c)
                ratio = h / w
                if 1.5 <= ratio <= 3.5:  # Chon cac contour dam bao ve ratio w/h
                    if h / roi.shape[
                            0] >= 0.6:  # Chon cac contour cao tu 60% bien so tro len

                        # Ve khung chu nhat quanh so
                        cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0),
                                      2)

                        # Tach so va predict
                        curr_num = thre_mor[y:y + h, x:x + w]
                        curr_num = cv2.resize(curr_num,
                                              dsize=(digit_w, digit_h))
                        _, curr_num = cv2.threshold(curr_num, 30, 255,
                                                    cv2.THRESH_BINARY)
                        curr_num = np.array(curr_num, dtype=np.float32)
                        curr_num = curr_num.reshape(-1, digit_w * digit_h)

                        # Dua vao model SVM
                        result = model_svm.predict(curr_num)[1]
                        result = int(result[0, 0])

                        if result <= 9:  # Neu la so thi hien thi luon
                            result = str(result)
                        else:  #Neu la chu thi chuyen bang ASCII
                            result = chr(result)

                        plate_info += result

            # cv2.imshow("Cac contour tim duoc", roi)
            # cv2.waitKey()

            # Viet bien so len anh
            cv2.putText(Ivehicle,
                        fine_tune(plate_info), (50, 50),
                        cv2.FONT_HERSHEY_PLAIN,
                        3.0, (0, 0, 255),
                        lineType=cv2.LINE_AA)

            # Hien thi anh
            print("Bien so=", plate_info)
            # cv2.imshow("Hinh anh output",Ivehicle)
            # cv2.waitKey()
            return plate_info
    except Exception:
        return ""


# chieucao, chieurong = roi.shape[:2]
#     plate_info = ""
#     plate_info2 = ""
#     plate_info1 = ""

#     for c in sort_contours(cont):
#         (x, y, w, h) = cv2.boundingRect(c)
#         if lp_type == 2:
#             if y <= chieucao/2:
#                 ratio = h/w
#                 if 1.5<=ratio<=3.5: # Chon cac contour dam bao ve ratio w/h
#                     if h/roi.shape[0]>=0.35: # Chon cac contour cao tu 35% bien so tro len

#                         # Ve khung chu nhat quanh so
#                         cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 2)

#                         # Tach so va predict
#                         curr_num = thre_mor[y:y+h,x:x+w] #tách từng ký tự sau khi vẽ được khung chữ nhật
#                         curr_num = cv2.resize(curr_num, dsize=(digit_w, digit_h))# đưa ảnh ký tự về dạng 60*30
#                         _, curr_num = cv2.threshold(curr_num, 30, 255, cv2.THRESH_BINARY)# chỉnh về màu đen trắng.

#                         curr_num = np.array(curr_num,dtype=np.float32)
#                         curr_num = curr_num.reshape(-1, digit_w * digit_h)
#                         # đưa về mảng để trùng độ dài với mảng ban đầu

#                         # Dua vao model SVM
#                         result = model_svm.predict(curr_num)[1]
#                         result = int(result[0, 0])

#                         if result<=9: # Neu la so thi hien thi luon
#                             result = str(result)
#                         else: #Neu la chu thi chuyen bang ASCII
#                             result = chr(result)

#                         plate_info2 +=result
#             else:
#                 ratio = h / w
#                 if 1.5 <= ratio <= 3.5:  # Chon cac contour dam bao ve ratio w/h
#                     if h / roi.shape[0] >= 0.35:  # Chon cac contour cao tu 35% bien so tro len

#                         # Ve khung chu nhat quanh so
#                         cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 2)

#                         # Tach so va predict
#                         curr_num = thre_mor[y:y + h, x:x + w]
#                         curr_num = cv2.resize(curr_num, dsize=(digit_w, digit_h))
#                         _, curr_num = cv2.threshold(curr_num, 30, 255, cv2.THRESH_BINARY)
#                         curr_num = np.array(curr_num, dtype=np.float32)
#                         curr_num = curr_num.reshape(-1, digit_w * digit_h)

#                         # Dua vao model SVM
#                         result = model_svm.predict(curr_num)[1]
#                         result = int(result[0, 0])

#                         if result <= 9:  # Neu la so thi hien thi luon
#                             result = str(result)
#                         else:  # Neu la chu thi chuyen bang ASCII
#                             result = chr(result)

#                         plate_info1 += result
#         else:
#             ratio = h / w
#             if 1.5 <= ratio <= 3.5:  # Chon cac contour dam bao ve ratio w/h
#                 if h / roi.shape[0] >= 0.6:  # Chon cac contour cao tu 35% bien so tro len

#                     # Ve khung chu nhat quanh so
#                     cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 2)

#                     # Tach so va predict
#                     curr_num = thre_mor[y:y + h, x:x + w]
#                     curr_num = cv2.resize(curr_num, dsize=(digit_w, digit_h))
#                     _, curr_num = cv2.threshold(curr_num, 30, 255, cv2.THRESH_BINARY)
#                     curr_num = np.array(curr_num, dtype=np.float32)
#                     curr_num = curr_num.reshape(-1, digit_w * digit_h)

#                     # Dua vao model SVM
#                     result = model_svm.predict(curr_num)[1]
#                     print(result)
#                     result = int(result[0, 0])

#                     if result <= 9:  # Neu la so thi hien thi luon
#                         result = str(result)
#                     else:  # Neu la chu thi chuyen bang ASCII
#                         result = chr(result)

#                     plate_info2 += result

#     plate_info = plate_info2 +" "+ plate_info1