Skip to content

Instantly share code, notes, and snippets.

@ctodd
Created July 9, 2020 04:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctodd/5c9ec80038de100d06490c2f6b6b1b0b to your computer and use it in GitHub Desktop.
Save ctodd/5c9ec80038de100d06490c2f6b6b1b0b to your computer and use it in GitHub Desktop.
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