Skip to content

Instantly share code, notes, and snippets.

@MLWhiz
Created June 27, 2020 18:08
Show Gist options
  • Select an option

  • Save MLWhiz/06d89469b998e92a2bb7e7a827335c2f to your computer and use it in GitHub Desktop.

Select an option

Save MLWhiz/06d89469b998e92a2bb7e7a827335c2f to your computer and use it in GitHub Desktop.
from PIL import Image
import numpy as np
import cv2
import matplotlib.pyplot as plt
def PILImage_to_cv2(img):
return np.asarray(img)
def drawboundingbox(img, boxes,pred_cls, rect_th=2, text_size=1, text_th=2):
img = PILImage_to_cv2(img)
class_color_dict = {}
#initialize some random colors for each class for better looking bounding boxes
for cat in pred_cls:
class_color_dict[cat] = [random.randint(0, 255) for _ in range(3)]
for i in range(len(boxes)):
cv2.rectangle(img, (int(boxes[i][0][0]), int(boxes[i][0][1])),
(int(boxes[i][1][0]),int(boxes[i][1][1])),
color=class_color_dict[pred_cls[i]], thickness=rect_th)
cv2.putText(img,pred_cls[i], (int(boxes[i][0][0]), int(boxes[i][0][1])), cv2.FONT_HERSHEY_SIMPLEX, text_size, class_color_dict[pred_cls[i]],thickness=text_th) # Write the prediction class
plt.figure(figsize=(20,30))
plt.imshow(img)
plt.xticks([])
plt.yticks([])
plt.show()
img = Image.open("sample_images/dog_with_ball.jpg")
drawboundingbox(img, data_dict['boxes'], data_dict['classes'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment