# note: https://github.com/FalkTannhaeuser/python-onvif-zeep # note: https://blog.csdn.net/zong596568821xp/article/details/89644654 from onvif import ONVIFCamera import zeep import time mycam = ONVIFCamera('192.168.30.130', 80, 'admin', '123456', '/home/packages/test/test_onvif/python-onvif-zeep-0.2.12/wsdl/') # Create media service object media = mycam.create_media_service() # Create ptz service object ptz = mycam.create_ptz_service() # Get target profile def zeep_pythonvalue(self, xmlvalue): return xmlvalue 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 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 def move_right(_ptz, _request): """向右持续移动n秒""" print('move right...') _request.Velocity.PanTilt.x = XMAX _request.Velocity.PanTilt.y = 0 # Start continuous move _ptz.ContinuousMove(_request) # Wait a certain time time.sleep(1) # Stop continuous move _ptz.Stop({'ProfileToken': _request.ProfileToken}) def move_left(_ptz, _request): """向左持续移动n秒""" print('move left...') _request.Velocity.PanTilt.x = XMIN _request.Velocity.PanTilt.y = 0 # Start continuous move _ptz.ContinuousMove(_request) # Wait a certain time time.sleep(0.5) # Stop continuous move _ptz.Stop({'ProfileToken': _request.ProfileToken}) def move_up(_ptz, _request): """向上持续移动n秒""" print('move up...') request.Velocity.PanTilt.x = 0 request.Velocity.PanTilt.y = YMAX # Start continuous move _ptz.ContinuousMove(_request) # Wait a certain time time.sleep(0.5) # Stop continuous move _ptz.Stop({'ProfileToken': _request.ProfileToken}) def move_down(_ptz, _request): """向下持续移动n秒""" print('move down...') request.Velocity.PanTilt.x = 0 request.Velocity.PanTilt.y = YMIN # Start continuous move _ptz.ContinuousMove(_request) # Wait a certain time time.sleep(0.5) # Stop continuous move _ptz.Stop({'ProfileToken': _request.ProfileToken}) # move_right(ptz, request) # move_left(ptz, request) # move_up(ptz, request) # move_down(ptz, request) def zoom_in(_ptz, _request): print('zoom in...') _request.Velocity.Zoom.x = 1 # Start continuous move _ptz.ContinuousMove(_request) # Wait a certain time time.sleep(1) # Stop continuous move _ptz.Stop({'ProfileToken': _request.ProfileToken}) def zoom_out(_ptz, _request): print('zoom out...') _request.Velocity.Zoom.x = -1 # Start continuous move _ptz.ContinuousMove(_request) # Wait a certain time time.sleep(1) # Stop continuous move _ptz.Stop({'ProfileToken': _request.ProfileToken}) zoom_in(ptz, request) # zoom_out(ptz, request)