在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()
要启动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()