continuous_move.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import asyncio, sys, os
  2. from onvif import ONVIFCamera
  3. IP="192.168.1.64" # Camera IP address
  4. PORT=80 # Port
  5. USER="admin" # Username
  6. PASS="intflow3121" # Password
  7. XMAX = 1
  8. XMIN = -1
  9. YMAX = 1
  10. YMIN = -1
  11. moverequest = None
  12. ptz = None
  13. active = False
  14. def do_move(ptz, request):
  15. # Start continuous move
  16. global active
  17. if active:
  18. ptz.Stop({'ProfileToken': request.ProfileToken})
  19. active = True
  20. ptz.ContinuousMove(request)
  21. def move_up(ptz, request):
  22. print ('move up...')
  23. request.Velocity.PanTilt.x = 0
  24. request.Velocity.PanTilt.y = YMAX
  25. do_move(ptz, request)
  26. def move_down(ptz, request):
  27. print ('move down...')
  28. request.Velocity.PanTilt.x = 0
  29. request.Velocity.PanTilt.y = YMIN
  30. do_move(ptz, request)
  31. def move_right(ptz, request):
  32. print ('move right...')
  33. request.Velocity.PanTilt.x = XMAX
  34. request.Velocity.PanTilt.y = 0
  35. do_move(ptz, request)
  36. def move_left(ptz, request):
  37. print ('move left...')
  38. request.Velocity.PanTilt.x = XMIN
  39. request.Velocity.PanTilt.y = 0
  40. do_move(ptz, request)
  41. def move_upleft(ptz, request):
  42. print ('move up left...')
  43. request.Velocity.PanTilt.x = XMIN
  44. request.Velocity.PanTilt.y = YMAX
  45. do_move(ptz, request)
  46. def move_upright(ptz, request):
  47. print ('move up left...')
  48. request.Velocity.PanTilt.x = XMAX
  49. request.Velocity.PanTilt.y = YMAX
  50. do_move(ptz, request)
  51. def move_downleft(ptz, request):
  52. print ('move down left...')
  53. request.Velocity.PanTilt.x = XMIN
  54. request.Velocity.PanTilt.y = YMIN
  55. do_move(ptz, request)
  56. def move_downright(ptz, request):
  57. print ('move down left...')
  58. request.Velocity.PanTilt.x = XMAX
  59. request.Velocity.PanTilt.y = YMIN
  60. do_move(ptz, request)
  61. def setup_move():
  62. mycam = ONVIFCamera(IP, PORT, USER, PASS)
  63. # Create media service object
  64. media = mycam.create_media_service()
  65. # Create ptz service object
  66. global ptz
  67. ptz = mycam.create_ptz_service()
  68. # Get target profile
  69. media_profile = media.GetProfiles()[0]
  70. # Get PTZ configuration options for getting continuous move range
  71. request = ptz.create_type('GetConfigurationOptions')
  72. request.ConfigurationToken = media_profile.PTZConfiguration.token
  73. ptz_configuration_options = ptz.GetConfigurationOptions(request)
  74. global moverequest
  75. moverequest = ptz.create_type('ContinuousMove')
  76. moverequest.ProfileToken = media_profile.token
  77. if moverequest.Velocity is None:
  78. moverequest.Velocity = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
  79. moverequest.Velocity.PanTilt.space = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].URI
  80. moverequest.Velocity.Zoom.space = ptz_configuration_options.Spaces.ContinuousZoomVelocitySpace[0].URI
  81. # Get range of pan and tilt
  82. # NOTE: X and Y are velocity vector
  83. global XMAX, XMIN, YMAX, YMIN
  84. XMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Max
  85. XMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Min
  86. YMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Max
  87. YMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Min
  88. def readin():
  89. """Reading from stdin and displaying menu"""
  90. global moverequest, ptz
  91. selection = sys.stdin.readline().strip("\n")
  92. lov=[ x for x in selection.split(" ") if x != ""]
  93. if lov:
  94. if lov[0].lower() in ["u","up"]:
  95. move_up(ptz,moverequest)
  96. elif lov[0].lower() in ["d","do","dow","down"]:
  97. move_down(ptz,moverequest)
  98. elif lov[0].lower() in ["l","le","lef","left"]:
  99. move_left(ptz,moverequest)
  100. elif lov[0].lower() in ["l","le","lef","left"]:
  101. move_left(ptz,moverequest)
  102. elif lov[0].lower() in ["r","ri","rig","righ","right"]:
  103. move_right(ptz,moverequest)
  104. elif lov[0].lower() in ["ul"]:
  105. move_upleft(ptz,moverequest)
  106. elif lov[0].lower() in ["ur"]:
  107. move_upright(ptz,moverequest)
  108. elif lov[0].lower() in ["dl"]:
  109. move_downleft(ptz,moverequest)
  110. elif lov[0].lower() in ["dr"]:
  111. move_downright(ptz,moverequest)
  112. elif lov[0].lower() in ["s","st","sto","stop"]:
  113. ptz.Stop({'ProfileToken': moverequest.ProfileToken})
  114. active = False
  115. else:
  116. print("What are you asking?\tI only know, 'up','down','left','right', 'ul' (up left), \n\t\t\t'ur' (up right), 'dl' (down left), 'dr' (down right) and 'stop'")
  117. print("")
  118. print("Your command: ", end='',flush=True)
  119. if __name__ == '__main__':
  120. setup_move()
  121. move_upleft(ptz,moverequest)
  122. #if os.name == 'nt':
  123. # loop = asyncio.ProactorEventLoop() # for subprocess' pipes on Windows
  124. # asyncio.set_event_loop(loop)
  125. #else:
  126. # loop = asyncio.get_event_loop()
  127. #try:
  128. # loop.add_reader(sys.stdin,readin)
  129. # print("Use Ctrl-C to quit")
  130. # print("Your command: ", end='',flush=True)
  131. # loop.run_forever()
  132. #except:
  133. # pass
  134. #finally:
  135. # loop.remove_reader(sys.stdin)
  136. # loop.close()