我有一台Garmin VIRB XE摄像机,并且想要获得实时流并与摄像机进行交互,例如获取GPS数据。我可以通过VLC媒体播放器获取实时流,也可以通过Windows命令提示符下的CURL将命令发布到摄像机,但是我无法使用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(主机='127.0.0.1',端口= 8000):网址重试次数超过了上限:http://192.168.1.37:80/virb(由ProxyError('无法连接到代理服务器。',RemoteDisconnected('响应')))”。 另外,捕获无法获得任何帧。
答案 0 :(得分: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