使用YOLOv11进行视频目标检测

使用YOLOv11进行视频目标检测

完整代码

bash 复制代码
import cv2
from ultralytics import YOLO

def predict(chosen_model, img, classes=[], conf=0.5):
    if classes:
        results = chosen_model.predict(img, classes=classes, conf=conf)
    else:
        results = chosen_model.predict(img, conf=conf)

    return results

def predict_and_detect(chosen_model, img, classes=[], conf=0.5, rectangle_thickness=2, text_thickness=1):
    results = predict(chosen_model, img, classes, conf=conf)
    for result in results:
        for box in result.boxes:
            cv2.rectangle(img, (int(box.xyxy[0][0]), int(box.xyxy[0][1])),
                          (int(box.xyxy[0][2]), int(box.xyxy[0][3])), (255, 0, 0), rectangle_thickness)
            cv2.putText(img, f"{result.names[int(box.cls[0])]}",
                        (int(box.xyxy[0][0]), int(box.xyxy[0][1]) - 10),
                        cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), text_thickness)
    return img, results

# defining function for creating a writer (for mp4 videos)
def create_video_writer(video_cap, output_filename):
    # grab the width, height, and fps of the frames in the video stream.
    frame_width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(video_cap.get(cv2.CAP_PROP_FPS))
    # initialize the FourCC and a video writer object
    fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    writer = cv2.VideoWriter(output_filename, fourcc, fps,
                             (frame_width, frame_height))
    return writer

model = YOLO("yolo11x.pt")

output_filename = "YourFilename.mp4"

video_path = r"YourVideoPath.mp4"
cap = cv2.VideoCapture(video_path)
writer = create_video_writer(cap, output_filename)
while True:
    success, img = cap.read()
    if not success:
        break
    result_img, _ = predict_and_detect(model, img, classes=[], conf=0.5)
    writer.write(result_img)
    cv2.imshow("Image", result_img)
    
    cv2.waitKey(1)
writer.release()

参考资料:

1.https://blog.csdn.net/qq_42589613/article/details/142729428

2.https://blog.csdn.net/java1314777/article/details/142665078

相关推荐
A向前奔跑6 小时前
前端实现实现视频播放的方案和面试问题
前端·音视频
TEL189246224779 小时前
IT66612:1对2 HDMI主动分配器,配备EDID RAM和嵌入式MCU
音视频·实时音视频·视频编解码
AI即插即用11 小时前
即插即用系列 | TGRS 2025 MGAM:面向遥感微小目标检测的多尺度高斯注意力机制
图像处理·人工智能·深度学习·目标检测·计算机视觉·视觉检测
9527华安12 小时前
Artix7系列FPGA实现SDI视频解码转CameraLink,基于GTP高速收发器+OSERDES2原语架构,提供2套工程源码和技术支持
fpga开发·架构·音视频
徽44013 小时前
农田植被目标检测数据标注与模型训练总结3
人工智能·目标检测·目标跟踪
wang_chao11813 小时前
Yolov5训练自定义数据集的步骤
深度学习·yolo·计算机视觉
Black蜡笔小新17 小时前
视频汇聚平台EasyCVR赋能加油站安防构建全场景视频监控体系
音视频
Coovally AI模型快速验证17 小时前
深度学习驱动的视频异常检测(VAD),AI如何让监控更智能?
人工智能·深度学习·目标检测·机器学习·自动驾驶·无人机
MediaTea17 小时前
Python 库手册:wave WAV 音频读写工具
开发语言·python·音视频
EasyDSS17 小时前
视频推流平台EasyDSS无人机推流直播技术在智慧消防场景中的应用
音视频·无人机