【问题标题】:Access IP camera with OpenCV使用 OpenCV 访问 IP 摄像机
【发布时间】:2018-01-14 08:32:40
【问题描述】:

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

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()

【问题讨论】:

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


【解决方案1】:

您可以使用 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

【讨论】:

  • urlliburllib2 在 Python 3.x 上不可用
【解决方案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)

【讨论】:

  • 这不适用于 python 3 TypeError: Can't convert 'bytes' object to str implicitly
  • urlliburllib2 在 Python 3.x 上不可用
【解决方案3】:

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

每个 IP 摄像机都有 RTSP 来流式传输实时视频。

因此您可以使用 RTSP Link 代替 videofeed

【讨论】:

    【解决方案4】:

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

    要访问笔记本电脑网络摄像头以外的摄像头,您可以使用这样的 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()
    

    ma​​in.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

    【讨论】:

      【解决方案5】:

      使用下面的代码直接通过opencv访问ipcam。将 VideoCapture 中的 url 替换为您的特定相机 rtsp url。给定的那个通常适用于我用过的大多数相机。

      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()
      

      【讨论】:

        【解决方案6】:

        如果使用 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()
        

        【讨论】:

          猜你喜欢
          • 2014-04-19
          • 2017-04-23
          • 2020-05-25
          • 2013-03-13
          • 1970-01-01
          • 1970-01-01
          • 2012-10-30
          • 1970-01-01
          相关资源
          最近更新 更多