git:https://github.com/ultralytics/ultralytics/blob/main/examples/YOLOv8-Region-Counter/readme.md
很长时间没有做yolov的项目了,最近一看yolov8有一个区域计数的功能,不得不说很实用啊。
b站:https://www.bilibili.com/video/BV1Ng4y1d7Yr/?spm_id_from=333.999.0.0

1. 开干

照着 git整就行;
pipe install ultralytics
1.安装库

# Clone ultralytics repo
git clone https://github.com/ultralytics/ultralytics

# cd to local directory
cd ultralytics/examples/YOLOv8-Region-Counter
  1. Run the Region Counting Using Ultralytics Yolov8
# If you want to save results
python yolov8_region_counter.py --source "path/to/video.mp4" --save-img --view-img

# If you want to run model on CPU
python yolov8_region_counter.py --source "path/to/video.mp4" --save-img --view-img --device cpu

# If you want to change model file
python yolov8_region_counter.py --source "path/to/video.mp4" --save-img --weights "path/to/model.pt"

# If you dont want to save results
python yolov8_region_counter.py --source "path/to/video.mp4" --view-img

Usage Options

--source: Specifies the path to the video file you want to run inference on.
--device: Specifies the device cpu or 0
--save-img: Flag to save the detection results as images.
--weights: Specifies a different YOLOv8 model file (e.g., yolov8n.pt, yolov8s.pt, yolov8m.pt, yolov8l.pt, yolov8x.pt).
--line-thickness: Specifies the bounding box thickness
--region-thickness: Specifies the region boxes thickness
--track-thickness: Specifies the track line thickness
counting_regions = [
    {
        "name": "YOLOv8 Polygon Region",
        "polygon": Polygon(
            [(50, 80), (250, 20), (450, 80), (400, 350), (100, 350)]
        ),  # Polygon with five points (Pentagon)
        "counts": 0,
        "dragging": False,
        "region_color": (255, 42, 4),  # BGR Value
        "text_color": (255, 255, 255),  # Region Text Color
    },
    {
        "name": "YOLOv8 Rectangle Region",
        "polygon": Polygon(
            [(200, 250), (440, 250), (440, 550), (200, 550)]
        ),  # Rectangle with four points
        "counts": 0,
        "dragging": False,
        "region_color": (37, 255, 225),  # BGR Value
        "text_color": (0, 0, 0),  # Region Text Color
    },
]
  1. 业务化改造,因为原仓库代码有问题
  2. python yolov8_region_counter.py --weights yolov8n.pt --device 0 --source D:\code\python_project\ultralytics\examples\YOLOv8-Region-Counter\test_person.mp4 --view-img --track-thickness 5
    –track-thickness参数 就是那几根线,我不清楚这几根线有啥意思。
    代码运行:

问题:1 有追踪的线
在这里插入图片描述
把线去掉
屏蔽这行代码,156行

cv2.polylines(frame, [points], isClosed=False, color=bbox_color, thickness=track_thickness)

在这里插入图片描述
问题2:生成的结果mp4 无法播放
不是bug,是我的运行命令没用对。
python yolov8_region_counter.py --weights yolov8n.pt --device 0 --source test_person.mp
4 --view-img --save-img

改进

可以自定义框的位置,
在这里插入图片描述

所使用命令:

python yolov8_region_counter.py --weights yolov8n.pt --device 0 --source test_person.mp
4 --view-img --save-img

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐