# 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: models/mrcnn/plate.py
# class PlateInferenceConfig(PlateConfig):
#     IMAGE_MAX_DIM = 512
#     IMAGE_MIN_DIM = 512


import cv2
import keras.backend.tensorflow_backend as KTF
import numpy as np
import tensorflow as tf
import models.mrcnn.model as modellib
                import matplotlib.pyplot as plt
                import matplotlib.patches as patches
from lib.config import cfg
from models.mrcnn.plate import PlateInferenceConfig
"""
    Use mask-rcnn to do plate detect
"""



class Singleton(type):
    _inst = {}

    def __call__(self, *args, **kw):
        if self not in self._inst:
            self._inst[self] = super(Singleton, self).__call__(*args, **kw)
        return self._inst[self]


class PlateDetect(metaclass=Singleton):
    def __init__(self):
        self.graph = tf.Graph()
        self.eval_sess = None
        self.model = None

    def detect(self, src, model_dir):
        first = self.eval_sess is None
        if first:
            # if first load
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            self.eval_sess = tf.Session(graph=self.graph, config=config)
        with self.graph.as_default():
            if first:

# 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: models/mrcnn/plate.py
# class PlateInferenceConfig(PlateConfig):
#     IMAGE_MAX_DIM = 512
#     IMAGE_MIN_DIM = 512


# Path: lib/mrcnn/plate_detect.py
import cv2
import keras.backend.tensorflow_backend as KTF
import numpy as np
import tensorflow as tf
import models.mrcnn.model as modellib
                import matplotlib.pyplot as plt
                import matplotlib.patches as patches
from lib.config import cfg
from models.mrcnn.plate import PlateInferenceConfig

"""
    Use mask-rcnn to do plate detect
"""



class Singleton(type):
    _inst = {}

    def __call__(self, *args, **kw):
        if self not in self._inst:
            self._inst[self] = super(Singleton, self).__call__(*args, **kw)
        return self._inst[self]


class PlateDetect(metaclass=Singleton):
    def __init__(self):
        self.graph = tf.Graph()
        self.eval_sess = None
        self.model = None

    def detect(self, src, model_dir):
        first = self.eval_sess is None
        if first:
            # if first load
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            self.eval_sess = tf.Session(graph=self.graph, config=config)
        with self.graph.as_default():
            if first:

                plate_config = PlateInferenceConfig()