直接上代码
def draw_rect(img, class_id, left, top, right, bottom):
cv2.rectangle(img, (left, bottom), (right, top), (0, 255, 0), thickness=2)
labelSize, baseLine = cv2.getTextSize(class_id, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
print(labelSize, baseLine)
top = max(top, labelSize[1])
cv2.rectangle(img, (left, bottom-labelSize[1]), (left+labelSize[0], bottom), (0, 255, 0), thickness=-1)
cv2.putText(img, class_id, (left, bottom), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
return img
效果图: