Skip to content

Instantly share code, notes, and snippets.

@shriyaRam
Created August 28, 2019 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shriyaRam/0921fb8606c9c9f4086b4a17ee9814e1 to your computer and use it in GitHub Desktop.
Save shriyaRam/0921fb8606c9c9f4086b4a17ee9814e1 to your computer and use it in GitHub Desktop.
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))
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.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment