文章目录
python实现onvif客户端及问题小结
1、前言2、python-onvif安装及ptz示例
2.1 openwrt下安装pip及python-onvif2.2 ptz示例 3、使用时可能的错误 1、前言
原本一直使用的go方式进行摄像头的搜索、改网络地址、获取media、ptz以及preset等,但是目前测试go的库在部分摄像头上无法正常ptz,于是在分析c/c++的库和python的库后,暂时打算测试一下python的库,使用python在pip中可以直接安装的onvif包即可,写起来还是很方便的,大部分接口都实现了,相较于go的包更成熟一些,相较于c++的方式能更快上手,所以目前搜索和改网络等还是使用的go,ptz等go实现不好的地方传递参数给python脚本来做,等到有功夫的时候再来把go的包修改一下(python的包显示似乎搜索不好处理,所以也没有全部切换到python)。
2、python-onvif安装及ptz示例地址,目前在0.2.0版本,似乎也有一段时间没有更新了:
https://github.com/quatanium/python-onvif
可以直接使用pip安装,这个是适用于python2的,其中也提供了适用于python3的地址:https://github.com/FalkTannhaeuser/python-onvif-zeep
2.1 openwrt下安装pip及python-onvif安装python2.7:
opkg updateopkg install python2.7
安装pip:
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py --no-check-certificatepython2.7 get-pip.py
安装python-onvif:
pip install onvif
2.2 ptz示例如下,也是采用的continuousMove,这个是官方给的示例,可以直接拿来用:
from time import sleepfrom onvif import onVIFCameraXMAX = 1XMIN = -1YMAX = 1YMIN = -1def perform_move(ptz, request, timeout): # Start continuous move ptz.ContinuousMove(request) # Wait a certain time sleep(timeout) # Stop continuous move ptz.Stop({'ProfileToken': request.ProfileToken})def move_up(ptz, request, timeout=1): print 'move up...' request.Velocity.PanTilt._x = 0 request.Velocity.PanTilt._y = YMAX perform_move(ptz, request, timeout)def move_down(ptz, request, timeout=1): print 'move down...' request.Velocity.PanTilt._x = 0 request.Velocity.PanTilt._y = YMIN perform_move(ptz, request, timeout)def move_right(ptz, request, timeout=1): print 'move right...' request.Velocity.PanTilt._x = XMAX request.Velocity.PanTilt._y = 0 perform_move(ptz, request, timeout)def move_left(ptz, request, timeout=1): print 'move left...' request.Velocity.PanTilt._x = XMIN request.Velocity.PanTilt._y = 0 perform_move(ptz, request, timeout)def continuous_move(): mycam = ONVIFCamera('192.168.0.112', 80, 'admin', '12345') # Create media service object media = mycam.create_media_service() # Create ptz service object ptz = mycam.create_ptz_service() # Get target profile media_profile = media.GetProfiles()[0]; # Get PTZ configuration options for getting continuous move range request = ptz.create_type('GetConfigurationOptions') request.ConfigurationToken = media_profile.PTZConfiguration._token ptz_configuration_options = ptz.GetConfigurationOptions(request) request = ptz.create_type('ContinuousMove') request.ProfileToken = media_profile._token ptz.Stop({'ProfileToken': media_profile._token}) # Get range of pan and tilt # NOTE: X and Y are velocity vector global XMAX, XMIN, YMAX, YMIN XMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Max XMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Min YMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Max YMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Min # move right move_right(ptz, request) # move left move_left(ptz, request) # Move up move_up(ptz, request) # move down move_down(ptz, request)if __name__ == '__main__': continuous_move()
3、使用时可能的错误No such file: /usr/lib/python2.7/site-packages/wsdl/devicemgmt.wsdl
解决方法:
https://github.com/FalkTannhaeuser/python-onvif-zeep/issues/13
指定wsdl地址位置即可:
可以使用find命令查找wsdl位置即可:
find / -name *.wsdl
比如我这里pip安装onvif后默认wsdl在/usr/下: