yolov5输入视频输出图片如何实现

yolov5视频检测如何只保存有目标的图片
源代码是检测视频保存整个视频

以下是一个简单的示例,展示了如何在 YOLOv5 视频检测中只保存有目标的图片:

import cv2
import numpy as np

# 加载 YOLOv5 模型
model = yolov5.load_model()

# 加载视频
video = cv2.VideoCapture('video.mp4')

# 读取视频的每一帧
while True:
    # 读取帧
    success, frame = video.read()
    
    if not success:
        break

    # 将帧转换为网络输入大小
    frame = cv2.resize(frame, (640, 640))
    
    # 进行检测
    detections = model.detect(frame)
    
    # 如果检测到目标
    if len(detections) > 0:
        # 使用 OpenCV 保存图片
        cv2.imwrite('detected.jpg', frame)

# 关闭视频
video.release()

望采纳。