美文网首页
在OpenCV中使用GPU加速FFMPEG解码

在OpenCV中使用GPU加速FFMPEG解码

作者: LabVIEW_Python | 来源:发表于2023-11-05 10:12 被阅读0次

在OpenCV使用cv2.VideoCapture(RTSP_URL) API函数对视频流解码时,默认解码硬件是CPU

import cv2

# TP-LINK全系列摄像机均支持通过RTSP协议来获取视频流,取流地址格式如下
# 主码流为:rtsp://username:password@ip:port/stream1
# 子码流为:rtsp://username:password@ip:port/stream2
# port:RTSP端口,默认为554,若为默认可不填

RTSP_URL = "rtsp://admin:ipc43kp4@192.168.3.51/stream1"
# 默认解码硬件是CPU
cap = cv2.VideoCapture(RTSP_URL)
if not cap.isOpened():
    print('Cannot open RTSP stream')
    exit(-1)

while True:
    _, frame = cap.read()
    cv2.imshow('RTSP stream', frame)

    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()
CPU解码,GPU Decode未启动

要启动GPU Decode模块,可以输入参数:

cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG, [cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY])

import cv2

# TP-LINK全系列摄像机均支持通过RTSP协议来获取视频流,取流地址格式如下
# 主码流为:rtsp://username:password@ip:port/stream1
# 子码流为:rtsp://username:password@ip:port/stream2
# port:RTSP端口,默认为554,若为默认可不填

RTSP_URL = "rtsp://admin:ipc43kp4@192.168.3.51/stream1"
# GPU解码
cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG, [cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY])

if not cap.isOpened():
    print('Cannot open RTSP stream')
    exit(-1)

while True:
    _, frame = cap.read()
    cv2.imshow('RTSP stream', frame)

    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()
启动GPU解码

相关文章

网友评论

      本文标题:在OpenCV中使用GPU加速FFMPEG解码

      本文链接:https://www.haomeiwen.com/subject/epyzidtx.html