yolov5 detect 显示出单个目标计数

1 在 detect.py 中 按 Ctrl+f 查找:

cv2.imshow(str(p), im0)

在这一句上面添加方法 cv2.putText 用于在识别图片画面中输出文本:

cv2.putText(im0, f"{n} {names[int(c)]}{'s' * (n > 1)}", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 2.2, (225, 105, 65), 4)  # 

注意,这里的color为BGR顺序,如:(0,0,255)为红色。

cv2.putText 为 Opencv 函数,用法解析如下:

putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None)

参数解释:

img: 图片
text: 要显示的文本字符串
org: 字符串框左上角点的位置
fontFace: 字体类型
fontScale: 字体大小
color: 字体颜色
thickness: 字体粗细

官方函数定义说明:

def putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None): # real signature unknown; restored from __doc__
    """
    putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
    .   @brief Draws a text string.
    .   
    .   The function cv::putText renders the specified text string in the image. Symbols that cannot be rendered
    .   using the specified font are replaced by question marks. See #getTextSize for a text rendering code
    .   example.
    .   
    .   @param img Image.
    .   @param text Text string to be drawn.
    .   @param org Bottom-left corner of the text string in the image.
    .   @param fontFace Font type, see #HersheyFonts.
    .   @param fontScale Font scale factor that is multiplied by the font-specific base size.
    .   @param color Text color.
    .   @param thickness Thickness of the lines used to draw a text.
    .   @param lineType Line type. See #LineTypes
    .   @param bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise,
    .   it is at the top-left corner.
    """
    pass

2 修改识别类为单个识别类:

找到下面获取类别参数的语句:

 parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')

补充: default=‘0’, 即:

 parser.add_argument('--classes', nargs='+', type=int, default='0', help='filter by class: --classes 0, or --classes 0 2 3')

可以进行debug测试是否可以输出计算识别类总数的字符串,如果不性,可以查看参数
view_imgwebcam是否为false,若是,则将下述代码中:

            if  view_img:
                cv2.putText(im0, f"{n} {names[int(c)]}{'s' * (n > 1)}", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 2.2, (225, 105, 65), 4)  # color: BGR
                cv2.imshow(str(p), im0)
                cv2.waitKey(1)  # 1 millisecond

if view_img: 修改为 if not view_img:
则最终是:

            if not view_img:
                # the line followed was added by daaize
                cv2.putText(im0, f"{n} {names[int(c)]}{'s' * (n > 1)}", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 2.2, (225, 105, 65), 4)  # color: BGR
                cv2.imshow(str(p), im0)
                cv2.waitKey(1)  # 1 millisecond

跑detect.py,结果如下:
yolov5 detect 显示出单个目标计数_第1张图片参考文章:
YOLOv5在图片上显示统计出的单一检测目标的个数_未来还要多远
在yolov5预测图片上显示预测结果_SSD-VGG博主

你可能感兴趣的:(opencv,python,计算机视觉)