使用对象计数Ultralytics YOLOv8
什么是对象计数?
使用 Ultralytics YOLOv8YOLOv8 在实时应用中表现出色,凭借其先进的算法和深度学习能力,可为人群分析和监控等各种场景提供高效、精确的物体计数。
观看: 使用对象计数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,
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,
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,
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,
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
争论:
名称 | 类型 | 默认值 | 说明 |
---|---|---|---|
names |
dict |
None |
类名词典 |
reg_pts |
list |
[(20, 400), (1260, 400)] |
定义计数区域的点列表。 |
line_thickness |
int |
2 |
边界框的线条粗细 |
view_img |
bool |
False |
用于控制是否显示视频流的标志。 |
view_in_counts |
bool |
True |
用于控制是否在视频流中显示输入计数的标志。 |
view_out_counts |
bool |
True |
用于控制是否在视频流中显示输出计数的标志。 |
draw_tracks |
bool |
False |
标志,用于控制是否绘制对象轨迹。 |
论据 model.track
论据 | 类型 | 默认值 | 说明 |
---|---|---|---|
source |
str |
None |
Specifies the source directory for images or videos. Supports file paths and URLs. |
persist |
bool |
False |
Enables persistent tracking of objects between frames, maintaining IDs across video sequences. |
tracker |
str |
botsort.yaml |
Specifies the tracking algorithm to use, e.g., bytetrack.yaml 或 botsort.yaml . |
conf |
float |
0.3 |
Sets the confidence threshold for detections; lower values allow more objects to be tracked but may include false positives. |
iou |
float |
0.5 |
Sets the Intersection over Union (IoU) threshold for filtering overlapping detections. |
classes |
list |
None |
Filters results by class index. For example, classes=[0, 2, 3] only tracks the specified classes. |
verbose |
bool |
True |
Controls the display of tracking results, providing a visual output of tracked objects. |
常见问题
如何使用Ultralytics YOLOv8 计数视频中的物体?
要使用Ultralytics YOLOv8 计数视频中的物体,可以按照以下步骤进行:
- 导入必要的程序库 (
cv2
,ultralytics
). - 加载预训练的YOLOv8 模型。
- 定义计算区域(如多边形、直线等)。
- 设置视频捕捉并初始化对象计数器。
- 处理每个帧,以跟踪定义区域内的对象并对其进行计数。
下面是一个简单的区域计数示例:
import cv2
from ultralytics import YOLO, solutions
def count_objects_in_region(video_path, output_video_path, model_path):
"""Count objects in a specific region within a video."""
model = YOLO(model_path)
cap = cv2.VideoCapture(video_path)
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))
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]
video_writer = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
counter = solutions.ObjectCounter(
view_img=True, reg_pts=region_points, 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()
count_objects_in_region("path/to/video.mp4", "output_video.avi", "yolov8n.pt")
在 "对象计数"部分探索更多配置和选项。
使用Ultralytics YOLOv8 进行物体计数有哪些优势?
使用Ultralytics YOLOv8 进行物体计数有几个优点:
- 资源优化:它通过提供精确的计数来促进有效的资源管理,帮助优化库存管理等行业的资源分配。
- 增强安全性:它通过准确跟踪和计算实体数量,帮助主动探测威胁,从而加强安全和监控。
- 知情决策:它为零售、交通管理等领域的决策和流程优化提供了宝贵的见解。
有关实际应用和代码示例,请访问对象计数的优势部分。
如何使用Ultralytics YOLOv8 对特定类别的对象进行计数?
要使用Ultralytics YOLOv8 统计特定类别的对象,您需要在跟踪阶段指定您感兴趣的类别。下面是Python 示例:
import cv2
from ultralytics import YOLO, solutions
def count_specific_classes(video_path, output_video_path, model_path, classes_to_count):
"""Count specific classes of objects in a video."""
model = YOLO(model_path)
cap = cv2.VideoCapture(video_path)
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)]
video_writer = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
counter = solutions.ObjectCounter(
view_img=True, reg_pts=line_points, 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()
count_specific_classes("path/to/video.mp4", "output_specific_classes.avi", "yolov8n.pt", [0, 2])
在这个例子中 classes_to_count=[0, 2]
,这意味着它会计算 0
和 2
(如人和车)。
在实时应用中,为什么要使用YOLOv8 而不是其他物体检测模型?
Ultralytics YOLOv8 与其他物体检测模型(如 Faster R-CNN、SSD 和以前的YOLO 版本)相比,它具有多项优势:
- 速度和效率: YOLOv8 具有实时处理能力,非常适合需要高速推理的应用,如监控和自动驾驶。
- 准确性:它为物体检测和跟踪任务提供了最先进的准确性,减少了误报的数量,提高了整个系统的可靠性。
- 易于集成: YOLOv8 可与各种平台和设备(包括移动设备和边缘设备)无缝集成,这对现代人工智能应用至关重要。
- 灵活性:支持对象检测、分割和跟踪等各种任务,可配置模型以满足特定用例的要求。
请查阅Ultralytics YOLOv8 文档,深入了解其功能和性能比较。
我能否将YOLOv8 用于人群分析和交通管理等高级应用?
Ultralytics YOLOv8 具有实时检测能力、可扩展性和集成灵活性,非常适合人群分析和交通管理等高级应用。其先进的功能可在动态环境中实现高精度的目标跟踪、计数和分类。使用实例包括
- 人群分析:监控和管理大型集会,确保安全并优化人流。
- 交通管理:跟踪和统计车辆,分析交通模式,实时管理交通拥堵。
有关详细信息和实施细节,请参阅YOLOv8 对象计数的实际应用指南。
📅创建于 9 个月前
✏️2 天前已更新