AbsoluteMove.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import asyncio, sys, os
  2. from onvif import ONVIFCamera
  3. import time
  4. IP="192.168.1.64" # Camera IP address
  5. PORT=80 # Port
  6. USER="admin" # Username
  7. PASS="intflow3121" # Password
  8. XMAX = 1
  9. XMIN = -1
  10. XNOW = 0.5
  11. YMAX = 1
  12. YMIN = -1
  13. YNOW = 0.5
  14. Move = 0.1
  15. Velocity = 1
  16. Zoom = 0
  17. positionrequest = None
  18. ptz = None
  19. active = False
  20. ptz_configuration_options = None
  21. media_profile = None
  22. def do_move(ptz, request):
  23. global active
  24. if active:
  25. ptz.Stop({'ProfileToken': request.ProfileToken})
  26. active = True
  27. ptz.AbsoluteMove(request)
  28. def move_up(ptz, request):
  29. if YNOW - Move <= -1:
  30. request.Position.PanTilt.y = YNOW
  31. else:
  32. request.Position.PanTilt.y = YNOW - Move
  33. do_move(ptz, request)
  34. def move_down(ptz, request):
  35. if YNOW + Move >= 1:
  36. request.Position.PanTilt.y = YNOW
  37. else:
  38. request.Position.PanTilt.y = YNOW + Move
  39. do_move(ptz, request)
  40. def move_right(ptz, request):
  41. if XNOW - Move >= -0.99:
  42. request.Position.PanTilt.x = XNOW - Move
  43. elif abs(XNOW + Move) >= 0.0:
  44. request.Position.PanTilt.x = abs(XNOW) - Move
  45. elif abs(XNOW) <= 0.01:
  46. request.Position.PanTilt.x = XNOW
  47. request.Position.PanTilt.y = YNOW
  48. do_move(ptz, request)
  49. def move_left(ptz, request):
  50. if XNOW + Move <= 1.0:
  51. request.Position.PanTilt.x = XNOW + Move
  52. elif XNOW <= 1.0 and XNOW > 0.99:
  53. request.Position.PanTilt.x = -XNOW
  54. elif XNOW < 0:
  55. request.Position.PanTilt.x = XNOW + Move
  56. elif XNOW <= -0.105556 and XNOW > -0.11:
  57. request.Position.PanTilt.x = XNOW
  58. request.Position.PanTilt.y = YNOW
  59. do_move(ptz, request)
  60. def move_upleft(ptz, request):
  61. if YNOW == -1:
  62. request.Position.PanTilt.y = YNOW
  63. else:
  64. request.Position.PanTilt.y = YNOW - Move
  65. if XNOW + Move <= 1.0:
  66. request.Position.PanTilt.x = XNOW + Move
  67. elif XNOW <= 1.0 and XNOW > 0.99:
  68. request.Position.PanTilt.x = -XNOW
  69. elif XNOW < 0:
  70. request.Position.PanTilt.x = XNOW + Move
  71. elif XNOW <= -0.105556 and XNOW > -0.11:
  72. request.Position.PanTilt.x = XNOW
  73. do_move(ptz, request)
  74. def move_upright(ptz, request):
  75. if YNOW == -1:
  76. request.Position.PanTilt.y = YNOW
  77. else:
  78. request.Position.PanTilt.y = YNOW - Move
  79. if XNOW - Move >= -0.99:
  80. request.Position.PanTilt.x = XNOW - Move
  81. elif abs(XNOW + Move) >= 0.0:
  82. request.Position.PanTilt.x = abs(XNOW) - Move
  83. elif abs(XNOW) <= 0.01:
  84. request.Position.PanTilt.x = XNOW
  85. do_move(ptz, request)
  86. def move_downleft(ptz, request):
  87. if YNOW - Move == 1:
  88. request.Position.PanTilt.y = YNOW
  89. else:
  90. request.Position.PanTilt.y = YNOW - Move
  91. if XNOW + Move <= 1.0:
  92. request.Position.PanTilt.x = XNOW + Move
  93. elif XNOW <= 1.0 and XNOW > 0.99:
  94. request.Position.PanTilt.x = -XNOW
  95. elif XNOW < 0:
  96. request.Position.PanTilt.x = XNOW + Move
  97. elif XNOW <= -0.105556 and XNOW > -0.11:
  98. request.Position.PanTilt.x = XNOW
  99. do_move(ptz, request)
  100. def move_downright(ptz, request):
  101. if YNOW == -1:
  102. request.Position.PanTilt.y = YNOW
  103. else:
  104. request.Position.PanTilt.y = YNOW - Move
  105. if XNOW - Move >= -0.99:
  106. request.Position.PanTilt.x = XNOW - Move
  107. elif abs(XNOW + Move) >= 0.0:
  108. request.Position.PanTilt.x = abs(XNOW) - Move
  109. elif abs(XNOW) <= 0.01:
  110. request.Position.PanTilt.x = XNOW
  111. do_move(ptz, request)
  112. def Zoom_in(ptz,request):
  113. if Zoom + Move >= 1.0:
  114. request.Position.Zoom = 1.0
  115. else:
  116. request.Position.Zoom = Zoom + Move
  117. do_move(ptz, request)
  118. def Zoom_out(ptz,request):
  119. if Zoom - Move <= 0.0:
  120. request.Position.Zoom = 0.0
  121. else:
  122. request.Position.Zoom = Zoom - Move
  123. do_move(ptz,request)
  124. def setup_move():
  125. mycam = ONVIFCamera(IP, PORT, USER, PASS)
  126. # Create media service object
  127. media = mycam.create_media_service()
  128. # Create ptz service object
  129. global ptz , ptz_configuration_options, media_profile
  130. ptz = mycam.create_ptz_service()
  131. # Get target profile
  132. media_profile = media.GetProfiles()[0]
  133. request = ptz.create_type('GetConfigurationOptions')
  134. request.ConfigurationToken = media_profile.PTZConfiguration.token
  135. ptz_configuration_options = ptz.GetConfigurationOptions(request)
  136. request_configuration = ptz.create_type('GetConfiguration')
  137. request_configuration.PTZConfigurationToken = media_profile.PTZConfiguration.token
  138. ptz_configuration = ptz.GetConfiguration(request_configuration)
  139. request_setconfiguration = ptz.create_type('SetConfiguration')
  140. request_setconfiguration.PTZConfiguration = ptz_configuration
  141. global positionrequest
  142. positionrequest = ptz.create_type('AbsoluteMove')
  143. positionrequest.ProfileToken = media_profile.token
  144. if positionrequest.Position is None :
  145. positionrequest.Position = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
  146. positionrequest.Position.PanTilt.space = ptz_configuration_options.Spaces.AbsolutePanTiltPositionSpace[0].URI
  147. positionrequest.Position.Zoom.space = ptz_configuration_options.Spaces.AbsoluteZoomPositionSpace[0].URI
  148. if positionrequest.Speed is None :
  149. positionrequest.Speed = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
  150. positionrequest.Speed.PanTilt.space = ptz_configuration_options.Spaces.PanTiltSpeedSpace[0].URI
  151. def Get_Status():
  152. # Get range of pan and tilt
  153. global XMAX, XMIN, YMAX, YMIN, XNOW, YNOW, Velocity, Zoom
  154. XMAX = ptz_configuration_options.Spaces.AbsolutePanTiltPositionSpace[0].XRange.Max
  155. XMIN = ptz_configuration_options.Spaces.AbsolutePanTiltPositionSpace[0].XRange.Min
  156. YMAX = ptz_configuration_options.Spaces.AbsolutePanTiltPositionSpace[0].YRange.Max
  157. YMIN = ptz_configuration_options.Spaces.AbsolutePanTiltPositionSpace[0].YRange.Min
  158. XNOW = ptz.GetStatus({'ProfileToken': media_profile.token}).Position.PanTilt.x
  159. YNOW = ptz.GetStatus({'ProfileToken': media_profile.token}).Position.PanTilt.y
  160. Velocity = ptz_configuration_options.Spaces.PanTiltSpeedSpace[0].XRange.Max
  161. Zoom = ptz.GetStatus({'ProfileToken': media_profile.token}).Position.Zoom.x
  162. def readin():
  163. """Reading from stdin and displaying menu"""
  164. global positionrequest, ptz
  165. selection = sys.stdin.readline().strip("\n")
  166. lov=[ x for x in selection.split(" ") if x != ""]
  167. if lov:
  168. if lov[0].lower() in ["u","up"]:
  169. move_up(ptz,positionrequest)
  170. elif lov[0].lower() in ["d","do","dow","down"]:
  171. move_down(ptz,positionrequest)
  172. elif lov[0].lower() in ["l","le","lef","left"]:
  173. move_left(ptz,positionrequest)
  174. elif lov[0].lower() in ["l","le","lef","left"]:
  175. move_left(ptz,positionrequest)
  176. elif lov[0].lower() in ["r","ri","rig","righ","right"]:
  177. move_right(ptz,positionrequest)
  178. elif lov[0].lower() in ["ul"]:
  179. move_upleft(ptz,positionrequest)
  180. elif lov[0].lower() in ["ur"]:
  181. move_upright(ptz,positionrequest)
  182. elif lov[0].lower() in ["dl"]:
  183. move_downleft(ptz,positionrequest)
  184. elif lov[0].lower() in ["dr"]:
  185. move_downright(ptz,positionrequest)
  186. elif lov[0].lower() in ["s","st","sto","stop"]:
  187. ptz.Stop({'ProfileToken': positionrequest.ProfileToken})
  188. active = False
  189. else:
  190. 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'")
  191. print("")
  192. print("Your command: ", end='',flush=True)
  193. # Test Define
  194. # def move(ptz, request):
  195. # request.Position.PanTilt.y = -1
  196. # request.Position.PanTilt.x = 0
  197. # do_move(ptz,request)
  198. if __name__ == '__main__':
  199. setup_move()
  200. # Get_Status()
  201. # Zoom_out(ptz,positionrequest)
  202. # Get_Status()
  203. # move(ptz,positionrequest)
  204. while True:
  205. if active == True:
  206. time.sleep(1)
  207. active = False
  208. else:
  209. Get_Status()
  210. move_up(ptz, positionrequest)