首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何进行交互并从摄像头获取直播流

如何进行交互并从摄像头获取直播流
EN

Stack Overflow用户
提问于 2019-08-20 10:18:31
回答 1查看 1.3K关注 0票数 1

我有一个Garmin VIRB XE相机,并想获得一个实时流和互动的相机,如获取GPS数据。我可以通过VLC媒体播放器获取直播流,也可以在windows命令提示符下通过CURL向摄像头发送命令,但是我不能使用OpenCV获取直播流,也不能使用python中的请求库与摄像头进行交互。

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

代码语言:javascript
复制
'''
import simplejson
import requests
url='http://192.168.1.37:80/virb'
data = {'command':'startRecording'}
r=requests.post(url, simplejson.dumps(data))
'''

代码语言:javascript
复制
'''
import cv2
capture = cv2.VideoCapture("rtsp://192.168.1.35/livePreviewStream")
'''

post返回错误"ProxyError: HTTPConnectionPool(host='127.0.0.1',port=8000):超过url:http://192.168.1.37:80/virb的最大重试次数(由ProxyError(‘无法连接到代理服务器’,RemoteDisconnected(‘没有响应的远程端关闭连接’))引起)“。此外,捕获无法获取任何帧。

EN

回答 1

Stack Overflow用户

发布于 2019-08-27 06:41:15

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

根据你的IP摄像头,你的RTSP流链接会有所不同,这是我的一个例子:

代码语言:javascript
复制
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

代码

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57565925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档