点击下方“深度学习爱好者”,选择加"星标"或“置顶”
OpenCV:用于视频处理和显示结果。 Ultralytics YOLO:一个流行且高效的深度学习模型,用于目标检测。
pip install opencv-python
pip install ultralytics
加载YOLO模型:我们加载一个预训练的YOLO模型(best.pt)用于眼睛检测。这个模型被训练来识别两种状态:“闭合”和“打开”的眼睛。下载模型 读取输入视频:使用OpenCV逐帧读取视频。 执行目标检测:使用YOLO检测每帧中眼睛的状态。 跟踪眼睛闭合持续时间:如果检测到眼睛“闭合”超过指定阈值,将显示警告。 显示结果:结果被写入输出视频文件,如果需要,警告消息会被叠加在帧上。
import cv2
from ultralytics import YOLO
# Load the pre-trained YOLO model
model = YOLO("best.pt")
names = model.names
# Open the video file
cap = cv2.VideoCapture("video.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))
# Initialize video writer to save the output
video_writer = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Initialize variables for eye closure detection
eye_closed_frames = 0
eye_closed_threshold_seconds = 1 # Threshold in seconds
eye_closed_threshold_frames = eye_closed_threshold_seconds * fps # Convert seconds to frames
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
# Predict the state of the eyes using YOLO
results = model.predict(im0, show=False)
boxes = results[0].boxes.xyxy.cpu().tolist()
clss = results[0].boxes.cls.cpu().tolist()
annotator = Annotator(im0, line_width=2, example=names)
eye_closed = False # Flag to check if the eye is closed in the current frame
If boxes are not None:
for box, cls in zip(boxes, clss):
clsName = names[int(cls)]
xmax = int(box[0])
ymin = int(box[1])
xmin = int(box[2])
ymax = int(box[3])
# Set color based on the class name
if clsName == 'closed':
clr = (0, 0, 255)
eye_closed = True # Mark eye as closed
elif clsName == 'opened':
clr = (0, 255, 0)
# Draw the bounding box and label
cv2.FONT_HERSHEY_SIMPLEX
Font_scale = 1
Font_thickness = 2
tw, th = cv2.getTextSize(clsName, font, font_scale, font_thickness)[0]
cv2.rectangle(im0, (xmin, ymin), (xmax, ymax), color=clr, thickness=2)
cv2.putText(im0, clsName, (xmax, ymin - 5), font, font_scale, color=clr, thickness=font_thickness)
# Check for eye closure duration
if eye_closed:
Eye_closed_frames += 1
else:
# Reset counter if the eye is not closed
# Display warning if eye has been closed for more than the threshold
if eye_closed_frames > eye_closed_threshold_frames:
print("Warning: Eye has been closed for more than 2 seconds!")
cv2.putText(im0, "WARNING: Eye closed for more than 2 seconds!", (50, 50), font, font_scale, (0, 0, 255), font_thickness)
# Write the processed frame to the output video
video_writer.write(im0)
# Release resources
cap.release()
video_writer.release()
捕获实时视频:将视频文件输入更改为实时视频流,方法是将cv2.VideoCapture参数更改为0(默认网络摄像头)。
cap = cv2.VideoCapture(0) # Use 0 for the default camera, or 1, 2, etc. for other cameras
为确保流畅的实时性能,你可能需要通过使用较小的模型(如YOLOv5s)或在可用的情况下在GPU上运行来优化模型推理速度。
使用cv2.imshow()实时显示视频流:
cv2.imshow("Eye Closure Detection", im0) if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit break
下载1:Pytoch常用函数手册 在「深度学习爱好者」公众号后台回复:Pytorch常用函数手册,即可下载全网第一份Pytorch常用函数手册,涵盖Tensors介绍、基础函数介绍、数据处理函数、优化函数、CUDA编程、多线程处理等十四章章内容。 下载2:Python视觉实战项目52讲 在「小白学视觉」公众号后台回复:Python视觉实战项目,即可下载包括图像分割、口罩检测、车道线检测、车辆计数、添加眼线、车牌识别、字符识别、情绪检测、文本内容提取、面部识别等31个视觉实战项目,助力快速学校计算机视觉。 交流群 欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~