Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.24 KB | None | 0 0
  1. # coding: utf8
  2.  
  3. import cv2
  4. import rospy
  5. import numpy as np
  6. from clever import srv
  7. from pyzbar import pyzbar
  8. from cv_bridge import CvBridge
  9. from sensor_msgs.msg import Image
  10. from clever.srv import SetLEDEffect
  11. from std_srvs.srv import Trigger
  12.  
  13.  
  14. # Определяет, есть ли цвет на фотографии
  15. def findcol(lower_color, upper_color, color_name):
  16.     # Take each frame
  17.     _, frame = cap.read()
  18.  
  19.     # Convert BGR to HSV
  20.     hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  21.  
  22.     mask = cv2.inRange(hsv, lower_color, upper_color)
  23.     res = []
  24.     # Bitwise-AND mask and original image
  25.     cv2.bitwise_and(frame, mask, res)
  26.     result = ""
  27.     for i in res:
  28.         if i == 1:
  29.             result = color_name
  30.             break
  31.     return result
  32.  
  33.  
  34. # Заполняет массив и помечает потенциально зараженных
  35. def allcolor():
  36.     if findcol(hsv_mingreen, hsv_maxgreen, '-') == '-':
  37.         people.append('-')
  38.     elif findcol(lower_red, upper_red, '+') == '+':
  39.         people.append("+")
  40.         set_effect(r=128, g=0, b=128)  # пометка зараженного
  41.     elif findcol(lower_yellow, upper_yellow, '?') == '?':
  42.         people.append('?')
  43.         set_effect(r=128, g=0, b=128)  # пометка зараженного
  44.  
  45.  
  46. rospy.init_node('flight')
  47.  
  48. # Настройка телеметрии и объявление переменных
  49. get_telemetry = rospy.ServiceProxy('get_telemetry', srv.GetTelemetry)
  50. navigate = rospy.ServiceProxy('navigate', srv.Navigate)
  51. navigate_global = rospy.ServiceProxy('navigate_global', srv.NavigateGlobal)
  52. set_position = rospy.ServiceProxy('set_position', srv.SetPosition)
  53. set_velocity = rospy.ServiceProxy('set_velocity', srv.SetVelocity)
  54. set_attitude = rospy.ServiceProxy('set_attitude', srv.SetAttitude)
  55. set_rates = rospy.ServiceProxy('set_rates', srv.SetRates)
  56. land = rospy.ServiceProxy('land', Trigger)
  57. cap = cv2.VideoCapture(0)
  58. hsv_mingreen = np.array((53, 0, 0), np.uint8)
  59. hsv_maxgreen = np.array((83, 255, 255), np.uint8)
  60. lower_red = np.array([0, 70, 50])
  61. upper_red = np.array([10, 255, 255])
  62. lower_yellow = np.array([20, 100, 100])
  63. upper_yellow = np.array([30, 255, 255])
  64. people = ["", "", "", "", "", "", "", "", ""]
  65. result = ""
  66. ans = ""
  67.  
  68. # Первый взлет
  69. # Взлет
  70. navigate(x=0, y=0, z=0.6, frame_id='body', speed=0.5, auto_arm=True)
  71.  
  72. rospy.sleep(10)
  73.  
  74. # 1
  75. navigate(x=0.295, y=0.295, z=0.6, speed=0.8, frame_id='aruco_map')
  76. allcolor()
  77. rospy.sleep(5)
  78.  
  79. # 2
  80. navigate(x=0.885, y=0.295, z=0.6, speed=0.8, frame_id='aruco_map')
  81. allcolor()
  82. rospy.sleep(5)
  83.  
  84. # 3
  85. navigate(x=0.295, y=0.885, z=0.6, speed=0.8, frame_id='aruco_map')
  86. allcolor()
  87. rospy.sleep(5)
  88.  
  89. # 4
  90. navigate(x=0.885, y=0.885, z=0.6, speed=0.8, frame_id='aruco_map')
  91. allcolor()
  92. rospy.sleep(5)
  93.  
  94. # 5
  95. navigate(x=0.295, y=1.475, z=0.6, speed=0.8, frame_id='aruco_map')
  96. allcolor()
  97. rospy.sleep(5)
  98.  
  99. # 6
  100. navigate(x=0.885, y=1.475, z=0.6, speed=0.8, frame_id='aruco_map')
  101. allcolor()
  102. rospy.sleep(5)
  103.  
  104. # 7
  105. navigate(x=0.295, y=2.065, z=0.6, speed=0.8, frame_id='aruco_map')
  106. allcolor()
  107. rospy.sleep(5)
  108.  
  109. # 8
  110. navigate(x=0.885, y=2.065, z=0.6, speed=0.8, frame_id='aruco_map')
  111. allcolor()
  112. rospy.sleep(5)
  113.  
  114. # 9
  115. navigate(x=0.59, y=2.655, z=0.6, speed=0.8, frame_id='aruco_map')
  116. allcolor()
  117. rospy.sleep(5)
  118.  
  119. # возвращение домой
  120. navigate(x=0, y=0, z=0, speed=0.8, frame_id='aruco_map')
  121. rospy.sleep(5)
  122.  
  123. # Посадка
  124. land()
  125.  
  126. # Проверка массива на правильность заполнения
  127. print(people)
  128.  
  129. # ожидание на базе
  130. rospy.sleep(120)
  131.  
  132. bad_num = []
  133. bad_value = []
  134.  
  135. for x in range(len(people)):
  136.     if people[x] == "?" or people[x] == "+":
  137.         bad_num.append(x + 1)
  138.  
  139. rospy.init_node('barcode_test')
  140.  
  141. bridge = CvBridge()
  142.  
  143.  
  144. # Image subscriber callback function
  145. def image_callback(data):
  146.     cv_image = bridge.imgmsg_to_cv2(data, 'bgr8')  # OpenCV image
  147.     barcodes = pyzbar.decode(cv_image)
  148.     for barcode in barcodes:
  149.         b_data = barcode.data.encode("utf-8")
  150.         b_type = barcode.type
  151.         (x, y, w, h) = barcode.rect
  152.         xc = x + w / 2
  153.         yc = y + h / 2
  154.         cv2.putText(cv_image, "QR-код!", (xc, yc), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
  155.         print("Был найден {} со значением {} с центром в точках x={}, y={}".format(b_type, b_data, xc, yc))
  156.         if b_data == "COVID - 2019":
  157.             set_effect(r=255, g=0, b=0)  # пометка зараженного
  158.             print("Заражен")
  159.             rospy.sleep(5)
  160.         bad_value.append(b_data)
  161.  
  162.  
  163. image_sub = rospy.Subscriber('main_camera/image_raw', Image, image_callback, queue_size=1)
  164.  
  165. rospy.spin()
  166.  
  167. # Функция полета по конкретным пациентам
  168. def target(num):
  169.     if num == 1:
  170.         navigate(x=0.295, y=0.295, z=0.6, speed=0.8, frame_id='aruco_map')
  171.         rospy.sleep(5)
  172.     elif num == 2:
  173.         navigate(x=0.295, y=0.885, z=0.6, speed=0.8, frame_id='aruco_map')
  174.         rospy.sleep(5)
  175.     elif num == 3:
  176.         navigate(x=0.295, y=1.475, z=0.6, speed=0.8, frame_id='aruco_map')
  177.         rospy.sleep(5)
  178.     elif num == 4:
  179.         navigate(x=0.295, y=2.065, z=0.6, speed=0.8, frame_id='aruco_map')
  180.         rospy.sleep(5)
  181.     elif num == 5:
  182.         navigate(x=0.59, y=2.655, z=0.6, speed=0.8, frame_id='aruco_map')
  183.         rospy.sleep(5)
  184.     elif num == 6:
  185.         navigate(x=0.885, y=2.065, z=0.6, speed=0.8, frame_id='aruco_map')
  186.         rospy.sleep(5)
  187.     elif num == 7:
  188.         navigate(x=0.885, y=1.475, z=0.6, speed=0.8, frame_id='aruco_map')
  189.         rospy.sleep(5)
  190.     elif num == 8:
  191.         navigate(x=0.885, y=0.885, z=0.6, speed=0.8, frame_id='aruco_map')
  192.         rospy.sleep(5)
  193.     else:
  194.         navigate(x=0.885, y=0.295, z=0.6, speed=0.8, frame_id='aruco_map')
  195.         rospy.sleep(5)
  196.  
  197.  
  198. # Второй взлет
  199. # Взлет
  200. navigate(x=0, y=0, z=0.6, frame_id='body', speed=0.5, auto_arm=True)
  201.  
  202. # Ожидание 10 секунд
  203. rospy.sleep(10)
  204.  
  205. for n in bad_num:
  206.     target(n)
  207.  
  208. # возвращение домой
  209. navigate(x=0, y=0, z=0, speed=0.8, frame_id='aruco_map')
  210.  
  211. # Проверка на правильность всех данных
  212. print(bad_num)
  213. print(bad_value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement