Reference resources: https://github.com/quatanium/python-onvif
In the onvif test tool for Haikang camera This article introduces how to use onvif test tool to debug Haikang camera and how to configure it.
This paper describes how to call the onvif protocol interface to control the cloud table rotation of Haikang camera. Most of the code queried is implemented using C++, which is a bit complex. This paper uses python interface directly to implement.
This article is implemented under python2. There are some problems with python3 installation and need to be debugged. Please look forward to it
1 Install python-onvif
1.1 pip Installation
The onvif package has been officially installed by python, so it can be installed directly from pip as follows
pip install onvif
Note that the pip must correspond to python2 here. If it corresponds to python3, you may have problems, so to avoid this problem, it is recommended to install from the following source code
1.2 Source Installation
git clone https://github.com/quatanium/python-onvif cd python-onvif python2 setup.py install
1.3 Installation Test
After successful installation, run python2, enter the following statement, and install successfully without error
from onvif import ONVIFCamera
2 Exercise Test
In the python-onvif installation package, there is an examples folder with a continuous_move.py file, coded as follows
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()
Modify the IP, username and password in the line 43 function to the parameters of your camera, such as the following for my camera
mycam = ONVIFCamera('192.168.170.249', 80, 'admin', 'jiaxun123')
Then run python2 continuous_move.py, and the camera cloud can move up and down to the right, respectively. With this code, we can write our own motion control program