python-onvif 实现客户端控制相机云台

本文将介绍如何调用onvif协议的接口,来实现代码控制海康相机云台转动,查询的大多数代码是使用C++来实现,感觉有点复杂,本文直接采用python接口来实现。

1、安装python-onvif

1.1 源码安装

终端输入以下代码:

$ git clone https://github.com/quatanium/python-onvif
$ cd python-onvif
$ sudo python setup.py install

python-onvif 实现客户端控制相机云台_第1张图片

1.2 安装测试

安装成功后,运行python2,输入以下语句,不报错即安装成功

from onvif import ONVIFCamera

2、 运动测试

在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.0.141', 80, 'admin', '**')

首先:

$ sudo apt-get install python-pygame

然后运行

$ python continuous_move.py

相机云台即可分别实现右左上下运动,参考此代码我们便可以编写我们自己的运动控制程序。


本文为本人笔记之用。
参考原链接:https://blog.csdn.net/zong596568821xp/article/details/89644654

你可能感兴趣的:(技术总结)