Skip to content

Instantly share code, notes, and snippets.

@KostaMalsev
Last active January 12, 2021 11:01
Show Gist options
  • Save KostaMalsev/c0f52744cdde9498237422afde70c485 to your computer and use it in GitHub Desktop.
Save KostaMalsev/c0f52744cdde9498237422afde70c485 to your computer and use it in GitHub Desktop.
#run detector on test image
#it takes a little longer on the first run and then runs at normal speed.
import random
#Define utility functions for presenting the results:
def load_image_into_numpy_array(path):
"""Load an image from file into a numpy array.
Puts image into numpy array to feed into tensorflow graph.
Note that by convention we put it into a numpy array with shape
(height, width, channels), where channels=3 for RGB.
Args:
path: the file path to the image
Returns:
uint8 numpy array with shape (img_height, img_width, 3)
"""
img_data = tf.io.gfile.GFile(path, 'rb').read()
image = Image.open(BytesIO(img_data))
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
#Place your test images here:
image_path = '/content/test/mnm.jpeg'
#Store test images in nmpy array:
image_np = load_image_into_numpy_array(image_path)
#Convert images to tensor form:
input_tensor = tf.convert_to_tensor(
np.expand_dims(image_np, 0), dtype=tf.float32)
#Perform detection on the image in tensor format:
detections, predictions_dict, shapes = detect_fn(input_tensor)
#Visualize the detection boxes on the image:
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'][0].numpy(),
(detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
detections['detection_scores'][0].numpy(),
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=0.5,#0.5,#0.5
agnostic_mode=False,
)
plt.figure(figsize=(12,16))
plt.imshow(image_np_with_detections)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment