def detect(im, param_vals):
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))

    x, y, params = model.get_detect_model()

    with tf.Session() as sess:
        y_vals = []
        for scaled_im in scaled_ims:
            feed_dict = {x: numpy.stack([scaled_im])}
            feed_dict.update(dict(zip(params, param_vals)))
            y_vals.append(sess.run(y, feed_dict=feed_dict))

    for i, (scaled_im, y_val) in enumerate(zip(scaled_ims, y_vals)):
        for window_coords in numpy.argwhere(y_val[0, :, :, 0] >
                                                       -math.log(1./0.99 - 1)):
            letter_probs = (y_val[0,
                                  window_coords[0],
                                  window_coords[1], 1:].reshape(
                                    7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)

            img_scale = float(im.shape[0]) / scaled_im.shape[0]

            bbox_tl = window_coords * (8, 4) * img_scale
            bbox_size = numpy.array(model.WINDOW_SHAPE) * img_scale

            present_prob = common.sigmoid(
                               y_val[0, window_coords[0], window_coords[1], 0])

            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs
예제 #2
0
def attack(im, param_vals):
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))
    img = scaled_ims[2]
    input = numpy.stack([img])

    x_hat = tf.Variable(tf.zeros(input.shape), dtype=tf.float32)

    assign_op = tf.assign(x_hat, input)

    _, y, params = model.get_detect_model(x_hat)

    y_val = tf.reduce_mean(y)

    optim_step = tf.train.GradientDescentOptimizer(1e-1).minimize(
        y_val, var_list=[x_hat])

    with tf.Session(config=tf.ConfigProto()) as sess:
        sess.run(assign_op)
        feed_dict = {}
        feed_dict.update(dict(zip(params, param_vals)))
        for i in range(1):
            sess.run(optim_step, feed_dict=feed_dict)
            print(sess.run(y_val, feed_dict=feed_dict))
        adv = x_hat.eval()
    adv = adv[0, :, :]
    print(adv)
    print(adv.shape)
    print(img)
    print(img.shape)
    #valid_imshow_data(adv.shape)
    plt.imshow(adv)
    plt.show()
예제 #3
0
def predict(list_image_path, param_values):
    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()

    # Execute the model at each scale.
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)
    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        for image_name in list_image_path:
            im_gray = cv2.imread(image_name, cv2.IMREAD_GRAYSCALE) / 255.
            im_gray = cv2.resize(im_gray, (128, 64))

            print("-------------")
            feed_dict = {x: numpy.stack([im_gray])}
            feed_dict.update(dict(zip(params, param_values)))
            y_val = sess.run(y, feed_dict=feed_dict)

            letter_probs = (y_val[0,
                            0,
                            0, 1:].reshape(
                10, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)

            present_prob = common.sigmoid(y_val[0, 0, 0, 0])
            print("input", image_name)
            print("output", letter_probs_to_code(letter_probs))
예제 #4
0
def detect(im, param_vals):
    """
    Detect all bounding boxes of number plates in an image.
    :param im:
        Image to detect number plates in.
    :param param_vals:
        Model parameters to use. These are the parameters output by the `train`
        module.
    :returns:
        Iterable of `bbox_tl, bbox_br, letter_probs`, defining the bounding box
        top-left and bottom-right corners respectively, and a 7,36 matrix
        giving the probability distributions of each letter.
    """

    # Convert the image to various scales.
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))

    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()

    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:
        y_vals = []

        for scaled_im in scaled_ims:
            feed_dict = {x: numpy.stack([scaled_im])}
            feed_dict.update(dict(zip(params, param_vals)))
            y_vals.append(sess.run(y, feed_dict=feed_dict))
            plt.imshow(scaled_im)
            plt.show()
    writer = tf.summary.FileWriter("logs/", sess.graph)

    # Interpret the results in terms of bounding boxes in the input image.
    # Do this by identifying windows (at all scales) where the model predicts a
    # number plate has a greater than 50% probability of appearing.
    #
    # To obtain pixel coordinates, the window coordinates are scaled according
    # to the stride size, and pixel coordinates.
    count_detect = 0
    for i, (scaled_im, y_val) in enumerate(zip(scaled_ims, y_vals)):
        for window_coords in numpy.argwhere(
                y_val[0, :, :, 0] > -math.log(1. / 0.99 - 1)):
            letter_probs = (y_val[0, window_coords[0], window_coords[1],
                                  1:].reshape(7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)

            img_scale = float(im.shape[0]) / scaled_im.shape[0]

            bbox_tl = window_coords * (8, 4) * img_scale
            bbox_size = numpy.array(model.WINDOW_SHAPE) * img_scale

            present_prob = common.sigmoid(y_val[0, window_coords[0],
                                                window_coords[1], 0])
            count_detect += 1
            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs
            print("count detect:", count_detect)
            print("show return window: ", bbox_tl, "return windows box: ",
                  bbox_tl + bbox_size)
            print("present: ", present_prob)
            print("letter: ", letter_probs_to_code(letter_probs))
예제 #5
0
 def __init__(self, param_file):
     f = numpy.load(param_file)
     self.param_vals = [
         f[n] for n in sorted(f.files, key=lambda s: int(s[4:]))
     ]
     self.x, self.y, self.params = model.get_detect_model()
     self.sess = tf.Session(config=tf.ConfigProto())
예제 #6
0
def detect(im, param_vals):
    """
    Detect number plates in an image.

    :param im:
        Image to detect number plates in.

    :param param_vals:
        Model parameters to use. These are the parameters output by the `train`
        module.

    :returns:
        Iterable of `bbox_tl, bbox_br, letter_probs`, defining the bounding box
        top-left and bottom-right corners respectively, and a 7,36 matrix
        giving the probability distributions of each letter.

    """

    x, y, params = model.get_detect_model()
    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:
        feed_dict = {x: [im]}
        feed_dict.update(dict(zip(params, param_vals)))
        predict_values = sess.run(y, feed_dict=feed_dict)
        letter_probs = predict_values.reshape(common.LENGTH, len(common.DIGITS))
        letter_probs = common.softmax(letter_probs)
        code= letter_probs_to_code(letter_probs)
    return code
예제 #7
0
파일: detect.py 프로젝트: 0x3a/deep-anpr
def detect(im, param_vals):
    """
    Detect number plates in an image.

    :param im:
        Image to detect number plates in.

    :param param_vals:
        Model parameters to use. These are the parameters output by the `train`
        module.

    :returns:
        Iterable of `bbox_tl, bbox_br, letter_probs`, defining the bounding box
        top-left and bottom-right corners respectively, and a 7,36 matrix
        giving the probability distributions of each letter.

    """

    # Convert the image to various scales.
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))

    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()

    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:
        y_vals = []
        for scaled_im in scaled_ims:
            feed_dict = {x: numpy.stack([scaled_im])}
            feed_dict.update(dict(zip(params, param_vals)))
            y_vals.append(sess.run(y, feed_dict=feed_dict))

    # Interpret the results in terms of bounding boxes in the input image.
    # Do this by identifying windows (at all scales) where the model predicts a
    # number plate has a greater than 50% probability of appearing.
    #
    # To obtain pixel coordinates, the window coordinates are scaled according
    # to the stride size, and pixel coordinates.
    for i, (scaled_im, y_val) in enumerate(zip(scaled_ims, y_vals)):
        for window_coords in numpy.argwhere(y_val[0, :, :, 0] >
                                                       -math.log(1./0.99 - 1)):
            letter_probs = (y_val[0,
                                  window_coords[0],
                                  window_coords[1], 1:].reshape(
                                    7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)

            img_scale = float(im.shape[0]) / scaled_im.shape[0]

            bbox_tl = window_coords * (8, 4) * img_scale
            bbox_size = numpy.array(model.WINDOW_SHAPE) * img_scale

            present_prob = common.sigmoid(
                               y_val[0, window_coords[0], window_coords[1], 0])

            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs
예제 #8
0
def detect_presense(im, param_vals):
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))
    img = scaled_ims[2]
    x, y, params = model.get_detect_model()

    with tf.Session(config=tf.ConfigProto()) as sess:
        feed_dict = {x: numpy.stack([img])}
        feed_dict.update(dict(zip(params, param_vals)))
        y_val = sess.run(y, feed_dict=feed_dict)
        y_val[0, :, :, 0]
    if (len(numpy.argwhere(y_val[0, :, :, 0] > -math.log(1. / 0.99 - 1)))):
        print('[FOUND PLATE]')
예제 #9
0
    def perturb(img, param_vals):
        x_origin = tf.Variable(tf.zeros(img.shape))
        assign_origin_op = tf.assign(x_origin, img)

        #convert image to gray so it can pass through detect model
        im_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) / 255.

        #resize colored image to match best scaled image shape
        max_im_shape = detect_max_shape(im_gray, param_vals)
        im_color_resize = tf.squeeze(
            tf.image.resize_images(x_origin,
                                   (max_im_shape[0], max_im_shape[1])))

        img_gray = tf.image.rgb_to_grayscale(im_color_resize)  #(250,250,1)
        im_gray = tf.squeeze(tf.cast(img_gray, numpy.float32))  #(250,250)

        input = tf.stack([im_gray])  #(1,250,250)

        _, y, params = model.get_detect_model(input)

        #mean over reduced probability for presence and letter detection, y[:,:,:,0] for just presence
        y_mean = tf.reduce_mean(y)

        optim_step = tf.train.GradientDescentOptimizer(3).minimize(
            y_mean, var_list=[x_origin])
        adv = []

        #create bounds for how far the image can deviate
        epsilon = tf.placeholder(tf.float32, ())
        below = img - epsilon
        above = img + epsilon
        projected = tf.clip_by_value(tf.clip_by_value(x_origin, below, above),
                                     0, 255)

        with tf.control_dependencies([projected]):
            project_step = tf.assign(x_origin, projected)

        #training
        with tf.Session(config=tf.ConfigProto()) as sess:
            sess.run(assign_origin_op)
            feed_dict = {}
            feed_dict.update(dict(zip(params, param_vals)))
            for i in range(400):
                sess.run(optim_step, feed_dict=feed_dict)
                sess.run(project_step, feed_dict={epsilon: 8000 / 255.0})
                print(sess.run(y_mean, feed_dict=feed_dict))
                if (isDetected(x_origin.eval().astype(numpy.uint8),
                               param_vals) == False):
                    break
            adv = (x_origin.eval().astype(numpy.uint8))

        return adv
예제 #10
0
def isDetected(im, param_vals):
    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) / 255.
    scaled_ims = list(make_scaled_ims(im_gray, model.WINDOW_SHAPE))
    x, y, params = model.get_detect_model()

    with tf.Session(config=tf.ConfigProto()) as sess:
        y_vals = []
        for scaled_im in scaled_ims:
            feed_dict = {x: numpy.stack([scaled_im])}
            feed_dict.update(dict(zip(params, param_vals)))
            y_vals.append(sess.run(y, feed_dict=feed_dict))

    count = 0
    for i, (scaled_im, y_val) in enumerate(zip(scaled_ims, y_vals)):
        for window_coords in numpy.argwhere(
                y_val[0, :, :, 0] > -math.log(1. / 0.99 - 1)):
            count = count + 1  #only care about number of boxes it can detect

    print("COUNT: ", count)
    return count != 0
예제 #11
0
def detect(im, param_vals):

    # Convert the image to various scales.
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))

    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()

    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:
        y_vals = []
        for scaled_im in scaled_ims:
            feed_dict = {x: numpy.stack([scaled_im])}
            feed_dict.update(dict(zip(params, param_vals)))
            y_vals.append(sess.run(y, feed_dict=feed_dict))
    # Interpret the results in terms of bounding boxes in the input image.
    # Do this by identifying windows (at all scales) where the model predicts a
    # number plate has a greater than 50% probability of appearing.
    #
    # To obtain pixel coordinates, the window coordinates are scaled according
    # to the stride size, and pixel coordinates.
    for i, (scaled_im, y_val) in enumerate(zip(scaled_ims, y_vals)):
        #print(i)
        #print(numpy.argwhere(y_val[0, :, :, 0] > -math.log(1./0.99 - 1)))
        #print(-math.log(1./0.99 - 1))
        #print(numpy.argwhere(y_val[0, :, :, 0] >-math.log(1./0.99 - 1)))
        for window_coords in numpy.argwhere(
                y_val[0, :, :, 0] > -math.log(1. / 0.99 - 1)):

            letter_probs = (y_val[0, window_coords[0], window_coords[1],
                                  1:].reshape(7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)

            img_scale = float(im.shape[0]) / scaled_im.shape[0]

            bbox_tl = window_coords * (8, 4) * img_scale
            bbox_size = numpy.array(model.WINDOW_SHAPE) * img_scale

            present_prob = common.sigmoid(y_val[0, window_coords[0],
                                                window_coords[1], 0])
            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs
예제 #12
0
파일: detect.py 프로젝트: sernger/ANPR
def detect(im, param_vals):
    """
    Detect number plates in an image.

    :param im:
        Image to detect number plates in.

    :param param_vals:
        Model parameters to use. These are the parameters output by the `train`
        module.

    :returns:
        Iterable of `bbox_tl, bbox_br, letter_probs`, defining the bounding box
        top-left and bottom-right corners respectively, and a 7,36 matrix
        giving the probability distributions of each letter.

    """

    # Convert the image to various scales.
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))

    for i, im in enumerate(scaled_ims):
        cv2.imwrite("scale{}.bmp".format(i), im*255.0)
    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()

    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:
        y_vals = []
        for scaled_im in scaled_ims:
            feed_dict = {x: numpy.stack([scaled_im])}
            feed_dict.update(dict(zip(params, param_vals)))
            y_vals.append(sess.run(y, feed_dict=feed_dict))

    # Interpret the results in terms of bounding boxes in the input image.
    # Do this by identifying windows (at all scales) where the model predicts a
    # number plate has a greater than 50% probability of appearing.
    #
    # To obtain pixel coordinates, the window coordinates are scaled according
    # to the stride size, and pixel coordinates.
    for i, (scaled_im, y_val) in enumerate(zip(scaled_ims, y_vals)):
        #for window_coords in numpy.argwhere(y_val[0, :, :, 0] >
        #                                               -math.log(1./0.99 - 1)):
#        win = numpy.argmax(y_val[0, :, :, 0])
#        print "win point:{} {} max:{}".format(win/y_val.shape[1], win%y_val.shape[1], 
#                                              y_val[0, win/y_val.shape[1], win%y_val.shape[1], 0])
        for window_coords in numpy.argwhere(y_val[0, :, :, 0] > 0):
            p_probs = y_val[0, window_coords[0], window_coords[1], 0]
#            print "p_probs:{}".format(p_probs)
            letter_probs = (y_val[0,
                                  window_coords[0],
                                  window_coords[1], 1:].reshape(
                                    7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)

            img_scale = float(im.shape[0]) / scaled_im.shape[0]

            bbox_tl = window_coords * (16, 8) * img_scale
            bbox_size = numpy.array(model.WINDOW_SHAPE) * img_scale

            present_prob = common.sigmoid(
                               y_val[0, window_coords[0], window_coords[1], 0])

            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs
예제 #13
0
def detect(im, param_vals):
    """
    Detect number plates in an image.

    :param im:
        Image to detect number plates in.

    :param param_vals:
        Model parameters to use. These are the parameters output by the `train`
        module.

    :returns:
        Iterable of `bbox_tl, bbox_br, letter_probs`, defining the bounding box
        top-left and bottom-right corners respectively, and a 7,36 matrix
        giving the probability distributions of each letter.

    """

    # Convert the image to various scales.
    scaled_ims = list(make_scaled_ims(im,
                                      model.WINDOW_SHAPE))  # 针对需检测图片产生迭代缩放图片列表

    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()  # 获取检测网络(5个卷积层)的输入,输出以及所有参数

    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:  # 建立会话
        y_vals = []
        for scaled_im in scaled_ims:  # 遍历缩放图片列表
            feed_dict = {x: numpy.stack([scaled_im])}  # 建立字典feed_dict{输入:缩放图片}
            feed_dict.update(dict(zip(
                params,
                param_vals)))  # 将字典{参数:参数值}更新到字典feed_dict中->{输入:缩放图片, 参数:参数值}
            y_vals.append(sess.run(y,
                                   feed_dict=feed_dict))  # 提供检测图片和网络参数值,执行检测操作

    # Interpret the results in terms of bounding boxes in the input image.
    # Do this by identifying windows (at all scales) where the model predicts a
    # number plate has a greater than 50% probability of appearing.
    #
    # To obtain pixel coordinates, the window coordinates are scaled according
    # to the stride size, and pixel coordinates.
    for i, (scaled_im,
            y_val) in enumerate(zip(scaled_ims,
                                    y_vals)):  # 枚举元组(缩放图片, 检测输出)及对应下标
        for window_coords in numpy.argwhere(
                y_val[0, :, :, 0] > -math.log(1. / 0.99 - 1)
        ):  # 对应sigmoid输出号码牌存在概率大于0.99的下标
            letter_probs = (y_val[0, window_coords[0], window_coords[1],
                                  1:].reshape(7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)  # 车牌号识别正确的概率分布

            img_scale = float(im.shape[0]) / scaled_im.shape[0]  # 缩放比例

            bbox_tl = window_coords * (
                8, 4) * img_scale  # 计算缩放图片上号码牌的boudingbox的左上角坐标
            bbox_size = numpy.array(
                model.WINDOW_SHAPE) * img_scale  # 号码牌的boudingbox的尺寸

            present_prob = common.sigmoid(y_val[0, window_coords[0],
                                                window_coords[1],
                                                0])  # 号码牌是否存在的概率分布

            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs  # 返回号码牌boudingbox的左上角坐标,右下角坐标,
예제 #14
0
)

import collections
import itertools
import math
import sys

import cv2
import numpy
import tensorflow as tf

import common
import model
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.50)
sess1 = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
x, y, params = model.get_detect_model()

f = numpy.load("weights.npz")
param_vals = [f[n] for n in sorted(f.files, key=lambda s: int(s[4:]))]


def make_scaled_ims(im, min_shape):

    ratio = 1. / 2**0.5
    shape = (im.shape[0] / ratio, im.shape[1] / ratio)
    #(479.4183976444793, 388.90872965260115)#(im.shape[0] / ratio, im.shape[1] / ratio)
    print("MIN SHAPE", shape, min_shape)
    while True:
        shape = (int(shape[0] * ratio), int(shape[1] * ratio))
        if shape[0] < min_shape[0] or shape[1] < min_shape[1]:
            break