api.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # note: https://github.com/FalkTannhaeuser/python-onvif-zeep
  2. # note: https://blog.csdn.net/zong596568821xp/article/details/89644654
  3. from onvif import ONVIFCamera
  4. import zeep
  5. import time
  6. mycam = ONVIFCamera('192.168.30.130', 80, 'admin', '123456',
  7. '/home/packages/test/test_onvif/python-onvif-zeep-0.2.12/wsdl/')
  8. # Create media service object
  9. media = mycam.create_media_service()
  10. # Create ptz service object
  11. ptz = mycam.create_ptz_service()
  12. # Get target profile
  13. def zeep_pythonvalue(self, xmlvalue):
  14. return xmlvalue
  15. zeep.xsd.simple.AnySimpleType.pythonvalue = zeep_pythonvalue
  16. media_profile = media.GetProfiles()[0]
  17. # Get PTZ configuration options for getting continuous move range
  18. request = ptz.create_type('GetConfigurationOptions')
  19. request.ConfigurationToken = media_profile.PTZConfiguration.token
  20. ptz_configuration_options = ptz.GetConfigurationOptions(request)
  21. request = ptz.create_type('ContinuousMove')
  22. request.ProfileToken = media_profile.token
  23. ptz.Stop({'ProfileToken': media_profile.token})
  24. if request.Velocity is None:
  25. request.Velocity = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
  26. request.Velocity = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
  27. request.Velocity.PanTilt.space = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].URI
  28. request.Velocity.Zoom.space = ptz_configuration_options.Spaces.ContinuousZoomVelocitySpace[0].URI
  29. XMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Max
  30. XMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Min
  31. YMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Max
  32. YMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Min
  33. def move_right(_ptz, _request):
  34. """向右持续移动n秒"""
  35. print('move right...')
  36. _request.Velocity.PanTilt.x = XMAX
  37. _request.Velocity.PanTilt.y = 0
  38. # Start continuous move
  39. _ptz.ContinuousMove(_request)
  40. # Wait a certain time
  41. time.sleep(1)
  42. # Stop continuous move
  43. _ptz.Stop({'ProfileToken': _request.ProfileToken})
  44. def move_left(_ptz, _request):
  45. """向左持续移动n秒"""
  46. print('move left...')
  47. _request.Velocity.PanTilt.x = XMIN
  48. _request.Velocity.PanTilt.y = 0
  49. # Start continuous move
  50. _ptz.ContinuousMove(_request)
  51. # Wait a certain time
  52. time.sleep(0.5)
  53. # Stop continuous move
  54. _ptz.Stop({'ProfileToken': _request.ProfileToken})
  55. def move_up(_ptz, _request):
  56. """向上持续移动n秒"""
  57. print('move up...')
  58. request.Velocity.PanTilt.x = 0
  59. request.Velocity.PanTilt.y = YMAX
  60. # Start continuous move
  61. _ptz.ContinuousMove(_request)
  62. # Wait a certain time
  63. time.sleep(0.5)
  64. # Stop continuous move
  65. _ptz.Stop({'ProfileToken': _request.ProfileToken})
  66. def move_down(_ptz, _request):
  67. """向下持续移动n秒"""
  68. print('move down...')
  69. request.Velocity.PanTilt.x = 0
  70. request.Velocity.PanTilt.y = YMIN
  71. # Start continuous move
  72. _ptz.ContinuousMove(_request)
  73. # Wait a certain time
  74. time.sleep(0.5)
  75. # Stop continuous move
  76. _ptz.Stop({'ProfileToken': _request.ProfileToken})
  77. # move_right(ptz, request)
  78. # move_left(ptz, request)
  79. # move_up(ptz, request)
  80. # move_down(ptz, request)
  81. def zoom_in(_ptz, _request):
  82. print('zoom in...')
  83. _request.Velocity.Zoom.x = 1
  84. # Start continuous move
  85. _ptz.ContinuousMove(_request)
  86. # Wait a certain time
  87. time.sleep(1)
  88. # Stop continuous move
  89. _ptz.Stop({'ProfileToken': _request.ProfileToken})
  90. def zoom_out(_ptz, _request):
  91. print('zoom out...')
  92. _request.Velocity.Zoom.x = -1
  93. # Start continuous move
  94. _ptz.ContinuousMove(_request)
  95. # Wait a certain time
  96. time.sleep(1)
  97. # Stop continuous move
  98. _ptz.Stop({'ProfileToken': _request.ProfileToken})
  99. zoom_in(ptz, request)
  100. # zoom_out(ptz, request)