1、配置文件 config.yaml
# 监控视频流ip地址
ipList:
- 192.168.3.1
# 摄像头账号和密码
loginInfo: {
user: admin,
password: 123456,
channel: 3
}
# 图片文件存储路径
save_image_dic: # 自己的文件路径
# 每一轮图片保存延时
delays: 10
2、saveImage.py
import os
import yaml
import cv2
import datetime
import time
config_path = 'config.yaml'
with open(config_path, 'r', encoding='utf-8') as f:
param = yaml.load(f.read(), Loader=yaml.FullLoader)
class SaveImage:
def __init__(self, ip_list, loginInfo, save_dic, param):
super(SaveImage, self).__init__()
self.ip_list = ip_list # 摄像头地址
self.user = loginInfo['user'] # 用户名
self.password = loginInfo['password'] # 密码
self.channel = loginInfo['channel'] # 通道
self.save_dic = save_dic # 文件存储地址
self.param = param # 配置参数
def __getStream__(self):
stream_list = []
for ip in self.ip_list:
stream_list.append(f"rtsp://{self.user}:{self.password}@{ip}/Streaming/Channels/{self.channel}")
return stream_list
def getHikvision(self):
stream_list = self.__getStream__()
for stream in stream_list:
cap = cv2.VideoCapture(stream)
if cap.isOpened():
print(f"监控球机{stream}连接成功")
image_name = self.save_dic + '/' + stream.split('@')[-1].split('/')[0] + '.jpg'
cv2.imwrite(image_name, cap.read()[1])
cap.release()
else:
print(f'监控球机{stream}连接失败')
print('*' * 30 + '\t', '*' * 30)
def timer_get_image(self):
last_time = datetime.datetime.now()
while True:
current_time = datetime.datetime.now()
if (current_time - last_time).seconds >= self.param['delays']:
self.getHikvision()
last_time = current_time
if __name__ == '__main__':
save_image = SaveImage(ip_list=param['ipList'], loginInfo=param['loginInfo'],
save_dic=param['save_image_dic'], param=param)
save_image.timer_get_image()
3、说明
方法中会轮询摄像头ip列表,每一次轮询会覆盖上一次的文件