实时保存网络流图片
import cv2
'''
HTTP协议:客户端发出请求,服务端响应
RTSP协议:客户端和服务端都可以发出请求
RTSP (Real Time Streaming Protocol):实时流传输协议,该协议是一个应用层协议,这个协议定义了,多个应用程序如何通过IP网络传输多媒体数据(即视频流)
RTSP协议是一个双向的
rtsp协议的IP: rtsp://YourIp:8554/live
http协议的IP: http://YourIp:8081/video
URL说明:
http: 传输协议
admin: 用户名,默认为admin
Password: 密码,默认就是123456
@IP: @符号后面跟摄像头的局域IP地址
:port: 冒号后面跟端口号
'''
if __name__ == "__main__":
# 建个窗口并命名
cv2.namedWindow("look", 1)
# IP视频地址
# url = "http://admin:123456@YourIp:8081/video"
url = "rtsp://admin:123456@YourIp:8554/live"
capture = cv2.VideoCapture(url) # 读取视频流
print('start work ~~')
print('Press "esc" exit')
print('Press "s" save img')
num = 0
filepath = './img/'
while capture.isOpened():
# Capture frame-by-frame
ret, frame = capture.read()
# Display the resulting frame
cv2.imshow('look', frame)
key = cv2.waitKey(3)
if key == 27:
# exit
print("Esc break")
break
if key == ord('s'):
# save
num = num + 1
filename = "img_%s.jpg" % num
cv2.imwrite(filepath + filename, frame)
print('save ' + filename)
# When everything done, release the capture
capture.release()
cv2.destroyAllWindows()