参考:https://github.com/quatanium/python-onvif
在《海康相机之onvif测试工具使用》这篇文章中,介绍了利用onvif测试工具来调试海康相机,并且介绍了如何配置海康相机。
本文将介绍如何调用onvif协议的接口,来实现代码控制海康相机云台转动,查询的大多数代码是使用C++来实现,感觉有点复杂,本文直接采用python接口来实现。C版本的请查看《ONVIF协议开发之网络摄像头云台控制(C版)》
1 安装python-onvif
1.1 pip安装
python官方已经提供了onvif包的安装,因此可直接通过pip来安装,如下
pip2 install onvif
要注意这里pip必须对应python2,如果对应是python3可能会有问题,因此为了避免这种问题,建议采用如下的源码安装
1.2 源码安装
git clone https://github.com/quatanium/python-onvif
cd python-onvif
python2 setup.py install
1.3 安装测试
安装成功后,运行python2,输入以下语句,不报错即安装成功
from onvif import ONVIFCamera
2 连续运动(python2版本)
在python-onvif安装包中,有examples文件夹,其中有个continuous_move.py文件,代码如下
from time import sleep
from onvif import ONVIFCamera
XMAX = 1
XMIN = -1
YMAX = 1
YMIN = -1
def 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):