You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

159 lines
4.3 KiB

  1. # On Ubuntu gstreamer interface is missing on OpenCV
  2. # On Fedora enable RPM fusion repo and install packages
  3. # dnf install python3-opencv gstreamer-plugins-bad-free gstreamer-plugins-ugly gstreamer1-plugins-bad-freeworld gstreamer1-plugins-good gstreamer1-libav python3-evdev
  4. from time import sleep
  5. import sys
  6. import socket
  7. import cv2
  8. import numpy as np
  9. import time
  10. from threading import Thread
  11. _, hostname = sys.argv
  12. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  13. sock.settimeout(0)
  14. sock.bind(("", 37021))
  15. #WIDTH, HEIGHT = 1920, 1080
  16. WIDTH, HEIGHT = 1280, 720
  17. WIDTH, HEIGHT = 640, 480
  18. #WIDTH, HEIGHT = 320, 240
  19. CROSSHAIR_THICKNESS = 3
  20. CROSSHAIR_SIZE = 50
  21. from glob import glob
  22. from evdev import InputDevice, categorize, ecodes
  23. state = dict()
  24. shoot = False
  25. robot_shoot_timestamp = 0
  26. class GamepadReader(Thread):
  27. def run(self):
  28. global shoot
  29. global robot_shoot_timestamp
  30. gamepad = InputDevice(glob("/dev/input/by-id/usb-Microsoft_Controller*event*")[0])
  31. print("Opening:", gamepad)
  32. for event in gamepad.read_loop():
  33. if not event:
  34. continue
  35. if event.type == 3:
  36. state["axis%d" % event.code] = event.value
  37. elif event.type == 1:
  38. state["button%d" % event.code] = event.value
  39. elif event.type == 0:
  40. pass # dno?
  41. else:
  42. print(event.type, event.value, event.code)
  43. if state.get("button310", 0) or state.get("button311", 0):
  44. shoot = True
  45. t = GamepadReader(daemon=True)
  46. t.start()
  47. print("Resolving hostname:", hostname)
  48. while True:
  49. try:
  50. sockaddr = socket.getaddrinfo(hostname, 37020)[0][-1]
  51. except OSError:
  52. sleep(0.5)
  53. else:
  54. break
  55. print("Sending let's play packet to", sockaddr)
  56. msg = "play:%d:%d" % (WIDTH, HEIGHT)
  57. sock.sendto(msg.encode("ascii"), sockaddr)
  58. pipeline = 'udpsrc port=5000 caps = "application/x-rtp, encoding-name=(string)H264" ! rtpjitterbuffer mode=3 ! rtph264depay ! decodebin ! videoconvert ! appsink'
  59. print("gst-launch-1.0 -v", pipeline.replace("appsink", "autovideosink"))
  60. sleep(5)
  61. cap = cv2.VideoCapture(pipeline)
  62. if not cap.isOpened():
  63. print("Didn't receive video feed")
  64. quit()
  65. red = np.zeros((HEIGHT,WIDTH,3), np.uint8)
  66. cv2.rectangle(red, (0, 0), (WIDTH, HEIGHT), (0, 0, 255), thickness=-1)
  67. cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
  68. cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
  69. blood = 1.0
  70. shoot_box_width = 0
  71. while True:
  72. ret, frame = cap.read()
  73. if ret == False:
  74. break
  75. # cv2.putText(frame, "yoyo",
  76. # (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 3)
  77. cv2.rectangle(frame, (WIDTH//2-CROSSHAIR_THICKNESS//2, HEIGHT//2-CROSSHAIR_SIZE//2), (WIDTH//2+CROSSHAIR_THICKNESS//2, HEIGHT//2+CROSSHAIR_SIZE//2), (0, 255, 0), thickness=-1)
  78. cv2.rectangle(frame, (WIDTH//2-CROSSHAIR_SIZE//2, HEIGHT//2-CROSSHAIR_THICKNESS//2), (WIDTH//2+CROSSHAIR_SIZE//2, HEIGHT//2+CROSSHAIR_THICKNESS//2), (0, 255, 0), thickness=-1)
  79. cv2.rectangle(frame, (10, HEIGHT-20), (int(WIDTH*0.30), HEIGHT-10),(0, 0, 0),thickness=-1)
  80. if blood > 0:
  81. blood -= 0.02
  82. frame = cv2.addWeighted( red, blood, frame, (1-blood), 0.0);
  83. msg = "speed:%d:%d" % (state.get("axis1", 0), state.get("axis3", 0))
  84. sock.sendto(msg.encode("ascii"), sockaddr)
  85. if shoot and time.time() - robot_shoot_timestamp >=1:
  86. msg = "shoot"
  87. robot_shoot_timestamp = time.time()
  88. shoot_box_width = 0
  89. #cv2.rectangle(frame, (WIDTH-10, HEIGHT-10), (40, 100),(0, 0, 0),thickness=-1)
  90. sock.sendto(msg.encode("ascii"), sockaddr)
  91. shoot = False
  92. else:
  93. shoot = False
  94. if time.time() - robot_shoot_timestamp <=1:
  95. shoot_box_width += int((WIDTH*0.30)/22)
  96. else:
  97. shoot_box_width = int(WIDTH*0.30)
  98. cv2.rectangle(frame, (10, HEIGHT-20), (shoot_box_width, HEIGHT-10),(0, 255, 0),thickness=-1)
  99. cv2.imshow("window",frame)
  100. if cv2.waitKey(1) & 0xFF == ord('q'):
  101. break
  102. try:
  103. #print("got:", sock.recv(100))
  104. data = sock.recv(100)
  105. cmd = data.decode("ascii")
  106. #print(cmd)
  107. if cmd == "hit":
  108. robot_hit_timestamp = time.time()
  109. blood = 2.2
  110. except BlockingIOError:
  111. pass
  112. #print("nuffing")