手把手教你YOLOv5如何进行区域目标检测

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

手把手教你YOLOv5如何进行区域目标检测

wiz_k   2022-12-23 我要评论

提示:本项目的源码是基于yolov5 6.0版本修改

效果展示

在使用YOLOv5的有些时候,我们会遇到一些具体的目标检测要求,比如要求不检测全图,只在规定的区域内才检测。所以为了满足这个需求,可以用一个mask覆盖掉不想检测的区域,使得YOLOv5在检测的时候,该覆盖区域就不纳入检测范围。

话不多说,直接上检测效果,可以很直观的看到目标在进入规定的检测区域才得到检测。

一、确定检测范围

快捷查询方法:

  • 用windows自带画图打开图片
  • 鼠标移到想要框选检测区域的四个顶点,查询点的像素坐标
  • 分别计算点的像素坐标与图片总像素坐标的比例(后面要用)

查询方法如下图所示:

提示:以下是计算的举例说明,仅供参考

例如:图中所标注的点(1010,174)代表(x,y)坐标

hl1 = 174 / 768 (可约分)监测区域纵坐标距离图片***顶部*** 比例

wl1 = 1010 / 1614 (可约分)监测区域横坐标距离图片***左部*** 比例

这里只举例了一个点,如检测范围是四边形,需要计算左上,右上,右下,左下四个顶点。

二、detect.py代码修改

1.确定区域检测范围

在下面代码位置填上计算好的比例:

 # mask for certain region
        #1,2,3,4 分别对应左上,右上,右下,左下四个点
        hl1 = 1.4 / 10 #监测区域高度距离图片顶部比例
        wl1 = 6.4 / 10 #监测区域高度距离图片左部比例
        hl2 = 1.4 / 10  # 监测区域高度距离图片顶部比例
        wl2 = 6.8 / 10  # 监测区域高度距离图片左部比例
        hl3 = 4.5 / 10  # 监测区域高度距离图片顶部比例
        wl3 = 7.6 / 10  # 监测区域高度距离图片左部比例
        hl4 = 4.5 / 10  # 监测区域高度距离图片顶部比例
        wl4 = 5.5 / 10  # 监测区域高度距离图片左部比例

在135行:for path, img, im0s, vid_cap in dataset: 下插入代码:

        # mask for certain region
        #1,2,3,4 分别对应左上,右上,右下,左下四个点
        hl1 = 1.6 / 10 #监测区域高度距离图片顶部比例
        wl1 = 6.4 / 10 #监测区域高度距离图片左部比例
        hl2 = 1.6 / 10  # 监测区域高度距离图片顶部比例
        wl2 = 6.8 / 10  # 监测区域高度距离图片左部比例
        hl3 = 4.5 / 10  # 监测区域高度距离图片顶部比例
        wl3 = 7.6 / 10  # 监测区域高度距离图片左部比例
        hl4 = 4.5 / 10  # 监测区域高度距离图片顶部比例
        wl4 = 5.5 / 10  # 监测区域高度距离图片左部比例
        if webcam:
            for b in range(0,img.shape[0]):
                mask = np.zeros([img[b].shape[1], img[b].shape[2]], dtype=np.uint8)
                #mask[round(img[b].shape[1] * hl1):img[b].shape[1], round(img[b].shape[2] * wl1):img[b].shape[2]] = 255
                pts = np.array([[int(img[b].shape[2] * wl1), int(img[b].shape[1] * hl1)],  # pts1
                                [int(img[b].shape[2] * wl2), int(img[b].shape[1] * hl2)],  # pts2
                                [int(img[b].shape[2] * wl3), int(img[b].shape[1] * hl3)],  # pts3
                                [int(img[b].shape[2] * wl4), int(img[b].shape[1] * hl4)]], np.int32)
                mask = cv2.fillPoly(mask,[pts],(255,255,255))
                imgc = img[b].transpose((1, 2, 0))
                imgc = cv2.add(imgc, np.zeros(np.shape(imgc), dtype=np.uint8), mask=mask)
                #cv2.imshow('1',imgc)
                img[b] = imgc.transpose((2, 0, 1))

        else:
            mask = np.zeros([img.shape[1], img.shape[2]], dtype=np.uint8)
            #mask[round(img.shape[1] * hl1):img.shape[1], round(img.shape[2] * wl1):img.shape[2]] = 255
            pts = np.array([[int(img.shape[2] * wl1), int(img.shape[1] * hl1)],  # pts1
                            [int(img.shape[2] * wl2), int(img.shape[1] * hl2)],  # pts2
                            [int(img.shape[2] * wl3), int(img.shape[1] * hl3)],  # pts3
                            [int(img.shape[2] * wl4), int(img.shape[1] * hl4)]], np.int32)
            mask = cv2.fillPoly(mask, [pts], (255,255,255))
            img = img.transpose((1, 2, 0))
            img = cv2.add(img, np.zeros(np.shape(img), dtype=np.uint8), mask=mask)
            img = img.transpose((2, 0, 1))

2.画检测区域线(若不想像效果图一样显示出检测区域可不添加)

在196行: p, s, im0, frame = path, ‘’, im0s.copy(), getattr(dataset, ‘frame’, 0) 后添加

            if webcam:  # batch_size >= 1
                p, s, im0, frame = path[i], f'{i}: ', im0s[i].copy(), dataset.count
                cv2.putText(im0, "Detection_Region", (int(im0.shape[1] * wl1 - 5), int(im0.shape[0] * hl1 - 5)),
                            cv2.FONT_HERSHEY_SIMPLEX,
                            1.0, (255, 255, 0), 2, cv2.LINE_AA)

                pts = np.array([[int(im0.shape[1] * wl1), int(im0.shape[0] * hl1)],  # pts1
                                [int(im0.shape[1] * wl2), int(im0.shape[0] * hl2)],  # pts2
                                [int(im0.shape[1] * wl3), int(im0.shape[0] * hl3)],  # pts3
                                [int(im0.shape[1] * wl4), int(im0.shape[0] * hl4)]], np.int32)  # pts4
                # pts = pts.reshape((-1, 1, 2))
                zeros = np.zeros((im0.shape), dtype=np.uint8)
                mask = cv2.fillPoly(zeros, [pts], color=(0, 165, 255))
                im0 = cv2.addWeighted(im0, 1, mask, 0.2, 0)
                cv2.polylines(im0, [pts], True, (255, 255, 0), 3)
                # plot_one_box(dr, im0, label='Detection_Region', color=(0, 255, 0), line_thickness=2)
            else:
                p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0)
                cv2.putText(im0, "Detection_Region", (int(im0.shape[1] * wl1 - 5), int(im0.shape[0] * hl1 - 5)),
                            cv2.FONT_HERSHEY_SIMPLEX,
                            1.0, (255, 255, 0), 2, cv2.LINE_AA)
                pts = np.array([[int(im0.shape[1] * wl1), int(im0.shape[0] * hl1)],  # pts1
                                [int(im0.shape[1] * wl2), int(im0.shape[0] * hl2)],  # pts2
                                [int(im0.shape[1] * wl3), int(im0.shape[0] * hl3)],  # pts3
                                [int(im0.shape[1] * wl4), int(im0.shape[0] * hl4)]], np.int32)  # pts4
                # pts = pts.reshape((-1, 1, 2))
                zeros = np.zeros((im0.shape), dtype=np.uint8)
                mask = cv2.fillPoly(zeros, [pts], color=(0, 165, 255))
                im0 = cv2.addWeighted(im0, 1, mask, 0.2, 0)

                cv2.polylines(im0, [pts], True, (255, 255, 0), 3)

总结

基于yolov5的区域目标检测实质上就是在图片选定检测区域做一个遮掩mask,检测区域不一定为四边形,也可是其他形状。该方法可检测图片/视频/摄像头。

提示:使用该方法要先确定数据集图像是否像监控图像一样,画面主体保持不变

原理展现如图所示:(图片参考http://t.csdn.cn/lgyO1

整体detect.py修改代码

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们