Collect.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # 这段程序是用来采集图像并分类成三个文件夹的
  2. import cv2
  3. from urllib import request
  4. import time
  5. import pygame
  6. import threading
  7. cap = cv2.VideoCapture('http://192.168.31.100:8080/camera')
  8. collecttime = time.time()
  9. status = 'stop'
  10. index = 0
  11. done = False
  12. speed = 20
  13. # 控制子线程
  14. def changeMotion(tank_state, tank_direct):
  15. global status
  16. if tank_state == 1 and tank_direct == 0:
  17. print("forward")
  18. print('send request')
  19. req = request.urlopen(
  20. 'http://192.168.31.100:8080/control?direct=forward')
  21. res = req.read()
  22. print(res)
  23. status = 'forward'
  24. elif tank_state == -1 and tank_direct == 0:
  25. print("backward")
  26. print('send request')
  27. req = request.urlopen(
  28. 'http://192.168.31.100:8080/control?direct=backward')
  29. res = req.read()
  30. print(res)
  31. status = 'stop'
  32. elif tank_state == 0:
  33. print("stop")
  34. print('send request')
  35. req = request.urlopen('http://192.168.31.100:8080/control?direct=stop')
  36. res = req.read()
  37. print(res)
  38. status = 'stop'
  39. elif tank_state == 1 and tank_direct == -1:
  40. print("left")
  41. print('send request')
  42. req = request.urlopen('http://192.168.31.100:8080/control?direct=left')
  43. res = req.read()
  44. print(res)
  45. status = 'left'
  46. elif tank_state == 1 and tank_direct == 1:
  47. print("right")
  48. print('send request')
  49. req = request.urlopen(
  50. 'http://192.168.31.100:8080/control?direct=right')
  51. res = req.read()
  52. print(res)
  53. status = 'right'
  54. elif tank_state == -1 and tank_direct == -1:
  55. print("right")
  56. print('send request')
  57. req = request.urlopen(
  58. 'http://192.168.31.100:8080/control?direct=right')
  59. res = req.read()
  60. print(res)
  61. status = 'right'
  62. elif tank_state == 1 and tank_direct == 1:
  63. print("left")
  64. print('send request')
  65. req = request.urlopen('http://192.168.31.100:8080/control?direct=left')
  66. res = req.read()
  67. print(res)
  68. status = 'left'
  69. def control():
  70. supportControllers = ['Xbox One S Controller']
  71. forword_button = [(4, 6)]
  72. backword_button = [(5, 7)]
  73. start_button = [(7, 11)]
  74. # 模块初始化
  75. pygame.init()
  76. pygame.joystick.init()
  77. # 查找支持的手柄
  78. number = pygame.joystick.get_count()
  79. for i in range(0, number):
  80. j = pygame.joystick.Joystick(i)
  81. name = j.get_name()
  82. if name in supportControllers:
  83. print(name)
  84. search_index=supportControllers.index(name)
  85. print(search_index)
  86. f_b = forword_button[search_index]
  87. b_b = backword_button[search_index]
  88. s_b = start_button[search_index]
  89. joystick=pygame.joystick.Joystick(0)
  90. joystick.init()
  91. buttonCount = joystick.get_numbuttons()
  92. print(buttonCount)
  93. # XBox S手柄在蓝牙连接和USB连接时按钮数量不一样
  94. if len(f_b) > 1:
  95. if buttonCount == 11:
  96. b6_index = f_b[0]
  97. b7_index = b_b[0]
  98. b11_index = s_b[0]
  99. elif buttonCount == 17:
  100. b6_index = f_b[1]
  101. b7_index = b_b[1]
  102. b11_index = s_b[1]
  103. clock = pygame.time.Clock()
  104. # 0停止,1前,-1后
  105. tank_state = 0
  106. # 0无,-1左,1右
  107. tank_direct = 0
  108. while not done:
  109. for event_ in pygame.event.get():
  110. if event_.type == pygame.JOYHATMOTION:
  111. hat = joystick.get_hat(0)
  112. # 获取方向键状态
  113. tank_direct = hat[0]
  114. print("方向键:"+str(tank_direct))
  115. changeMotion(tank_state, tank_direct)
  116. elif event_.type == pygame.JOYBUTTONDOWN or event_.type == pygame.JOYBUTTONUP:
  117. b6 = joystick.get_button(b6_index)
  118. b7 = joystick.get_button(b7_index)
  119. b11 = joystick.get_button(b11_index)
  120. print(str(b6)+" "+str(b7))
  121. if b11 == 1:
  122. #设置初始速度
  123. print('send request')
  124. req = request.urlopen(
  125. 'http://192.168.31.100:8080/control?speed='+str(speed))
  126. res = req.read()
  127. print(res)
  128. if b6 == 1:
  129. tank_state = -1
  130. changeMotion(tank_state, tank_direct)
  131. if b7 == 1:
  132. tank_state = 1
  133. changeMotion(tank_state, tank_direct)
  134. if b6+b7 == 0:
  135. tank_state = 0
  136. changeMotion(tank_state, tank_direct)
  137. pygame.quit()
  138. thread = threading.Thread(target=control)
  139. thread.start()
  140. # 显示主线程
  141. while True:
  142. ret, frame = cap.read()
  143. cv2.imshow("capture", frame)
  144. currenttime = time.time()
  145. if currenttime-collecttime > 2:
  146. if status == 'forward' or status == 'left' or status == 'right':
  147. cv2.imwrite('/home/xiongweixp/Documents/collect/' +
  148. status+'/'+str(index)+'.jpg', frame)
  149. index = index+1
  150. collecttime = currenttime
  151. key = cv2.waitKey(1)
  152. if key == ord('q'):
  153. done = True
  154. break