【问题标题】:How to interact and get live stream from camera如何交互并从摄像头获取实时流
【发布时间】:2019-08-20 02:18:30
【问题描述】:

我有一台 Garmin VIRB XE 摄像头,并且想要获取实时流并与摄像头交互,例如获取 GPS 数据。我可以通过 VLC 媒体播放器获取实时流,也可以通过 CURL 从 Windows 命令提示符向相机发布命令,但我无法使用 OpenCV 获取实时流并使用 python 中的请求库与相机交互。

我可以使用 VLC 媒体播放器的网络流功能从“rtsp://192.168.1.35/livePreviewStream”获取实时流,也可以与相机交互,例如通过“curl --data”{\“command\” :\"startRecording\"}" http://192.168.1.35/virb" 从命令提示符开始我可以开始录制,但以下代码不起作用。

'''
import simplejson
import requests
url='http://192.168.1.37:80/virb'
data = {'command':'startRecording'}
r=requests.post(url, simplejson.dumps(data))
'''

'''
import cv2
capture = cv2.VideoCapture("rtsp://192.168.1.35/livePreviewStream")
'''

帖子返回错误 “ProxyError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: http://192.168.1.37:80/virb (由 ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response' )))”。 捕获也无法获得任何帧。

【问题讨论】:

    标签: opencv curl python-requests vlc live-streaming


    【解决方案1】:

    由于您已经确认您的 RTSP 链接适用于 VLC 播放器,因此这里有一个使用 OpenCV 和 cv2.VideoCapture.read() 的 IP 摄像机视频流小部件。由于read() 是一个阻塞操作,因此此实现使用线程来获取不同线程中的帧。通过将此操作放在仅专注于获取帧的单独操作中,它可以通过减少 I/O 延迟来提高性能。我使用了自己的 IP 摄像机 RTSP 流链接。将 stream_link 更改为您自己的 IP 摄像头链接。

    根据您的 IP 摄像机,您的 RTSP 流链接会有所不同,这是我的一个示例:

    rtsp://username:password@192.168.1.49:554/cam/realmonitor?channel=1&subtype=0
    rtsp://username:password@192.168.1.45/axis-media/media.amp
    

    代码

    from threading import Thread
    import cv2
    
    class VideoStreamWidget(object):
        def __init__(self, src=0):
            # Create a VideoCapture object
            self.capture = cv2.VideoCapture(src)
    
            # Start the thread to read frames from the video stream
            self.thread = Thread(target=self.update, args=())
            self.thread.daemon = True
            self.thread.start()
    
        def update(self):
            # Read the next frame from the stream in a different thread
            while True:
                if self.capture.isOpened():
                    (self.status, self.frame) = self.capture.read()
    
        def show_frame(self):
            # Display frames in main program
            if self.status:
                self.frame = self.maintain_aspect_ratio_resize(self.frame, width=600)
                cv2.imshow('IP Camera Video Streaming', self.frame)
    
            # Press Q on keyboard to stop recording
            key = cv2.waitKey(1)
            if key == ord('q'):
                self.capture.release()
                cv2.destroyAllWindows()
                exit(1)
    
        # Resizes a image and maintains aspect ratio
        def maintain_aspect_ratio_resize(self, image, width=None, height=None, inter=cv2.INTER_AREA):
            # Grab the image size and initialize dimensions
            dim = None
            (h, w) = image.shape[:2]
    
            # Return original image if no need to resize
            if width is None and height is None:
                return image
    
            # We are resizing height if width is none
            if width is None:
                # Calculate the ratio of the height and construct the dimensions
                r = height / float(h)
                dim = (int(w * r), height)
            # We are resizing width if height is none
            else:
                # Calculate the ratio of the 0idth and construct the dimensions
                r = width / float(w)
                dim = (width, int(h * r))
    
            # Return the resized image
            return cv2.resize(image, dim, interpolation=inter)
    
    if __name__ == '__main__':
        stream_link = 'your stream link!'
        video_stream_widget = VideoStreamWidget(stream_link)
        while True:
            try:
                video_stream_widget.show_frame()
            except AttributeError:
                pass
    

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 2016-10-23
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2022-10-20
      相关资源
      最近更新 更多