【发布时间】:2023-02-04 21:40:18
【问题描述】:
我正在测试一个小的 python 脚本来通过 RTSP 从 IP 摄像机捕获图像。我可以通过 VLC 和使用 ffmpeg 通过 CLI 正常打开视频,但是使用带有 Python3 的 OpenCV,我收到错误:OpenCV: Couldn't read video stream from file "rtsp://admin:admin123456@192.168.15.2:8554/profile0 "
此外,我可以使用 python 脚本打开本地 .mp4 视频。将 RTSP 与 opencv 一起使用时,这似乎是一个问题。
这是代码:
import cv2
import os
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp' # Use tcp instead of udp if stream is unstable
cap = cv2.VideoCapture("rtsp://admin:admin123456@192.168.15.2:8554/profile0")
if not cap.isOpened():
print('Cannot open RTSP stream')
exit(-1)
while True:
success, img = cap.read()
cv2.imshow('RTSP stream', img)
if cv2.waitKey(1) & 0xFF == ord('q'): # Keep running until you press `q`
cap.release()
break
cv2.destroyAllWindows()
尝试运行 python 脚本时出现错误:OpenCV: Couldn't read video stream from file rtsp://admin:admin123456@192.168.15.2:8554/profile0"
【问题讨论】:
-
尝试为
apiPreference显式传递 CAP_FFMPEG。在 macOS 上,您正在获取 AVFoundation(错误字符串暗示 AVFoundation)。 -- 检查print(cv.getBuildInformation())视频 I/O 部分。 -
视频 I/O:DC1394:无 FFMPEG:无 avcodec:无 avformat:无 avutil:无 swscale:无 avresample:无 GStreamer:无 AVFoundation:是 还尝试明确传递 CAP_FFMPEG,同样的错误。
-
你不能使用 ffmpeg,OpenCV 不是用 ffmpeg 构建的。找到一个带有 ffmpeg 的构建。为此你需要 ffmpeg。 AVFoundation 做不到。
标签: python macos opencv avfoundation rtsp