在 Python OpenCV 中访问网络摄像机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49978705/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:20:42  来源:igfitidea点击:

Access IP Camera in Python OpenCV

pythonopencvip-camera

提问by Employee

How do I access my IP Camera stream?

如何访问我的网络摄像机流?

Code for displaying a standard webcam stream is

显示标准网络摄像头流的代码是

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

How do I do the same exact thing but with the IP Camera?

我如何做同样的事情,但使用 IP 摄像机?

My system:

我的系统:

  • Python 2.7.14
  • OpenCV 2.4.9
  • Teledyne Dalsa Genie Nano XL Camera
  • 蟒蛇 2.7.14
  • OpenCV 2.4.9
  • Teledyne Dalsa Genie Nano XL 相机

Help will be highly appreciated

帮助将不胜感激

采纳答案by Employee

I answer my own question reporting what therefore seems to be the most comprehensiveoverall procedure to Access IP Camera in Python OpenCV.

我回答我自己的问题,报告因此似乎是在 Python OpenCV 中访问 IP 摄像机的最全面的整体程序。

Given an IP camera:

给定一个 IP 摄像头:

  • Find your camera IPaddress
  • Find the portwhere the IP address is accessed
  • Find the protocol(HTTP/RTSP etc.) specified by the camera provider
  • 查找您的相机IP地址
  • 找到port访问IP地址的地方
  • 找到protocol相机提供商指定的(HTTP/RTSP 等)

Then, if your camera is protected go ahead and find out:

然后,如果您的相机受到保护,请继续查找:

  • your username
  • your password
  • 您的 username
  • 您的 password

Then use your data to run the following script:

然后使用您的数据运行以下脚本:

"""Access IP Camera in Python OpenCV"""

import cv2

stream = cv2.VideoCapture('protocol://IP:port/1')

# Use the next line if your camera has a username and password
# stream = cv2.VideoCapture('protocol://username:password@IP:port/1')  

while True:

    r, f = stream.read()
    cv2.imshow('IP Camera stream',f)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

NOTE: In my original question I specify to being working with Teledyne Dalsa Genie Nano XL Camera. Unfortunately for this kind of cameras this normal way of accessing the IP Camera video stream does not work and the Sapera SDKmust be employed in order to grab frames from the device.

注意:在我最初的问题中,我指定使用Teledyne Dalsa Genie Nano XL 相机。不幸的是,对于此类摄像机,这种访问 IP 摄像机视频流的正常方式不起作用,必须使用Sapera SDK才能从设备抓取帧。

回答by sgarizvi

An IP camera can be accessed in opencv by providing the streaming URL of the camera in the constructor of cv2.VideoCapture.

通过在cv2.VideoCapture.

Usually, RTSP or HTTP protocol is used by the camera to stream video. An example of IP camera streaming URL is as follows:

通常,摄像机使用 RTSP 或 HTTP 协议来传输视频。IP 摄像机流 URL 的示例如下:

rtsp://192.168.1.64/1

rtsp://192.168.1.64/1

It can be opened with OpenCV like this:

它可以像这样用 OpenCV 打开:

capture = cv2.VideoCapture('rtsp://192.168.1.64/1')

Most of the IP cameras have a username and password to access the video. In such case, the credentials have to be provided in the streaming URL as follows:

大多数 IP 摄像机都有用户名和密码来访问视频。在这种情况下,必须在流 URL 中提供凭据,如下所示:

capture = cv2.VideoCapture('rtsp://username:[email protected]/1')

回答by ToanJunifer

The easiest way to stream video via IP Camera !

通过网络摄像机传输视频的最简单方法!

I just edit your example. You must replace your IP and add /videoon your link. And go ahead with your project

我只是编辑你的例子。您必须替换您的 IP 并添加/video您的链接。继续你的项目

import cv2

cap = cv2.VideoCapture('http://192.168.18.37:8090/video')

while(True):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

回答by John Harris

This works with my IP camera:

这适用于我的 IP 摄像机:

import cv2

#print("Before URL")
cap = cv2.VideoCapture('rtsp://admin:[email protected]/H264?ch=1&subtype=0')
#print("After URL")

while True:

    #print('About to start the Read command')
    ret, frame = cap.read()
    #print('About to show frame of Video.')
    cv2.imshow("Capturing",frame)
    #print('Running..')

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

I found the Stream URL in the Camera's Setup screen: IP Camera Setup Screen

我在相机的设置屏幕中找到了流 URL: IP 摄像机设置屏幕

Note that I added the Username (admin) and Password (123456) of the camera and ended it with an @ symbol before the IP address in the URL (admin:123456@)

请注意,我添加了摄像机的用户名 (admin) 和密码 (123456),并在 URL (admin:123456@) 中的 IP 地址前以 @ 符号结束

回答by Venkatesh

First find out your IP camera's streaming url, like whether it's RTSP/HTTP etc.

首先找出你的网络摄像机的流媒体 url,比如它是否是 RTSP/HTTP 等。

Code changes will be as follows:

代码更改如下:

cap = cv2.VideoCapture("ipcam_streaming_url")

For example:

例如:

cap = cv2.VideoCapture("http://192.168.18.37:8090/test.mjpeg")

回答by Gustavo GeoDrones

To access an Ip Camera, first, I recommend you to install it like you are going to use for the standard application, without any code, using normal software.

要访问网络摄像机,首先,我建议您像使用标准应用程序一样安装它,无需任何代码,使用普通软件。

After this, you have to know that for different cameras, we have different codes. There is a website where you can see what code you can use to access them:

在此之后,您必须知道对于不同的相机,我们有不同的代码。有一个网站,您可以在其中查看可以使用哪些代码来访问它们:

https://www.ispyconnect.com/sources.aspx

https://www.ispyconnect.com/sources.aspx

But be careful, for my camera (Intelbras S3020) it does not work. The right way is to ask the company of your camera, and if they are a good company they will provide it.

但是要小心,对于我的相机(Intelbras S3020)它不起作用。正确的方法是询问您相机的公司,如果他们是一家好公司,他们会提供。

When you know your code just add it like:

当您知道您的代码时,只需将其添加为:

cap = cv2.VideoCapture("http://LOGIN:PASSWORD@IP/cgi-bin/mjpg/video.cgi?&subtype=1")

Instead LOGIN you will put your login, and instead PASSWORD you will put your password.

您将输入登录名而不是 LOGIN,您将输入密码而不是 PASSWORD。

To find out camera's IP address there is many softwares that you can download and provide the Ip address to you. I use the software from Intelbras, but I also recommend EseeCloud because they work for almost all cameras that I've bought:

要找出摄像机的 IP 地址,您可以下载许多软件并将 IP 地址提供给您。我使用 Intelbras 的软件,但我也推荐 EseeCloud,因为它们几乎适用于我购买的所有相机:

https://eseecloud.software.informer.com/1.2/

https://esecloud.software.informer.com/1.2/

In this example, it shows the protocol http to access the Ip camera, but you can also use rstp, it depends on the camera, as I said.

在这个例子中,它显示了访问IP摄像头的协议http,但你也可以使用rstp,这取决于摄像头,正如我所说。

If you have any further questions just let me know.

如果您有任何进一步的问题,请告诉我。

回答by Soma Banerjee

In pycharmI wrote the code for accessing the IP Camera like:

pycharm 中,我编写了用于访问 IP 摄像机的代码,例如:

import cv2

cap=VideoCapture("rtsp://user_name:password@IP_address:port_number")

ret, frame=cap.read()

You will need to replace user_name, password, IPand portwith suitable values

您将需要更换user_namepasswordIPport用合适的值

回答by Garry

For getting the IP Camera video link:

获取网络摄像机视频链接:

  1. Open the IP Camera with given IPand PORTin browser
  2. Right click the video and select "copy image address"
  3. Use that address to capture video
  1. 给定打开网络摄像机IPPORT浏览器
  2. 右击视频,选择“复制图片地址”
  3. 使用该地址捕获视频

回答by Dinusha Dilanka

You can access to most of ip cameras as below

您可以访问大多数网络摄像机,如下所示

import cv2 

path = "http://username:password@your_ip:your_port/tmpfs/auto.jpg"

while True:

     cap = cv2.VideoCapture(path)

     ret, frame = cap.read()
     if not ret:
         break

     cv2.imshow('frame', frame)

     key = cv2.waitKey(1) & 0xFF

     if key == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()

But, I am not sure this method will perform well because it is accessing image.

但是,我不确定此方法是否会执行良好,因为它正在访问图像。

回答by Rami Alloush

Getting the correct URL for your camera seems to be the actual challenge! I'm putting my working URL here, it might help someone. The camera is EZVIZ C1Cwith exact model cs-c1c-d0-1d2wf. The working URL is

为您的相机获取正确的 URL 似乎是真正的挑战!我把我的工作网址放在这里,它可能对某人有所帮助。相机EZVIZ C1C型号准确cs-c1c-d0-1d2wf。工作网址是

rtsp://admin:[email protected]/h264_stream

where SZGBZTis the verification code found at the bottom of the camera. adminis always adminregardless of any settings or users you have.

SZGBZT相机底部的验证码在哪里。admin始终admin与您拥有的任何设置或用户无关。

The final code will be

最终代码将是

video_capture = cv2.VideoCapture('rtsp://admin:[email protected]/h264_stream')