Opencv 如何互动并从摄像机获取实时流

Opencv 如何互动并从摄像机获取实时流,opencv,curl,python-requests,vlc,live-streaming,Opencv,Curl,Python Requests,Vlc,Live Streaming,我有一个Garmin VIRB XE摄像头,希望获得实时流并与摄像头交互,就像获取GPS数据一样。我可以通过VLC media player获取实时流,也可以通过windows命令提示符下的CURL将命令发布到摄影机,但我无法使用OpenCV获取实时流,也无法使用python中的请求库与摄影机交互 我可以从“网络”获取实时流rtsp://192.168.1.35/livePreviewStream使用VLC media player的网络流媒体功能,还可以与摄像头进行交互,例如,通过命令提示符下

我有一个Garmin VIRB XE摄像头,希望获得实时流并与摄像头交互,就像获取GPS数据一样。我可以通过VLC media player获取实时流,也可以通过windows命令提示符下的CURL将命令发布到摄影机,但我无法使用OpenCV获取实时流,也无法使用python中的请求库与摄影机交互

我可以从“网络”获取实时流rtsp://192.168.1.35/livePreviewStream使用VLC media player的网络流媒体功能,还可以与摄像头进行交互,例如,通过命令提示符下的“curl--data”{\“command\:\“startRecording\”}”,我可以开始录制,但以下代码不起作用

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

post返回错误 “ProxyError:HTTPConnectionPool(host='127.0.0.1',port=8000):url超过了最大重试次数:(由ProxyError('无法连接到代理')、RemoteDisconnected('远程端关闭连接,无响应')引起)。”。
此外,捕获无法获得任何帧。

因为您已经确认您的RTSP链接与VLC player一起工作,这里有一个使用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
代码

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