Python 使用 cv2.VideoCapture() 从 IP 摄像头读取流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22654948/
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
Reading stream from IP camera with cv2.VideoCapture()
提问by theta
Referring to this similar question How to parse mjpeg http stream from ip camera?I was able to read the stream from IP camera, by using requests:
参考这个类似的问题How to parse mjpeg http stream from ip camera? 我能够使用requests以下命令从 IP 摄像机读取流:
stream = requests.get('http://<user>:<pass>@<addr>:<port>/videostream.cgi', stream=True)
bytez = ''
while True:
bytez += stream.raw.read(16384)
...
and it works beautifully, but would like to get there by using cv2.VideoCapture()instead requests.
它工作得很好,但想通过使用cv2.VideoCapture()请求来实现。
I tried variations in a manner of:
我尝试了以下方式的变化:
cap = cv2.VideoCapture()
cap.open('http://<user>:<pass>@<addr>:<port>/videostream.cgi?.mjpg')
while(True):
ret, frame = cap.read()
...
but wasn't able to get anything, but Exception about empty frame.
但无法得到任何东西,但关于空帧的异常。
How to read IP camera stream with cv2.VideoCapture()?
如何使用 cv2.VideoCapture() 读取 IP 摄像机流?
采纳答案by TonyParker
Add C:\OpenCV\3rdparty\ffmpeg\ to the Windows PATH environment variable or copy opencv_ffmpeg.dll from that directory to C:\Python27. This has been answered in this question OpenCV 2.4 VideoCapture not working on Windows
将 C:\OpenCV\3rdparty\ffmpeg\ 添加到 Windows PATH 环境变量或将 opencv_ffmpeg.dll 从该目录复制到 C:\Python27。这已在此问题OpenCV 2.4 VideoCapture not working on Windows 中得到解答
回答by Antonio Minides
I haven't yet tried accessing an IP camera from VideoCapture, but on your method
cap = cv2.VideoCapture()the video capture is expecting a number representing the camera usually 0.
我还没有尝试从 VideoCapture 访问 IP 摄像机,但是在您的方法中
cap = cv2.VideoCapture(),视频捕获通常需要一个代表摄像机的数字0。
By leaving it empty, it doesn't access any camera thus the Exception about an empty frame (even though later on you declare cap.open(), openCV already tried to open a camera and determined that is empty)
通过将其留空,它不会访问任何相机,因此有关空帧的异常(即使稍后您声明cap.open(),openCV 已经尝试打开相机并确定它是空的)
回答by bugmenot123
Pass the location of the camera in your cap = cv2.VideoCapture()line:
在您的cap = cv2.VideoCapture()行中传递相机的位置:
cap = cv2.VideoCapture('http://<user>:<pass>@<addr>:<port>/videostream.cgi?.mjpg')

