def write(x, results):
"""
把num_batch个预测结果,画在对应的图像上。
:param x: tensor. shape is 8. [img_index, x1, y1, x2, y2, object_score, cls_score, cls)
:param results: list. 对应的原图
:return:
"""
c1 = tuple(x[1:3].int()) # x1, y1
c2 = tuple(x[3:5].int()) # x2, y2
img = results[int(x[0])] # 根据x中的图片索引,从results找到对应的原图。
cls = int(x[-1])
color = random.choice(colors) # list. each item of the colors is a tuple.
label = "{0}".format(classes[cls]) # classes is a list. The index of the list is cls
cv2.rectangle(img, c1, c2, color, 1) # c1和c2分别是目标的左上角和右下角坐标
t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1, 1)[0] # The size of a box. xy
c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
cv2.rectangle(img, c1, c2, color, -1) # c1和c2分别是文本框的左上角和右下角坐标, 里面随机填充一种颜色
# Bottom-left corner of the text string in the image.
cv2.putText(img, label, (c1[0], c1[1] + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1, [225, 255, 255], 1)
return img
效果图: