# 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
        masks = result[0]['masks']
        bboxes = result[0]['rois']
        plates = []
        for idx, bbox in enumerate(bboxes):
            mask = masks[..., idx]
            x1, y1, x2, y2 = min(bbox[1], bbox[3]), min(bbox[2], bbox[0]), max(bbox[1], bbox[3]), max(bbox[2], bbox[0])
            """
                (x1, y1) -------- (x2, y1)
                        |        |
                        |        |
                        |        |
                (x1, y2) -------- (x2, y2)        
            """
            w, h = abs(bbox[3] - bbox[1]), abs(bbox[2] - bbox[0])
            # use mask.copy() because of a bug of opencv-python for some pictures
            # TypeError: Layout of the output array image is incompatible with cv::Mat (step[ndims-1] != elemsize
            # or step[1] != elemsize*nchannels)
            _, contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            contour = contours[0].squeeze()
            dis_11 = np.linalg.norm(contour - np.array([x1, y1]), axis=1)
            top_left = contour[np.argmin(dis_11)] + np.array([-3, -3])
            dis_12 = np.linalg.norm(contour - np.array([x1, y2]), axis=1)
            bottom_left = contour[np.argmin(dis_12)] + np.array([-3, 3])
            dis_21 = np.linalg.norm(contour - np.array([x2, y1]), axis=1)
            top_right = contour[np.argmin(dis_21)] + np.array([3, -3])
            dis_22 = np.linalg.norm(contour - np.array([x2, y2]), axis=1)
            bottom_right = contour[np.argmin(dis_22)] + np.array([3, 3])
            approx = np.vstack([top_left, bottom_left, bottom_right, top_right])
            plates.append(approx)


# 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

        masks = result[0]['masks']
        bboxes = result[0]['rois']
        plates = []
        for idx, bbox in enumerate(bboxes):
            mask = masks[..., idx]
            x1, y1, x2, y2 = min(bbox[1], bbox[3]), min(bbox[2], bbox[0]), max(bbox[1], bbox[3]), max(bbox[2], bbox[0])
            """
                (x1, y1) -------- (x2, y1)
                        |        |
                        |        |
                        |        |
                (x1, y2) -------- (x2, y2)        
            """
            w, h = abs(bbox[3] - bbox[1]), abs(bbox[2] - bbox[0])
            # use mask.copy() because of a bug of opencv-python for some pictures
            # TypeError: Layout of the output array image is incompatible with cv::Mat (step[ndims-1] != elemsize
            # or step[1] != elemsize*nchannels)
            _, contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            contour = contours[0].squeeze()
            dis_11 = np.linalg.norm(contour - np.array([x1, y1]), axis=1)
            top_left = contour[np.argmin(dis_11)] + np.array([-3, -3])
            dis_12 = np.linalg.norm(contour - np.array([x1, y2]), axis=1)
            bottom_left = contour[np.argmin(dis_12)] + np.array([-3, 3])
            dis_21 = np.linalg.norm(contour - np.array([x2, y1]), axis=1)
            top_right = contour[np.argmin(dis_21)] + np.array([3, -3])
            dis_22 = np.linalg.norm(contour - np.array([x2, y2]), axis=1)
            bottom_right = contour[np.argmin(dis_22)] + np.array([3, 3])
            approx = np.vstack([top_left, bottom_left, bottom_right, top_right])
            plates.append(approx)


            if cfg.VIS: