简体   繁体   English

如何在python中使用opencv访问来自网络摄像机的视频流?

[英]How to access video stream from an ip camera using opencv in python?

Right now I am using a web cam and it works perfectly fine with the code below -:现在我正在使用网络摄像头,它与以下代码完美配合 -:

capture = cv2.VideoCapture(0)

Now instead of web cam, I want to use the ip camera( https://192.168.0.60 ) What would be the easiest way to do it with OpenCV(Python)?现在代替网络摄像头,我想使用网络摄像头( https://192.168.0.60 )使用 OpenCV(Python)最简单的方法是什么?

I saw a bunch of posts, but couldn't find a straight answer to this.我看了一堆帖子,但找不到直接的答案。 Can someone help, who already got it running?有人可以帮忙吗,谁已经开始运行了?

Thank you!谢谢!

Firstly, you must find the exact url for your video stream, and that's best done with a web browser.首先,您必须找到视频流的确切网址,最好使用网络浏览器来完成。 For example I use IP Webcam app on android (com.pass.webcam) and it's stream will be on:例如,我在 android (com.pass.webcam) 上使用 IP 网络摄像头应用程序,它的流将打开:

http://phone-ip-address:port/video

If I visit that url with a web browser, I can see the live stream.如果我使用网络浏览器访问该 url,我可以看到实时流。 Make sure, that what you see is only the video stream, not a html page with the stream.确保您看到的只是视频流,而不是带有流的 html 页面。 If there is a html page, you can right-click and select Open image in new tab (in Chrome) to get to the stream.如果有 html 页面,您可以右键单击并选择在新选项卡中打开图像(在 Chrome 中)以访问流。

However it looks like OpenCV can only read the video stream if the filename/url has the right suffix.但是,如果文件名/ url 具有正确的后缀,则 OpenCV 似乎只能读取视频流。 Adding ?type=some.mjpeg worked for me.添加 ?type=some.mjpeg 对我有用。 So the url would be:所以网址将是:

http://phone-ip-address:port/video?type=some.mjpeg

Try visiting such an url in the web browser before you go for opencv.在使用 opencv 之前,请尝试在 Web 浏览器中访问这样的 url。

Take a look at this example with python , OpenCV , IPCAM and hikvisionpythonOpenCVIPCAMhikvision看一下这个例子

import numpy as np 
import cv2

cap = cv2.VideoCapture() 
cap.open("rtsp://USER:PASS@IP:PORT/Streaming/Channels/2")

while(True):
     # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('Salida',frame)

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

# When everything done, release the capture 
cap.release()
cv2.destroyAllWindows()

Image: Get video from IPCAM with python and OpenCV图片:使用 python 和 OpenCV 从 IPCAM 获取视频

Here you go,干得好,

import numpy as np
import cv2

cap = cv2.VideoCapture('rtsp://<username_of_camera>:<password_of_camera@<ip_address_of_camera')

while(True):

    ret, frame = cap.read()
    cv2.imshow('Stream IP Camera OpenCV',frame)

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

cap.release()
cv2.destroyAllWindows()

For example:例如:

import numpy as np
import cv2

cap = cv2.VideoCapture('rtsp://admin:admin@192.168.0.60')

while(True):

    ret, frame = cap.read()
    cv2.imshow('Stream IP camera opencv',frame)

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

cap.release()
cv2.destroyAllWindows()

Then save the file as camera.py (.py), go to command prompt or terminal, locate file and type python camera.py or python <file_name>.py enter to run script.然后将文件另存为 camera.py (.py),进入命令提示符或终端,找到文件并输入python camera.pypython <file_name>.py enter 运行脚本。
If you want to exit from script windows just press "q" or close cmd.如果您想退出脚本窗口,只需按“q”或关闭 cmd。

Hope this helpful.希望这有帮助。

Here is an example for a Vivotek IP8136W IP Camera.以下是 Vivotek IP8136W IP 摄像机的示例。 It does not support streaming.它不支持流媒体。

This code continuously grabs still frames, at about 2fps.此代码以大约 2fps 的速度连续抓取静止帧。 No observed CPU loading.没有观察到 CPU 负载。

import numpy as np
import cv2

# for webcams, request stream only once.
#cap = cv2.VideoCapture(0)
    
while(True):

    # For single image IP cams, request frame every time.
    cap = cv2.VideoCapture("http://root:0002A78D65F2@192.168.1.38/cgi-bin/viewer/video.jpg")

    ret, frame = cap.read()
   
    # Display the resulting frame
    if ret:
        cv2.imshow('camera',frame)
    else:
        print("error getting frame.")

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

# Done. release the capture
cap.release()
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM