参考文档:配置摄像机ONVIF协议参数 - SDC 10.0 C系列产品文档 - 华为机器视觉 (huawei.com)
配置摄像机ONVIF协议参数
1)登录摄像机Web界面,选择“配置 > 视音频 > 视频”,将“编码协议”设置为“H.264”
2)进入“网络平台对接”配置,选择“第二协议参数 > ONVIF”,进入ONVIF协议参数配置页面
3)python代码实现
安装onvif包
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple onvif
代码实现
""" ONVIF鉴权方式 Digest_MD5 (priority) / WSSE """ from onvif import ONVIFCamera import cv2 import requests from requests.auth import HTTPDigestAuth import numpy as np from datetime import datetime print("请求前时间:", datetime.now().strftime("%Y-%m-%d %H:%M:%S")) # 摄像头连接信息 camera_ip = 'xxx.xxx.xxx.xxx' # 替换为实际的摄像头IP地址 camera_port = 80 # 替换为实际的端口号 username = 'xxxx' # 替换为实际的用户名 password = 'xxxx' # 替换为实际的密码 wsdl_path = 'C:/Software/Anaconda/anaconda/envs/yolov8_2/Lib/site-packages/wsdl' # 替换为实际的WSDL文件路径 # 连接到ONVIF摄像头 camera = ONVIFCamera(camera_ip, camera_port, username, password, wsdl_path) # 创建媒体服务 media_service = camera.create_media_service() # 获取视频源配置 profiles = media_service.GetProfiles() profile = profiles[0] # 获取截图URL snapshot_uri = media_service.GetSnapshotUri({'ProfileToken': profile.token}) snapshot_url = snapshot_uri.Uri # 获取截图 response = requests.get(snapshot_url, auth=HTTPDigestAuth(username, password)) # 将截图保存为图像文件 if response.status_code == 200: image_data = np.frombuffer(response.content, np.uint8) image = cv2.imdecode(image_data, cv2.IMREAD_COLOR) print("请求后时间:", datetime.now().strftime("%Y-%m-%d %H:%M:%S")) cv2.imwrite('snapshot.jpg', image) print("Snapshot saved as snapshot.jpg") else: print(f"Failed to get snapshot. Status code: {response.status_code}")
扩展:如果出现 onvif.exceptions.ONVIFError: Unknown error: No such file: /home/lzh/anaconda3/envs/py2/lib/python2.7/site-packages/wsdl/devicemgmt.wsdl报错
这是因为wsdl_path 路径没有配置正确,将其路径改为安装onvif包的位置,报错解决
标签:python,抓取,camera,snapshot,import,wsdl,onvif,ONVIF From: https://www.cnblogs.com/REN-Murphy/p/18204548