使用OpenCV访问IP摄像头

时间:2017-08-07 05:40:57

标签: python-3.x opencv ip-camera

无法访问视频流。任何人都可以帮助我获取视频流。我在谷歌搜索了解决方案并在堆栈溢出中发布了另一个问题,但遗憾的是没有什么不能解决问题。

import cv2
cap = cv2.VideoCapture()
cap.open('http://192.168.4.133:80/videostream.cgi?user=admin&pwd=admin')
while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

6 个答案:

答案 0 :(得分:2)

您可以使用urllib从视频流中读取帧。

import cv2
import urllib
import numpy as np

stream = urllib.urlopen('http://192.168.100.128:5000/video_feed')
bytes = ''
while True:
    bytes += stream.read(1024)
    a = bytes.find(b'\xff\xd8')
    b = bytes.find(b'\xff\xd9')
    if a != -1 and b != -1:
        jpg = bytes[a:b+2]
        bytes = bytes[b+2:]
        img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
        cv2.imshow('Video', img)
        if cv2.waitKey(1) == 27:
            exit(0)

如果您要从电脑的网络摄像头流式传输视频,请查看此信息。 https://github.com/shehzi-khan/video-streaming

答案 1 :(得分:2)

谢谢。可能是,现在urlopen不在utllib下。它位于urllib.request.urlopen下。我使用此代码:

import cv2
from urllib.request import urlopen
import numpy as np

stream = urlopen('http://192.168.4.133:80/video_feed')
bytes = ''
while True:
    bytes += stream.read(1024)
    a = bytes.find(b'\xff\xd8')
    b = bytes.find(b'\xff\xd9')
    if a != -1 and b != -1:
        jpg = bytes[a:b+2]
        bytes = bytes[b+2:]
        img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
        cv2.imshow('Video', img)
        if cv2.waitKey(1) == 27:
            exit(0)

答案 2 :(得分:2)

您可以使用此代码在浏览器中获取实时视频供稿。

要访问笔记本电脑网络摄像头以外的摄像头,您可以使用RTSP链接

  

rtsp:// admin:12345@192.168.1.1:554 / h264 / ch1 / main / av_stream“

其中

   username:admin
   password:12345
   your camera ip address and port
   ch1 is first camera on that DVR
     

使用此链接为您的摄像机替换cv2.VideoCamera(0)   它会起作用

camera.py

import cv2

class VideoCamera(object):
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        self.video = cv2.VideoCapture(0)
        # If you decide to use video.mp4, you must have this file in the folder
        # as the main.py.
        # self.video = cv2.VideoCapture('video.mp4')

    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, image = self.video.read()
        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # video stream.
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()

main.py

from flask import Flask, render_template, Response
from camera import VideoCamera

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

然后您可以按照this blog来提高视频流的FPS

答案 3 :(得分:1)

您可以使用RTSP代替直接视频Feed。

每台IP摄像机都有RTSP来实时播放视频。

因此,您可以使用RTSP Link而不是视频输入

答案 4 :(得分:1)

使用以下代码直接通过opencv访问ipcam。用您的特定摄像机rtsp网址替换VideoCapture中的网址。给出的一个通常适用于我使用过的大多数相机。

import cv2

cap = cv2.VideoCapture("rtsp://[username]:[pass]@[ip address]/media/video1")

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

答案 5 :(得分:-1)

如果使用python 3,则可能需要使用字节数组而不是字符串。 (修改当前的最佳答案)

with urllib.request.urlopen('http://192.168.100.128:5000/video_feed') as stream:

    bytes = bytearray()

    while True:
        bytes += stream.read(1024)
        a = bytes.find(b'\xff\xd8')
        b = bytes.find(b'\xff\xd9')
        if a != -1 and b != -1:
            jpg = bytes[a:b+2]
            bytes = bytes[b+2:]
            img = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
            cv2.imshow('Video', img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

cv2.destroyAllWindows()