yolov5进行划区域检测,实现人流计数功能

      在进行过目标检测的训练后,我们如果想要实现划分区域检测,需要在detect.py文件里面添加一些代码来实现。

1.我们需要在#Run inference模块里修改:

在for path, img, im0s, vid_cap, s in dataset:这句代码的后面添加如下代码:大家可以设置不同点框出各种各样的区域形状。

 # Run inference
    model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz))  # warmup
    seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
    for path, img, im0s, vid_cap, s 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.(这步修改可以把设置的划分区域绘制出来)在# Process predictions模块中添加如下代码:

  # Process predictions
        for i, det in enumerate(pred):  # per image
            seen += 1
            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)

3.下面开始对识别到的目标进行计数,在第2步紧接着的p = Path(p) # to Path这行代码前面添加:

global person_count
person_count = 0

接着在if save_img or save_crop or view_img:  # Add bbox to image这行代码的后面添加:

                    if save_img or save_crop or view_img:  # Add bbox to image
                        c = int(cls)  # integer class
                        label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
                        annotator.box_label(xyxy, label, color=colors(c, True))
                        person_count += 1 #新添加的代码

接着下面的两个模块的代码的修改如代码显示,其中新增的部分添加的有注释。

  # Stream results
            im0 = annotator.result()
            if view_img:
                text = 'person_num:%d ' % (person_count)  # 新增代码
                cv2.putText(im0, text, (180, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)  # 新增代码
                cv2.namedWindow("Webcam", cv2.WINDOW_NORMAL)
                cv2.resizeWindow("Webcam", 1280, 720)
                cv2.moveWindow("Webcam", 0, 100)
                cv2.imshow("Webcam", im0)
                cv2.waitKey(1)

            # Save results (image with detections)
            if save_img:
                text = 'person_num:%d ' % (person_count)  # 新增代码
                cv2.putText(im0, text, (180, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)  # 新增代码
                if dataset.mode == 'image':
                    cv2.imwrite(save_path, im0)
                else:  # 'video' or 'stream'
                    if vid_path[i] != save_path:  # new video
                        vid_path[i] = save_path
                        if isinstance(vid_writer[i], cv2.VideoWriter):
                            vid_writer[i].release()  # release previous video writer
                        if vid_cap:  # video
                            fps = vid_cap.get(cv2.CAP_PROP_FPS)
                            w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
                            h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                        else:  # stream
                            fps, w, h = 30, im0.shape[1], im0.shape[0]
                        save_path = str(Path(save_path).with_suffix('.mp4'))  # force *.mp4 suffix on results videos
                        vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
                    vid_writer[i].write(im0)

        # Print time (inference-only)
        LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms")

我们还需要导入需要的包:

import numpy as np
import cv2


这样我们就完成了划分区域检测并实现目标计数。

4.效果展示:

原图展示:

划分区域检测效果图:

参考博客:YOLOv5如何进行区域目标检测(手把手教学)_yolov5指定区域检测-CSDN博客

  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在yolov5实现检测图片左上角计数功能,可以按照以下步骤进行操作: 1. 在yolov5中添加一个计数器变量,用于记录检测到的对象数量。 2. 在检测函数中,对每个检测到的对象进行处理,将计数器变量加1。 3. 在绘制检测结果时,将计数器变量的值添加到左上角的位置,以显示检测到的对象数量。 以下是一个示例代码: ``` import cv2 import torch from models.experimental import attempt_load from utils.general import non_max_suppression, scale_coords, plot_one_box from utils.datasets import letterbox counter = 0 def detect(img_path, model, device): global counter img = cv2.imread(img_path) # 读取图片 img0 = img.copy() # 备份原始图片 img = letterbox(img, new_shape=640)[0] # 调整图片大小 img = img[:, :, ::-1].transpose(2, 0, 1) # 转换颜色通道 img = np.ascontiguousarray(img) img = torch.from_numpy(img).to(device) # 转为tensor # 进行检测 pred = model(img, augment=False)[0] pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45) # 处理检测结果 for i, det in enumerate(pred): det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round() for *xyxy, conf, cls in reversed(det): label = f'{names[int(cls)]} {conf:.2f}' plot_one_box(xyxy, img0, label=label, color=colors[int(cls)], line_thickness=3) counter += 1 # 对象数量加1 # 在左上角显示对象数量 cv2.putText(img0, f'Detected: {counter}', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) return img0 # 加载模型 weights = 'yolov5s.pt' device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') model = attempt_load(weights, map_location=device) # 进行检测 img_path = 'test.jpg' result = detect(img_path, model, device) # 显示结果 cv2.imshow('result', result) cv2.waitKey(0) cv2.destroyAllWindows() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值