pythonsoapwsdlonvifzeep

How to execute ContinuousMove with python zeep for onvif camera?


I was trying to execute onvif commands using python3 zeep package and got most of the commands working. But as I'm new and not familiar with onvif ( especially PTZ ), I couldn't get it to work yet. I'll provide the code I tried:

import time
from zeep import Client
from zeep.wsse.username import UsernameToken

ptz_wsdl = "D:\Projects\camera\zeep\wsdl\ptz.wsdl"
ip = "192.168.1.12"
port = "2020"
username = "onviftest"
password = "onviftest"

xaddr = "http://"+ip+":"+port+"/onvif/ptz_service"

ptz_client = Client(wsdl=ptz_wsdl, wsse=UsernameToken(username, password, use_digest=True))
ptz = ptz_client.create_service("{http://www.onvif.org/ver20/ptz/wsdl}PTZBinding", xaddr)

token = ptz.GetConfigurations()[0]["token"]
node = ptz.GetNodes()[0]

move = ptz.ContinuousMove(token, velocity)

time.sleep(5)

print(move)

And the GetNodes() output:

{
    'Name': 'PTZNODE',
    'SupportedPTZSpaces': {
        'AbsolutePanTiltPositionSpace': [
            {
                'URI': 'http://www.onvif.org/ver10/tptz/PanTiltSpaces/PositionGenericSpace',
                'XRange': {
                    'Min': -170.0,
                    'Max': 170.0
                },
                'YRange': {
                    'Min': -32.0,
                    'Max': 35.0
                }
            }
        ],
        'AbsoluteZoomPositionSpace': [],
        'RelativePanTiltTranslationSpace': [
            {
                'URI': 'http://www.onvif.org/ver10/tptz/PanTiltSpaces/TranslationGenericSpace',
                'XRange': {
                    'Min': -170.0,
                    'Max': 170.0
                },
                'YRange': {
                    'Min': -32.0,
                    'Max': 35.0
                }
            }
        ],
        'RelativeZoomTranslationSpace': [],
        'ContinuousPanTiltVelocitySpace': [
            {
                'URI': 'http://www.onvif.org/ver10/tptz/PanTiltSpaces/VelocityGenericSpace',
                'XRange': {
                    'Min': -1.0,
                    'Max': 1.0
                },
                'YRange': {
                    'Min': -1.0,
                    'Max': 1.0
                }
            }
        ],
        'ContinuousZoomVelocitySpace': [],
        'PanTiltSpeedSpace': [
            {
                'URI': 'http://www.onvif.org/ver10/tptz/PanTiltSpaces/GenericSpeedSpace',
                'XRange': {
                    'Min': 0.0,
                    'Max': 0.0
                }
            }
        ],
        'ZoomSpeedSpace': [],
        'Extension': None,
        '_attr_1': None
    },
    'MaximumNumberOfPresets': 8,
    'HomeSupported': True,
    'AuxiliaryCommands': [],
    'Extension': None,
    'token': 'PTZNODETOKEN',
    'FixedHomePosition': None,
    '_attr_1': {
}
}

As you can see Continuous move is supported for this camera... My code is incomplete (with velocity object which needs x, y and space) as I tried various ways to create velocity object, it always ends up with some error. What can be added to the code to make this work? [My python may not be good as im still learning as i know only javascript but right now obligated to python for the onvif command due to the ST board I have]

Any help is appreciated :)

UPDATE: I tried the below code, no movement in camera and res gives None.

from zeep import Client
from zeep.wsse.username import UsernameToken

ptz_wsdl = "D:\Projects\camera\zeep\wsdl\ptz.wsdl"
media_wsdl = "D:\Projects\camera\zeep\wsdl\media.wsdl"

ip = "192.168.1.12"
port = "2020"
username = "onviftest"
password = "onviftest"

xaddr = "http://"+ip+":"+port+"/onvif/ptz_service"

media_client = Client(wsdl=media_wsdl, wsse=UsernameToken(username, password, use_digest=True))
media = media_client.create_service("{http://www.onvif.org/ver10/media/wsdl}MediaBinding", xaddr)

ptz_client = Client(wsdl=ptz_wsdl, wsse=UsernameToken(username, password, use_digest=True))
ptz = ptz_client.create_service("{http://www.onvif.org/ver20/ptz/wsdl}PTZBinding", xaddr)

profile = media.GetProfiles()[0]

ptz_config = ptz.GetConfigurationOptions(profile.PTZConfiguration.token)

velocity = {
    "PanTilt": {
        "x": ptz_config.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Max,
        "y": ptz_config.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Max,
        "space": ptz_config.Spaces.ContinuousPanTiltVelocitySpace[0].URI,
    },
    # "Zoom": None
}

res = ptz.ContinuousMove(profile.token, velocity )
print(res)

Solution

  • After multiple attempts, I was able to get it to work:

    from zeep import Client
    from zeep.wsse.username import UsernameToken
    
    ptz_wsdl = "D:\Projects\camera\zeep\wsdl\ptz.wsdl"
    media_wsdl = "D:\Projects\camera\zeep\wsdl\media.wsdl"
    
    ip = "192.168.1.12"
    port = "2020"
    username = "onviftest"
    password = "onviftest"
    
    xaddr = "http://"+ip+":"+port+"/onvif/ptz_service"
    
    media_client = Client(wsdl=media_wsdl, wsse=UsernameToken(
        username, password, use_digest=True))
    media = media_client.create_service(
        "{http://www.onvif.org/ver10/media/wsdl}MediaBinding", xaddr)
    
    ptz_client = Client(wsdl=ptz_wsdl, wsse=UsernameToken(
        username, password, use_digest=True))
    ptz = ptz_client.create_service(
        "{http://www.onvif.org/ver20/ptz/wsdl}PTZBinding", xaddr)
    
    profile = media.GetProfiles()[0]
    
    ptz_config = ptz.GetConfigurationOptions(profile.PTZConfiguration.token)
    
    velocity = {
        "PanTilt": {
            "x": ptz_config.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Min,
            "y": ptz_config.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Min,
        },
    }
    
    ptz.ContinuousMove(profile.token, Velocity=velocity)