使用Ultralytics YOLOv8 计数物体 🚀
什么是对象计数?
使用 Ultralytics YOLOv8YOLOv8 在实时应用中表现出色,凭借其先进的算法和深度学习能力,可为人群分析和监控等各种场景提供高效、精确的物体计数。
观看: 使用对象计数Ultralytics YOLOv8
物体计数的优势?
- 资源优化:对象计数通过提供准确的计数和优化库存管理等应用中的资源分配,促进了高效的资源管理。
- 增强安全性:物体计数可准确跟踪和计数实体,有助于主动探测威胁,从而增强安全性和监控能力。
- 知情决策:物体计数为决策、优化零售、交通管理和其他各种领域的流程提供了宝贵的见解。
真实世界的应用
物流 | 水产养殖 |
---|---|
使用传送带包装计数Ultralytics YOLOv8 | 利用海洋数鱼Ultralytics YOLOv8 |
使用YOLOv8 进行对象计数示例
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2
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 = object_counter.ObjectCounter()
counter.set_args(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()
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2
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 = object_counter.ObjectCounter()
counter.set_args(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()
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2
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 = object_counter.ObjectCounter()
counter.set_args(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()
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2
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 = object_counter.ObjectCounter()
counter.set_args(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()
区域可移动
点击区域边缘,可将其移动到框架内的任何位置
可选参数 set_args
名称 | 类型 | 默认值 | 说明 |
---|---|---|---|
view_img |
bool |
False |
显示带有计数的帧 |
view_in_counts |
bool |
True |
仅在视频帧上显示内计数 |
view_out_counts |
bool |
True |
仅在视频帧上显示外数 |
line_thickness |
int |
2 |
增加边界框并计算文本厚度 |
reg_pts |
list |
[(20, 400), (1260, 400)] |
定义区域面积的点 |
classes_names |
dict |
model.model.names |
类名词典 |
count_reg_color |
RGB Color |
(255, 0, 255) |
计数区域或线条对象的颜色 |
track_thickness |
int |
2 |
跟踪线的厚度 |
draw_tracks |
bool |
False |
启用绘制轨迹线 |
track_color |
RGB Color |
(0, 255, 0) |
每条轨道线的颜色 |
line_dist_thresh |
int |
15 |
直线计数器的欧氏距离阈值 |
count_txt_color |
RGB Color |
(255, 255, 255) |
对象计数文本的前景色 |
region_thickness |
int |
5 |
对象计数器区域或线条的厚度 |
count_bg_color |
RGB Color |
(255, 255, 255) |
计数荧光笔颜色 |
论据 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 |
显示物体跟踪结果 |