Created
July 9, 2020 00:14
-
-
Save hamletbatista/62bbd09a2a0eea84afa0bc99020c79fb to your computer and use it in GitHub Desktop.
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
import cv2 | |
def draw_bounding_boxes(file_path, prediction_result): | |
## read image file from disk | |
img = cv2.imread(file_path, cv2.IMREAD_COLOR) | |
height = img.shape[0] # Image height | |
width = img.shape[1] # Image width | |
color = (255,0,0) | |
for result in prediction_result.payload: | |
score = result.image_object_detection.score * 100 | |
#exclude low confidence predictions | |
if score > 80: | |
text = f"{result.display_name} {score:.2f}%" | |
# https://cloud.google.com/vision/automl/object-detection/docs/reference/rpc/google.cloud.automl.v1beta1 | |
x1 = result.image_object_detection.bounding_box.normalized_vertices[0].x | |
# denormalize | |
x1 = int(x1 * width ) | |
y1 = result.image_object_detection.bounding_box.normalized_vertices[0].y | |
# denormalize | |
y1 = int(y1 * height ) | |
x2 = result.image_object_detection.bounding_box.normalized_vertices[1].x | |
# denormalize | |
x2 = int(x2 * width ) | |
y2 = result.image_object_detection.bounding_box.normalized_vertices[1].y | |
# denormalize | |
y2 = int(y2 * height ) | |
print(f"Predicted label and score: {text}") | |
print(f"Predicted x1, y1: {x1, y1}") | |
print(f"Predicted x2, y2: {x2, y2}") | |
cv2.rectangle(img, (x1,y1), (x2, y2), color, 2) | |
# Add label with score | |
cv2.putText(img, text, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, 255) | |
return img | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment