# Path: models/easypr/net/judgenet.py
# class Judgenet:
# 
#     def __init__(self):
#         self.num_classes = 2
# 
#         self.x = None
#         self.y = None
#         self.keep_prob = None
# 
#         self.pred_logits = None
#         self.pred_labels = None
# 
#         self.accuracy = None
# 
#         self.l2_loss = None
# 
#         self.weights = []
#         self.biases = []
# 
#     def compile(self):
# 
#         self.keep_prob = tf.placeholder(tf.float32)
# 
#         self.weights = []
#         self.biases = []
# 
#         input = ImageLayer(36, 136, 3, layer_name='JUDGE_IMAGE')
#         label = LabelLayer()
# 
#         convpools1 = ConvPoolLayer(input, 5, 5, 16, 2, 2, stride=(2, 1), layer_name='JUDGE_1')
# 
#         convpools2 = ConvPoolLayer(convpools1, 5, 5, 32, 2, 2, stride=(2, 1), layer_name='JUDGE_2')
# 
#         dp = DropoutLayer(convpools2, self.keep_prob, layer_name='JUDGE_DP')
# 
#         flatten = FlattenLayer(dp, layer_name='JUDGE_FLATTEN')
#         ip1 = DenseLayer(flatten, 128, layer_name="JUDGE_DENSE1")
#         self.weights += ip1.weights
#         self.biases += ip1.biases
#         ip1_relu = ActivationLayer(ip1, layer_name='JUDGE_ACT')
# 
#         pred = OutputLayer(ip1_relu, self.num_classes, layer_name='JUDGE_OUTPUT')
#         self.weights += pred.weights
#         self.biases += pred.biases
# 
#         self.x = input.output
#         self.y = label.output
#         self.pred_logits = pred.output
#         self.pred_labels = tf.argmax(self.pred_logits, 1)
# 
#         correct_pred = tf.equal(self.pred_labels, self.y)
#         self.accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# 
#         for w in self.weights + self.biases:
#             l2_loss = tf.nn.l2_loss(w)
#             if self.l2_loss is None:
#                 self.l2_loss = l2_loss
#             else:
#                 self.l2_loss += l2_loss
# 
# Path: models/easypr/cnn_train.py
# def eval_model(nodes, samples_feed, eval_sess=None, model_dir=None, first=True):
#     if first:
#         ckpt = tf.train.get_checkpoint_state(model_dir)
#         if ckpt and ckpt.model_checkpoint_path:
#             saver = tf.train.Saver()
#             print("Model restored...", ckpt.model_checkpoint_path)
#             saver.restore(eval_sess, ckpt.model_checkpoint_path)
#     return eval_sess.run(nodes, samples_feed)


import numpy as np
import tensorflow as tf
from models.easypr.net.judgenet import Judgenet
from models.easypr.cnn_train import eval_model



class _PlateJudge:
    def __init__(self):
        self.graph = tf.Graph()
        self.eval_sess = None
        self.model = None

    def judge(self, plates, model_dir):
        judgeRes = []
        images = []
        for plate in plates:
            images.append(plate.plate_image)

        tmp = np.array(images) / 255 * 2 - 1
        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: models/easypr/net/judgenet.py
# class Judgenet:
# 
#     def __init__(self):
#         self.num_classes = 2
# 
#         self.x = None
#         self.y = None
#         self.keep_prob = None
# 
#         self.pred_logits = None
#         self.pred_labels = None
# 
#         self.accuracy = None
# 
#         self.l2_loss = None
# 
#         self.weights = []
#         self.biases = []
# 
#     def compile(self):
# 
#         self.keep_prob = tf.placeholder(tf.float32)
# 
#         self.weights = []
#         self.biases = []
# 
#         input = ImageLayer(36, 136, 3, layer_name='JUDGE_IMAGE')
#         label = LabelLayer()
# 
#         convpools1 = ConvPoolLayer(input, 5, 5, 16, 2, 2, stride=(2, 1), layer_name='JUDGE_1')
# 
#         convpools2 = ConvPoolLayer(convpools1, 5, 5, 32, 2, 2, stride=(2, 1), layer_name='JUDGE_2')
# 
#         dp = DropoutLayer(convpools2, self.keep_prob, layer_name='JUDGE_DP')
# 
#         flatten = FlattenLayer(dp, layer_name='JUDGE_FLATTEN')
#         ip1 = DenseLayer(flatten, 128, layer_name="JUDGE_DENSE1")
#         self.weights += ip1.weights
#         self.biases += ip1.biases
#         ip1_relu = ActivationLayer(ip1, layer_name='JUDGE_ACT')
# 
#         pred = OutputLayer(ip1_relu, self.num_classes, layer_name='JUDGE_OUTPUT')
#         self.weights += pred.weights
#         self.biases += pred.biases
# 
#         self.x = input.output
#         self.y = label.output
#         self.pred_logits = pred.output
#         self.pred_labels = tf.argmax(self.pred_logits, 1)
# 
#         correct_pred = tf.equal(self.pred_labels, self.y)
#         self.accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# 
#         for w in self.weights + self.biases:
#             l2_loss = tf.nn.l2_loss(w)
#             if self.l2_loss is None:
#                 self.l2_loss = l2_loss
#             else:
#                 self.l2_loss += l2_loss
# 
# Path: models/easypr/cnn_train.py
# def eval_model(nodes, samples_feed, eval_sess=None, model_dir=None, first=True):
#     if first:
#         ckpt = tf.train.get_checkpoint_state(model_dir)
#         if ckpt and ckpt.model_checkpoint_path:
#             saver = tf.train.Saver()
#             print("Model restored...", ckpt.model_checkpoint_path)
#             saver.restore(eval_sess, ckpt.model_checkpoint_path)
#     return eval_sess.run(nodes, samples_feed)


# Path: lib/easypr/plate_judge.py
import numpy as np
import tensorflow as tf
from models.easypr.net.judgenet import Judgenet
from models.easypr.cnn_train import eval_model




class _PlateJudge:
    def __init__(self):
        self.graph = tf.Graph()
        self.eval_sess = None
        self.model = None

    def judge(self, plates, model_dir):
        judgeRes = []
        images = []
        for plate in plates:
            images.append(plate.plate_image)

        tmp = np.array(images) / 255 * 2 - 1
        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:

                self.model = Judgenet()