# Path: lib/config.py
# __C = edict()
# __C.DETECTOR = edict()
# __C.DETECTOR.METHOD = 0
# __C.DETECTOR.TRAIN = edict()
# __C.DETECTOR.TEST = edict()
# __C.RECOGNIZER = edict()
# __C.RECOGNIZER.METHOD = 0
# __C.RECOGNIZER.TRAIN = edict()
# __C.RECOGNIZER.TEST = edict()
# __C.ROOT_DIR = Path(os.path.dirname(__file__), '..')
# __C.DATA_DIR = __C.ROOT_DIR / 'data'
# __C.OUTPUT_DIR = __C.ROOT_DIR / 'output'
# __C.USE_DETECTOR = True
# __C.USE_RECOGNIZER = True
# __C.DEBUG = False
# __C.VIS = False
# __C.CLASSES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
#                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
#                'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
#                'W', 'X', 'Y', 'Z', '川', '鄂', '赣', '甘', '贵',
#                '桂', '黑', '沪', '冀', '津', '京', '吉', '辽',
#                '鲁', '蒙', '闽', '宁', '青', '琼', '陕', '苏',
#                '晋', '皖', '湘', '新', '豫', '渝', '粤', '云',
#                '藏', '浙')  #
# def _merge_a_into_b(a, b):
# def cfg_from_file(filename):
# 
# Path: lib/detector.py
# def detect(src):
#     """
#         Detector factory
#     :param src: raw image
#     :return: [[x1, y1, x2, y2, x3, y3, x4, y4]]
#     """
#     method = cfg.DETECTOR.METHOD
#     return eval('_detect_' + _methods[method])(src)
# 
# Path: lib/recognizer.py
# def recognize(src):
#     """
#         Recognizer factory
#     :param src: plate roi
#     :return:
#     """
#     method = cfg.RECOGNIZER.METHOD
#     return eval('_recognize_' + _methods[method])(src)
# 
# Path: lib/utils/align.py
# def align(image, points):
#     """
#     :param image:
#     :param points:
#     :return: aligned image
#     """
#     # alignment
#     origin_point = np.require(np.array(points).reshape((4, 2)), dtype=np.single)
#     height = int(max(np.linalg.norm(origin_point[0] - origin_point[1]), np.linalg.norm(origin_point[2] - origin_point[3])))
#     width = int(max(np.linalg.norm(origin_point[0] - origin_point[3]), np.linalg.norm(origin_point[1] - origin_point[2])))
# 
#     target_point = np.float32([[0, 0], [0, height], [width, height], [width, 0]])
#     map_matrix = cv2.getPerspectiveTransform(origin_point, target_point)
#     cols = width + 1
#     rows = height + 1
#     color = cv2.warpPerspective(image, map_matrix, (cols, rows))
#     return color


import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from lib.config import cfg
from lib.detector import detect as plate_detect
from lib.recognizer import recognize as chars_recognize
from lib.utils.align import align
            vis_image = align(src, res)
            ax2.imshow(cv2.cvtColor(vis_image, cv2.COLOR_BGR2RGB))
            plt.show()


def test_chars_recognize():
    print("Testing Chars Recognize")

    file = cfg.DATA_DIR / 'demo' / 'chars_recognize.jpg'
    assert file.exists()
    src = cv2.imread(str(file))
    if cfg.VIS:
        plt.imshow(cv2.cvtColor(src, cv2.COLOR_BGR2RGB))
        plt.show()

    rec_res = chars_recognize(src)
    print("Chars Recognize: {} ({})".format(rec_res, 'CORRECT' if rec_res == '沪AGH092' else 'WRONG'))


def test_plate_recognize():
    print("Testing Plate Recognize")

    file = cfg.DATA_DIR / 'demo' / 'test.jpg'

    src = cv2.imread(str(file))

    results = plate_detect(src)

    for res in results:
        print("Plate position: \n", res)

# Path: lib/config.py
# __C = edict()
# __C.DETECTOR = edict()
# __C.DETECTOR.METHOD = 0
# __C.DETECTOR.TRAIN = edict()
# __C.DETECTOR.TEST = edict()
# __C.RECOGNIZER = edict()
# __C.RECOGNIZER.METHOD = 0
# __C.RECOGNIZER.TRAIN = edict()
# __C.RECOGNIZER.TEST = edict()
# __C.ROOT_DIR = Path(os.path.dirname(__file__), '..')
# __C.DATA_DIR = __C.ROOT_DIR / 'data'
# __C.OUTPUT_DIR = __C.ROOT_DIR / 'output'
# __C.USE_DETECTOR = True
# __C.USE_RECOGNIZER = True
# __C.DEBUG = False
# __C.VIS = False
# __C.CLASSES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
#                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
#                'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
#                'W', 'X', 'Y', 'Z', '川', '鄂', '赣', '甘', '贵',
#                '桂', '黑', '沪', '冀', '津', '京', '吉', '辽',
#                '鲁', '蒙', '闽', '宁', '青', '琼', '陕', '苏',
#                '晋', '皖', '湘', '新', '豫', '渝', '粤', '云',
#                '藏', '浙')  #
# def _merge_a_into_b(a, b):
# def cfg_from_file(filename):
# 
# Path: lib/detector.py
# def detect(src):
#     """
#         Detector factory
#     :param src: raw image
#     :return: [[x1, y1, x2, y2, x3, y3, x4, y4]]
#     """
#     method = cfg.DETECTOR.METHOD
#     return eval('_detect_' + _methods[method])(src)
# 
# Path: lib/recognizer.py
# def recognize(src):
#     """
#         Recognizer factory
#     :param src: plate roi
#     :return:
#     """
#     method = cfg.RECOGNIZER.METHOD
#     return eval('_recognize_' + _methods[method])(src)
# 
# Path: lib/utils/align.py
# def align(image, points):
#     """
#     :param image:
#     :param points:
#     :return: aligned image
#     """
#     # alignment
#     origin_point = np.require(np.array(points).reshape((4, 2)), dtype=np.single)
#     height = int(max(np.linalg.norm(origin_point[0] - origin_point[1]), np.linalg.norm(origin_point[2] - origin_point[3])))
#     width = int(max(np.linalg.norm(origin_point[0] - origin_point[3]), np.linalg.norm(origin_point[1] - origin_point[2])))
# 
#     target_point = np.float32([[0, 0], [0, height], [width, height], [width, 0]])
#     map_matrix = cv2.getPerspectiveTransform(origin_point, target_point)
#     cols = width + 1
#     rows = height + 1
#     color = cv2.warpPerspective(image, map_matrix, (cols, rows))
#     return color


# Path: plate.py
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from lib.config import cfg
from lib.detector import detect as plate_detect
from lib.recognizer import recognize as chars_recognize
from lib.utils.align import align

            vis_image = align(src, res)
            ax2.imshow(cv2.cvtColor(vis_image, cv2.COLOR_BGR2RGB))
            plt.show()


def test_chars_recognize():
    print("Testing Chars Recognize")

    file = cfg.DATA_DIR / 'demo' / 'chars_recognize.jpg'
    assert file.exists()
    src = cv2.imread(str(file))
    if cfg.VIS:
        plt.imshow(cv2.cvtColor(src, cv2.COLOR_BGR2RGB))
        plt.show()

    rec_res = chars_recognize(src)
    print("Chars Recognize: {} ({})".format(rec_res, 'CORRECT' if rec_res == '沪AGH092' else 'WRONG'))


def test_plate_recognize():
    print("Testing Plate Recognize")

    file = cfg.DATA_DIR / 'demo' / 'test.jpg'

    src = cv2.imread(str(file))

    results = plate_detect(src)

    for res in results:
        print("Plate position: \n", res)

        vis_image = align(src, res)