【问题标题】:Python - OpenCV: Couldn't read video stream from file RSTPPython - OpenCV:无法从文件 RSTP 读取视频流
【发布时间】: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


【解决方案1】:

我在 MacOS 12.5 上有类似的问题。

我的cv2.getBuildInformation()上有FFMPEG: YES,但仍然收到错误OpenCV: Couldn't read video stream from file

但是,使用相同的 conda 环境(相同的 opencv-python 版本)但通过 jupyter notebook 而不是脚本不会出错,并且文件读取没有问题!

import cv2
recording = cv2.VideoCapture(video_path)

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    相关资源
    最近更新 更多