【脚本】绘制矩形框,文字(用cv2)【草稿暂存】

参考链接

  • getTextSize()和putText()

截取的yolov5中绘制矩形框的代码

p1, p2 =(int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
# 绘制矩形box
cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)

# 绘制box上面的文字
if label:
    tf = max(self.lw - 1, 1)  # font thickness
    w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0]  # text width, height
    outside = p1[1] - h >= 3
    p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
    cv2.rectangle(self.im, p1, p2, color, -1, cv2.LINE_AA)  # filled
    cv2.putText(self.im,
                label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2),
                0,
                self.lw / 3,
                txt_color,
                thickness=tf,
                lineType=cv2.LINE_AA)

展示效果

【脚本】绘制矩形框,文字(用cv2)【草稿暂存】_第1张图片

你可能感兴趣的:(脚本,python,opencv,tensorflow)