Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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