Created
July 9, 2020 04:49
-
-
Save ctodd/5c9ec80038de100d06490c2f6b6b1b0b 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 matplotlib.pyplot as plt | |
import matplotlib.patches as patches | |
from PIL import Image | |
import numpy as np | |
from itertools import cycle | |
def show_annotated_image(img_path, bboxes, prec): | |
im = np.array(Image.open(img_path), dtype=np.uint8) | |
# Create figure and axes | |
fig,ax = plt.subplots(1) | |
# Display the image | |
ax.imshow(im) | |
colors = cycle(['r', 'o', 'b', 'g', 'c', 'm', 'k', 'w']) | |
right = .1 | |
bottom = .9 | |
for bbox in bboxes: | |
if bbox['class_id'] == 0: | |
class_id = 'pooping' | |
text_color = 'red' | |
elif bbox['class_id'] == 1: | |
class_id = 'not_pooping' | |
text_color = 'orange' | |
# Create a Rectangle patch | |
rect = patches.Rectangle((bbox['left'],bbox['top']),bbox['width'],bbox['height'],linewidth=1,edgecolor=text_color,facecolor='none') | |
# Add the patch to the Axes | |
ax.add_patch(rect) | |
if float(prec) == 1: | |
ax.text(right, bottom, class_id, | |
horizontalalignment='left', | |
verticalalignment='top', | |
color=text_color, | |
backgroundcolor='white', | |
transform=ax.transAxes) | |
elif float(prec) < 1: | |
ax.text(right, bottom, class_id + '\n' + prec + '%', | |
horizontalalignment='left', | |
verticalalignment='top', | |
color=text_color, | |
backgroundcolor='white', | |
transform=ax.transAxes) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment