Linux RTSP 流和 OpenCV (Python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20891936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RTSP stream and OpenCV (Python)
提问by Guadancil11
I have an IP camera streaming on Linux through rtsp protocol and h264 linux driver. I am able to see the video in VLC with the following address and port:
我有一个通过 rtsp 协议和 h264 linux 驱动程序在 Linux 上传输的 IP 摄像机。我可以使用以下地址和端口在 VLC 中看到视频:
rtsp://192.168.1.2:8080/out.h264
However if I try to get the same video for OpenCV processing in Python 2.7.5 (MacOS X 10.9):
但是,如果我尝试在 Python 2.7.5 (MacOS X 10.9) 中获取用于 OpenCV 处理的相同视频:
import cv
video = cv.CaptureFromFile('rtsp://192.168.1.2:8080/out.h264')
I get the following error:
我收到以下错误:
WARNING: Couldn't read movie file rtsp://192.168.1.2:8080/out.h264
It seems something rather simple, but I am stuck on it. Thanks.
这似乎很简单,但我坚持它。谢谢。
回答by Pabzt
this works for me (using opencv 2.4.9):
这对我有用(使用 opencv 2.4.9):
vcap = cv.VideoCapture("rtsp://192.168.1.2:8080/out.h264")
while(1):
ret, frame = vcap.read()
cv.imshow('VIDEO', frame)
cv.waitKey(1)
回答by Solar.gy
OpenCV relies on ffmpeg or other video backends for handling video formats and IP camera protocols. Depending on your platform and how you installed OpenCV, you may not have any support for rtsp.
OpenCV 依赖 ffmpeg 或其他视频后端来处理视频格式和 IP 摄像机协议。根据您的平台以及您安装 OpenCV 的方式,您可能不支持 rtsp。
You can check video backend support for your OpenCV installation:
您可以检查 OpenCV 安装的视频后端支持:
python -c "import cv2; print(cv2.getBuildInformation())"
Video I/O:
DC1394 1.x: NO
DC1394 2.x: NO
FFMPEG: NO
avcodec: NO
avformat: NO
avutil: NO
swscale: NO
avresample: NO
GStreamer: NO
OpenNI: NO
OpenNI PrimeSensor Modules: NO
OpenNI2: NO
PvAPI: NO
GigEVisionSDK: NO
Aravis SDK: NO
UniCap: NO
UniCap ucil: NO
V4L/V4L2: NO/NO
XIMEA: NO
Xine: NO
gPhoto2: NO
回答by Krik
回答by Uni
Somehow by default, as I knew, OpenCV used TCP protocol for transporting. Then, if your streaming is using UDP protocol, then you must define the environ option by the following code:
据我所知,默认情况下,OpenCV 使用 TCP 协议进行传输。然后,如果您的流媒体使用 UDP 协议,那么您必须通过以下代码定义环境选项:
import os
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
cam = cv2.VideoCapture("rtsp://YOUR_STREAMING_IP_ADDRESS:PORT/foo.sdp", cv2.CAP_FFMPEG)
You also need check if your openCV2 build comes with FFMPEG (RTSP) or not as Solar.gy 's anwser, if not, you must rebuild and install openCV with FFMPEG.
您还需要检查您的 openCV2 构建是否带有 FFMPEG (RTSP) 作为 Solar.gy 的 anwser,如果没有,您必须使用 FFMPEG 重建和安装 openCV。
python -c "import cv2; print(cv2.getBuildInformation())"