在OpenCV中使用GPU加速FFMPEG解码

在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解码
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容