跳至内容

使用Ultralytics YOLOv8 计数物体 🚀

什么是对象计数?

使用 Ultralytics YOLOv8YOLOv8 在实时应用中表现出色,凭借其先进的算法和深度学习能力,可为人群分析和监控等各种场景提供高效、精确的物体计数。


观看: 使用对象计数Ultralytics YOLOv8

观看: Class-wise Object Counting using Ultralytics YOLOv8

物体计数的优势?

  • 资源优化:对象计数通过提供准确的计数和优化库存管理等应用中的资源分配,促进了高效的资源管理。
  • 增强安全性:物体计数可准确跟踪和计数实体,有助于主动探测威胁,从而增强安全性和监控能力。
  • 知情决策:物体计数为决策、优化零售、交通管理和其他各种领域的流程提供了宝贵的见解。

真实世界的应用

物流 水产养殖
使用传送带包装计数Ultralytics YOLOv8 利用海洋数鱼Ultralytics YOLOv8
使用传送带包装计数Ultralytics YOLOv8 利用海洋数鱼Ultralytics YOLOv8

使用YOLOv8 进行对象计数示例

import cv2

from ultralytics import YOLO, solutions

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Define region points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]

# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

# Init Object Counter
counter = solutions.ObjectCounter(
    view_img=True,
    reg_pts=region_points,
    classes_names=model.names,
    draw_tracks=True,
    line_thickness=2,
)

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False)

    im0 = counter.start_counting(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
import cv2

from ultralytics import YOLO, solutions

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Define region points as a polygon with 5 points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360), (20, 400)]

# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

# Init Object Counter
counter = solutions.ObjectCounter(
    view_img=True,
    reg_pts=region_points,
    classes_names=model.names,
    draw_tracks=True,
    line_thickness=2,
)

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False)

    im0 = counter.start_counting(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
import cv2

from ultralytics import YOLO, solutions

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Define line points
line_points = [(20, 400), (1080, 400)]

# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

# Init Object Counter
counter = solutions.ObjectCounter(
    view_img=True,
    reg_pts=line_points,
    classes_names=model.names,
    draw_tracks=True,
    line_thickness=2,
)

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False)

    im0 = counter.start_counting(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
import cv2

from ultralytics import YOLO, solutions

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

line_points = [(20, 400), (1080, 400)]  # line or region points
classes_to_count = [0, 2]  # person and car classes for count

# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

# Init Object Counter
counter = solutions.ObjectCounter(
    view_img=True,
    reg_pts=line_points,
    classes_names=model.names,
    draw_tracks=True,
    line_thickness=2,
)

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False, classes=classes_to_count)

    im0 = counter.start_counting(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
区域可移动

点击区域边缘,可将其移动到框架内的任何位置

论据 ObjectCounter

下面的表格显示了 ObjectCounter 争论:

名称 类型 默认值 说明
classes_names dict None 类名词典
reg_pts list [(20, 400), (1260, 400)] 定义计数区域的点列表。
count_reg_color tuple (255, 0, 255) 计数区域的 RGB 颜色。
count_txt_color tuple (0, 0, 0) 计数文本的 RGB 颜色。
count_bg_color tuple (255, 255, 255) 计数文本背景的 RGB 颜色。
line_thickness int 2 边界框的线条粗细
track_thickness int 2 轨道线的厚度。
view_img bool False 用于控制是否显示视频流的标志。
view_in_counts bool True 用于控制是否在视频流中显示输入计数的标志。
view_out_counts bool True 用于控制是否在视频流中显示输出计数的标志。
draw_tracks bool False 标志,用于控制是否绘制对象轨迹。
track_color tuple None 轨道的 RGB 颜色。
region_thickness int 5 对象计数区域的厚度。
line_dist_thresh int 15 行计数器的欧氏距离阈值。
cls_txtdisplay_gap int 50 显示每个班级计数之间的差距。

论据 model.track

名称 类型 默认值 说明
source im0 None 图像或视频的源目录
persist bool False 帧与帧之间的持久轨迹
tracker str botsort.yaml 跟踪方法 "bytetrack "或 "botsort
conf float 0.3 置信度阈值
iou float 0.5 借据阈值
classes list None 按类别筛选结果,即 classes=0,或 classes=[0,2,3]
verbose bool True 显示物体跟踪结果


Created 2023-12-02, Updated 2024-06-10
Authors: glenn-jocher (14), IvorZhu331 (1), RizwanMunawar (6), AyushExel (1)

评论