Last active
February 27, 2021 14:18
-
-
Save Phelan164/262c8c50781b832ef653789d7aab2f3f to your computer and use it in GitHub Desktop.
object detection tensorflow hub example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For running inference on the TF-Hub module. | |
import tensorflow as tf | |
import tensorflow_hub as hub | |
# For downloading the image. | |
import tempfile | |
from six.moves.urllib.request import urlopen | |
from six import BytesIO | |
# For drawing onto the image. | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from PIL import Image | |
from PIL import ImageColor | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
from PIL import ImageOps | |
print('loading model...') | |
hub_model = hub.load('https://tfhub.dev/tensorflow/ssd_mobilenet_v2/2') | |
print('model loaded!') | |
import cv2 | |
import numpy as np | |
# By Heiko Gorski, Source: https://commons.wikimedia.org/wiki/File:Naxos_Taverna.jpg | |
image_np = cv2.imread("Naxos_Taverna.jpg") | |
# running inference | |
results = hub_model(np.expand_dims(image_np, axis=0)) | |
result = {key:value.numpy() for key,value in results.items()} | |
def display_image(image): | |
fig = plt.figure(figsize=(20, 15)) | |
plt.grid(False) | |
plt.imshow(image) | |
plt.show() | |
def draw_bounding_box_on_image(image, | |
ymin, | |
xmin, | |
ymax, | |
xmax, | |
color, | |
font, | |
thickness=4, | |
display_str_list=()): | |
"""Adds a bounding box to an image.""" | |
draw = ImageDraw.Draw(image) | |
im_width, im_height = image.size | |
(left, right, top, bottom) = (xmin * im_width, xmax * im_width, | |
ymin * im_height, ymax * im_height) | |
draw.line([(left, top), (left, bottom), (right, bottom), (right, top), | |
(left, top)], | |
width=thickness, | |
fill=color) | |
# If the total height of the display strings added to the top of the bounding | |
# box exceeds the top of the image, stack the strings below the bounding box | |
# instead of above. | |
display_str_heights = [font.getsize(ds)[1] for ds in display_str_list] | |
# Each display_str has a top and bottom margin of 0.05x. | |
total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights) | |
if top > total_display_str_height: | |
text_bottom = top | |
else: | |
text_bottom = top + total_display_str_height | |
# Reverse list and print from bottom to top. | |
for display_str in display_str_list[::-1]: | |
text_width, text_height = font.getsize(display_str) | |
margin = np.ceil(0.05 * text_height) | |
draw.rectangle([(left, text_bottom - text_height - 2 * margin), | |
(left + text_width, text_bottom)], | |
fill=color) | |
draw.text((left + margin, text_bottom - text_height - margin), | |
display_str, | |
fill="black", | |
font=font) | |
text_bottom -= text_height - 2 * margin | |
def draw_boxes(image, boxes, class_names, scores, max_boxes=10, min_score=0.1): | |
"""Overlay labeled boxes on an image with formatted scores and label names.""" | |
colors = list(ImageColor.colormap.values()) | |
try: | |
font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf", | |
25) | |
except IOError: | |
print("Font not found, using default font.") | |
font = ImageFont.load_default() | |
for i in range(min(boxes.shape[0], max_boxes)): | |
if scores[i] >= min_score: | |
ymin, xmin, ymax, xmax = tuple(boxes[i]) | |
display_str = "{}: {}%".format(class_names[i], | |
int(100 * scores[i])) | |
color = colors[hash(class_names[i]) % len(colors)] | |
image_pil = Image.fromarray(np.uint8(image)).convert("RGB") | |
draw_bounding_box_on_image( | |
image_pil, | |
ymin, | |
xmin, | |
ymax, | |
xmax, | |
color, | |
font, | |
display_str_list=[display_str]) | |
np.copyto(image, np.array(image_pil)) | |
return image | |
image_with_boxes = draw_boxes( | |
image_np, result["detection_boxes"][0], | |
result["detection_classes"][0], result["detection_scores"][0]) | |
display_image(image_with_boxes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment