import matplotlib.pyplot as plt
import matplotlib.patches as patches
import cv2
# ----------- 使用opencv绘制Bounding box -----------
def plot_one_box(x, img, color=None, label=None, line_thickness=None): # Plots one bounding box on image
tl = line_thickness or round(0.002 * max(img.shape[0:2])) + 1 # line thickness
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
cv2.rectangle(img, c1, c2, color, thickness=tl)
if label:
tf = max(tl - 1, 1) # font thickness
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
cv2.rectangle(img, c1, c2, color, -1) # filled
cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
# -------------- 使用Matplotlib中提供的配色Cmap ------------
cmap = plt.get_cmap('tab20b')
color = cmap[int(np.where(unique_labels == int(cls_pred))[0])]
# -------------- 使用opencv绘制Bounding box ------------
plot_one_box([x1, y1, x2, y2], img, label=label, color=color)
# ----------- 使用matplotlib绘制Bounding box -----------
# Create a Rectangle patch
bbox = patches.Rectangle((x1, y1), box_w, box_h, linewidth=2,
edgecolor=color,
facecolor='none')
# Add the bbox to the plot
ax.add_patch(bbox)
# Add label
plt.text(x1, y1, s=classes[int(cls_pred)], color='white', verticalalignment='top',
bbox={'color': color, 'pad': 0})