前言
最近一直在研究人工智能,包括处理一些图片或者视频的视觉目标检测,接触了 YOLO ,索性就好好研究一下,在这里记录一下。
介绍
大模型YOLO(You Only Look Once)是一种先进的深度学习目标检测算法,由Joseph Redmon等人在2015年首次提出。YOLO主要用于目标检测领域(机器视觉子领域),通过单次查看即可完成对图像中物体的识别和定位,具有速度快、准确率高、可解释性强和适用性广等优点,是当前目标检测领域最重要的代表之一。
特点
速度快:YOLO是一种端到端的算法,可以在一次前向传递中同时检测多个对象,因此速度非常快,适用于实时应用。在GPU上运行时,YOLO可以实现较高的检测速度。
准确率高:YOLO使用整个图像进行预测,能够捕获全局上下文信息,从而提高检测准确率。随着版本的迭代,YOLO的检测精度也在不断提升。
可解释性强:YOLO使用单个神经网络进行预测,可以直接输出边界框的坐标和类别概率,易于理解和解释。
适用性广:YOLO可以应用于各种不同的目标检测任务,包括人体姿态估计、车辆检测、行人检测等。
原理
YOLO原理非常简单。我们将目标检测重新定义为一个单一的检测问题,从图像像素直接到边界框坐标和类别概率。我们用一个卷积神经网络同时预测多个边界框和这些框类别概率。模型在完整的图像上训练,并直接优化检测性能。这个统一的模型相比较于传统检测模型有很多优点。YOLO设计可实现端到端训练和实时的速度,同时保持较高的平均精度。
安装
1
pip install ultralytics
实例
实例代码通过调用电脑摄像头,分析摄像头里面是否有人,如果有则保存视频。
实例使用本机摄像头,如果使用网络摄像头,请使用rtspURL格式,后面有介绍。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import cv2
import time
from ultralytics import YOLO
# 加载模型
model = YOLO('yolo11m.pt')
# 摄像头
# video_path = '64_1744337217.mp4'
cap = cv2.VideoCapture(0)
# 视频参数
fps = cap.get(cv2.CAP_PROP_FPS)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 状态变量
is_recording = False # 是否正在录制
video_writer = None # 视频写入对象
last_person_time = 0 # 最后检测到人的时间
person_in_frame = False # 当前帧是否有人
# 视频保存参数
save_duration_after_exit = 1 # 人消失后继续录制的时间(秒)
output_codec = 'mp4v' # 视频编码格式
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 实时执行检测
results = model(frame)
current_person = False
# 分析检测结果
for result in results:
class_ids = result.boxes.cls.cpu().numpy().astype(int)
# 检查是否有人(COCO类别ID 0)
if 0 in class_ids:
current_person = True
break
# 状态变化检测
if current_person:
last_person_time = time.time()
if not person_in_frame:
print("检测到人进入画面,开始录制...")
person_in_frame = True
else:
if person_in_frame and (time.time() - last_person_time > save_duration_after_exit):
print("人已离开,结束录制")
person_in_frame = False
# 视频录制控制
if person_in_frame:
if not is_recording:
# 初始化视频写入器
timestamp = time.strftime("%Y%m%d-%H%M%S")
output_file = f'person_detection_{timestamp}.mp4'
fourcc = cv2.VideoWriter_fourcc(*output_codec)
video_writer = cv2.VideoWriter(output_file, fourcc, fps, (frame_width, frame_height))
is_recording = True
# 写入当前帧(带检测结果)
for box in result.boxes.xyxy.cpu().numpy():
x1, y1, x2, y2 = map(int, box)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
video_writer.write(frame)
else:
if is_recording:
video_writer.release()
is_recording = False
print(f"视频已保存至:{output_file}")
# 实时显示
cv2.imshow('Person Detection Recorder', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
if is_recording:
video_writer.release()
cap.release()
cv2.destroyAllWindows()
rtspURL格式
rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream
username 用户名,常用 admin
password 密码,常用 12345
ip 摄像头IP,如 192.0.0.64
port 端口号,默认为 554
codec 视频编码模式,有 h264、MPEG-4、mpeg4 等
channel 通道号,起始为1,例如通道1,则为 ch1
subtype 码流类型,主码流为 main,辅码流为 sub