参考:https://github.com/quatanium/python-onvif
在《海康相机之onvif测试工具使用》这篇文章中,介绍了利用onvif测试工具来调试海康相机,并且介绍了如何配置海康相机。
本文将介绍如何调用onvif协议的接口,来实现代码控制海康相机云台转动,查询的大多数代码是使用C++来实现,感觉有点复杂,本文直接采用python接口来实现。C版本的请查看《ONVIF协议开发之网络摄像头云台控制(C版)》
python官方已经提供了onvif包的安装,因此可直接通过pip来安装,如下
pip2 install onvif
要注意这里pip必须对应python2,如果对应是python3可能会有问题,因此为了避免这种问题,建议采用如下的源码安装
git clone https://github.com/quatanium/python-onvif
cd python-onvif
python2 setup.py install
安装成功后,运行python2,输入以下语句,不报错即安装成功
from onvif import ONVIFCamera
在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):
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()
将第43行函数里的IP、用户名、密码修改为自己相机的参数,比如我的相机参数如下
mycam = ONVIFCamera('192.168.170.*', 80, 'admin', '**')
然后运行python2 continuous_move.py,相机云台即可分别实现右左上下运动,参考此代码我们便可以编写我们自己的运动控制程序
============2019.8.15更新=============
前端时间经过测试,把Python3版本的调通了,安装方法如下
pip3 install --upgrade onvif_zeep
连续运动代码
from time import sleep
from onvif import ONVIFCamera
import zeep
XMAX = 1
XMIN = -1
YMAX = 1
YMIN = -1
def zeep_pythonvalue(self, xmlvalue):
return xmlvalue
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):
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.170.**', 80, 'admin', '**')
# Create media service object
media = mycam.create_media_service()
# Create ptz service object
ptz = mycam.create_ptz_service()
# Get target profile
zeep.xsd.simple.AnySimpleType.pythonvalue = zeep_pythonvalue
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})
if request.Velocity is None:
request.Velocity = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
request.Velocity = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
request.Velocity.PanTilt.space = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].URI
request.Velocity.Zoom.space = ptz_configuration_options.Spaces.ContinuousZoomVelocitySpace[0].URI
# 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()
仿照连续运动的实例,结合onvif协议,可以写绝对运动的控制程序
from time import sleep
from onvif import ONVIFCamera
import zeep
XMAX = 1
XMIN = -1
YMAX = 1
YMIN = -1
def zeep_pythonvalue(self, xmlvalue):
return xmlvalue
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 absolute_move():
pan = 0
pan_speed = 1
tilt = 0
tilt_speed = 1
zoom = 1
zoom_speed = 1
mycam = ONVIFCamera('192.168.170.**', 80, 'admin', '**')
# Create media service object
media = mycam.create_media_service()
# Create ptz service object
ptz = mycam.create_ptz_service()
# Get target profile
zeep.xsd.simple.AnySimpleType.pythonvalue = zeep_pythonvalue
media_profile = media.GetProfiles()[0]
# Get PTZ configuration options for getting absolute move range
request = ptz.create_type('GetConfigurationOptions')
request.ConfigurationToken = media_profile.PTZConfiguration.token
# ptz_configuration_options = ptz.GetConfigurationOptions(request)
request = ptz.create_type('AbsoluteMove')
request.ProfileToken = media_profile.token
ptz.Stop({'ProfileToken': media_profile.token})
if request.Position is None:
request.Position = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
if request.Speed is None:
request.Speed = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
request.Position.PanTilt.x = pan
request.Speed.PanTilt.x = pan_speed
request.Position.PanTilt.y = tilt
request.Speed.PanTilt.y = tilt_speed
request.Position.Zoom = zoom
request.Speed.Zoom = zoom_speed
ptz.AbsoluteMove(request)
print('finish')
if __name__ == '__main__':
absolute_move()