ControlCenter.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import cv2 as cv
  2. import io
  3. import logging
  4. import socketserver
  5. from threading import Condition
  6. from threading import Thread
  7. from http import server
  8. import time
  9. import traceback
  10. import socket
  11. import os
  12. from urllib.parse import urlparse
  13. from Tank import Tank
  14. class StreamingHandler(server.BaseHTTPRequestHandler):
  15. def do_GET(self):
  16. # 摄像头
  17. if self.path.startswith('/camera'):
  18. self.send_response(200)
  19. self.send_header('Age', 0)
  20. self.send_header('Cache-Control', 'no-cache, private')
  21. self.send_header('Pragma', 'no-cache')
  22. self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
  23. self.end_headers()
  24. ltime=0
  25. try:
  26. while True:
  27. self.wfile.write(b'--FRAME\r\n')
  28. self.send_header('Content-Type', 'image/jpeg')
  29. ret, deal_frame = cap.read()
  30. deal_frame = cv.flip(deal_frame, -1)
  31. r, show_frame = cv.imencode('.jpg', deal_frame)
  32. self.send_header('Content-Length', len(show_frame))
  33. self.end_headers()
  34. self.wfile.write(bytearray(show_frame))
  35. self.wfile.write(b'\r\n')
  36. except Exception as e:
  37. logging.warning(
  38. 'Removed streaming client %s: %s',
  39. self.client_address, str(e))
  40. traceback.print_exc()
  41. # 坦克控制
  42. elif self.path.startswith('/control'):
  43. parsed_result=urlparse(self.path)
  44. command = parsed_result.query
  45. print(command)
  46. # 解析命令
  47. orders = command.split('=')
  48. ord = orders[0]
  49. value = orders[1]
  50. print(value)
  51. print('{0} = {1}'.format(ord,value))
  52. if ord=='direct':
  53. if value=='forward':
  54. tank.forward()
  55. elif value=='backward':
  56. tank.backward()
  57. elif value=='left':
  58. tank.left()
  59. elif value=='right':
  60. tank.right()
  61. elif value=='stop':
  62. tank.stop()
  63. elif ord=='speed':
  64. speed=int(value)
  65. tank.setSpeed(speed)
  66. self.send_response(200)
  67. self.send_header('Content-type', 'text/html')
  68. self.send_header("test", "This is test!")
  69. self.end_headers()
  70. self.wfile.write(b'success')
  71. else:
  72. self.send_error(404)
  73. self.end_headers()
  74. class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
  75. #添加ipv6的支持
  76. address_family = socket.AF_INET6
  77. allow_reuse_address = True
  78. daemon_threads = True
  79. def run(port = 8080):
  80. global cap, server, tank
  81. tank = Tank()
  82. # tank.setSpeed(30)
  83. cap = cv.VideoCapture(0)
  84. cap.set(cv.CAP_PROP_FRAME_WIDTH, 1280)
  85. cap.set(cv.CAP_PROP_FRAME_HEIGHT, 720)
  86. try:
  87. address = ('', port)
  88. server = StreamingServer(address, StreamingHandler)
  89. server.serve_forever()
  90. except Exception as e:
  91. traceback.print_exc()
  92. finally:
  93. cap.release()
  94. def stop():
  95. global server
  96. server.shutdown()
  97. server.server_close()
  98. run()