# 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/utils/find_last.py
# def find_last(output_dir, prefix):
#     # find the last checkpoint
#     dir_names = next(os.walk(output_dir))[1]
#     dir_names = filter(lambda f: f.startswith(prefix), dir_names)
#     dir_names = sorted(dir_names)
#     assert dir_names, 'There is no trained model. Please download or train.'
#     return dir_names[-1]
# 
# Path: lib/easypr/chars_recognize.py
# class CharsRecognize(object):
#     def __init__(self):
#         self.charsSegment = CharsSegment()
# 
#     def charsRecognize(self, plate, model_dir):
#         chars = []
#         result = self.charsSegment.charsSegment(plate, chars)
#         if cfg.DEBUG:
#             import matplotlib.pyplot as plt
#             for i, char in enumerate(chars):
#                 plt.subplot(1, len(chars), i+1)
#                 plt.imshow(char, cmap='gray')
#             plt.show()
# 
#         temp = []
#         plate_license = ""
# 
#         if result == 0:
#             temp = chars_identify.identify(np.array(chars)[..., None], model_dir)
# 
#         for index in temp:
#             plate_license += cfg.CLASSES[index]
# 
#         return plate_license


from lib.config import cfg
from lib.utils.find_last import find_last
from lib.easypr.chars_recognize import CharsRecognize
# @Author  : fh
# @File    : recognizer.py
# @Desc    :
"""
    The recognizer wrapper of ULPR. It will recognize the plate license of the plate roi.
    If you want to add a method, add the name in methods and the corresponding function at the end of the file.
"""


# lib methods

_methods = [
    'easypr',  # 0
]


def recognize(src):
    """
        Recognizer factory
    :param src: plate roi
    :return:
    """
    method = cfg.RECOGNIZER.METHOD
    return eval('_recognize_' + _methods[method])(src)


# method 0
def _recognize_easypr(src):
    cr = CharsRecognize()
    dir_name = find_last(cfg.OUTPUT_DIR, 'chars')

# 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/utils/find_last.py
# def find_last(output_dir, prefix):
#     # find the last checkpoint
#     dir_names = next(os.walk(output_dir))[1]
#     dir_names = filter(lambda f: f.startswith(prefix), dir_names)
#     dir_names = sorted(dir_names)
#     assert dir_names, 'There is no trained model. Please download or train.'
#     return dir_names[-1]
# 
# Path: lib/easypr/chars_recognize.py
# class CharsRecognize(object):
#     def __init__(self):
#         self.charsSegment = CharsSegment()
# 
#     def charsRecognize(self, plate, model_dir):
#         chars = []
#         result = self.charsSegment.charsSegment(plate, chars)
#         if cfg.DEBUG:
#             import matplotlib.pyplot as plt
#             for i, char in enumerate(chars):
#                 plt.subplot(1, len(chars), i+1)
#                 plt.imshow(char, cmap='gray')
#             plt.show()
# 
#         temp = []
#         plate_license = ""
# 
#         if result == 0:
#             temp = chars_identify.identify(np.array(chars)[..., None], model_dir)
# 
#         for index in temp:
#             plate_license += cfg.CLASSES[index]
# 
#         return plate_license


# Path: lib/recognizer.py
from lib.config import cfg
from lib.utils.find_last import find_last
from lib.easypr.chars_recognize import CharsRecognize

# @Author  : fh
# @File    : recognizer.py
# @Desc    :
"""
    The recognizer wrapper of ULPR. It will recognize the plate license of the plate roi.
    If you want to add a method, add the name in methods and the corresponding function at the end of the file.
"""


# lib methods

_methods = [
    'easypr',  # 0
]


def recognize(src):
    """
        Recognizer factory
    :param src: plate roi
    :return:
    """
    method = cfg.RECOGNIZER.METHOD
    return eval('_recognize_' + _methods[method])(src)


# method 0
def _recognize_easypr(src):
    cr = CharsRecognize()
    dir_name = find_last(cfg.OUTPUT_DIR, 'chars')

    return cr.charsRecognize(src, str(cfg.OUTPUT_DIR / dir_name / 'models'))