def localiza_base(self, frame): #@ chamado qnd searchBaseON = True --> desativa somente qnd próx. da base imagem, results = mobilenet_simples.detect(frame) thresholds = {"dog": 95, "horse": 90, "car": 90, "cow": 70} try: if len(results) > 0: self.INFOS['base_detectada'] = results[0] if results[0][0] == goal[2]: print(f"{results[0][0]}: -- {results[0][1]}") if results[0][0] == goal[2] and results[0][1] >= thresholds[ goal[2]]: self.ALVO['centro'] = ( ((results[0][2][0] + results[0][3][0]) // 2), ((results[0][2][1] + results[0][3][1]) // 2)) if not self.STATUS['searchBaseConfirmed']: # ativa o Checkpoint apenas uma vez, qnd chamar centraliza_robo. self.STATUS['checkpoint'] = True if not self.STATUS[ 'aproximaBase']: # evita que o searchBaseConfirmed seja reativado self.STATUS['searchBaseConfirmed'] = True print(f'Base [{results[0][0]}] detectada!') # Se for falso, volta para a pista: else: print('Procurando Base...') except CvBridgeError as e: print('ex', e)
def identifica_cor(frame): ''' Segmenta o maior objeto cuja cor é parecida com cor_h (HUE da cor, no espaço HSV). ''' # No OpenCV, o canal H vai de 0 até 179, logo cores similares ao # vermelho puro (H=0) estão entre H=-8 e H=8. # Precisamos dividir o inRange em duas partes para fazer a detecção # do vermelho: # frame = cv2.flip(frame, -1) # flip 0: eixo x, 1: eixo y, -1: 2 eixos frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) cor_menor = np.array([0, 50, 50]) cor_maior = np.array([10, 255, 255]) segmentado_cor = cv2.inRange(frame_hsv, cor_menor, cor_maior) # Note que a notacão do numpy encara as imagens como matriz, portanto o enderecamento é # linha, coluna ou (y,x) # Por isso na hora de montar a tupla com o centro precisamos inverter, porque centro = (frame.shape[1] // 2, frame.shape[0] // 2) # A operação MORPH_CLOSE fecha todos os buracos na máscara menores # que um quadrado 7x7. É muito útil para juntar vários # pequenos contornos muito próximos em um só. segmentado_cor = cv2.morphologyEx(segmentado_cor, cv2.MORPH_CLOSE, np.ones((7, 7))) # Representa a area e o centro do maior contorno no frame result_frame, result_tuples = mnet.detect(frame) #cv2.imshow('video', frame) cv2.imshow('seg', segmentado_cor) cv2.waitKey(1) return centro, result_frame, result_tuples
def detectMobileNet(self): """ Detecta a MobileNet na visão do robô :return result_tuples: tupla dos pesos encontrados """ result_frame, result_tuples = mnet.detect(self.getImage()) print(result_tuples) return result_tuples
def roda_todo_frame(imagem): global cv_image global media global centro global resultados global cx global bx global maior_contorno_area global cor global fecha_creeper global lista_detect global img_detect global lista_objects global achou_objeto global x_detect now = rospy.get_rostime() imgtime = imagem.header.stamp lag = now - imgtime delay = lag.nsecs try: antes = time.clock() cv_image = bridge.compressed_imgmsg_to_cv2(imagem, "bgr8") temp_image = bridge.compressed_imgmsg_to_cv2(imagem, "bgr8") cx = point_fuga.image_callback(temp_image) bx = point_fuga2.image_callback(temp_image) centro, img, resultados = visao_module.processa(cv_image) media, maior_contorno_area = visao_module.identifica_cor( cv_image, goal[0]) img_detect, lista_detect = mobilenet_simples.detect(cv_image) for objeto in lista_detect: if objeto[0] == goal[2]: x_detect = (objeto[2][0] + objeto[3][0]) / 2 achou_objeto = True # print(lista_detect) # fecha_creeper = tutorial.close_gripper() depois = time.clock() cv2.imshow("Camera", img_detect) cv2.waitKey(1) except CvBridgeError as e: print('ex', e)
def processa(frame): '''Use esta funcao para basear o processamento do seu robo''' result_frame, result_tuples = mnet.detect(frame) centro = (frame.shape[1]//2, frame.shape[0]//2) def cross(img_rgb, point, color, width,length): cv2.line(img_rgb, (point[0] - int(length/2), point[1]), (point[0] + int(length/2), point[1]), color ,width, length) cv2.line(img_rgb, (point[0], point[1] - int(length/2)), (point[0], point[1] + int(length/2)),color ,width, length) cross(result_frame, centro, [255,0,0], 1, 17) return centro, result_frame, result_tuples
def processa(frame): '''Use esta funcao para basear o processamento do seu robo''' result_frame, result_tuples, position = mnet.detect(frame) centro = (frame.shape[1] // 2, frame.shape[0] // 2) def cross(img_rgb, point, color, width, length): cv2.line(img_rgb, (point[0] - length / 2, point[1]), (point[0] + length / 2, point[1]), color, width, length) cv2.line(img_rgb, (point[0], point[1] - length / 2), (point[0], point[1] + length / 2), color, width, length) cross(result_frame, centro, [255, 0, 0], 1, 17) cv2.imshow('video', result_frame) cv2.waitKey(1) return centro, result_frame, result_tuples, position
def processa(frame, name): '''Use esta funcao para basea r o processamento do seu robo''' result_frame, result_tuples = mnet.detect(frame) centro = (frame.shape[1] // 2, frame.shape[0] // 2) reconheceu = False if len(result_tuples) > 0: if result_tuples[0][0] == name and result_tuples[0][1] >= 60.0: reconheceu = True def cross(img_rgb, point, color, width, length): cv2.line(img_rgb, (point[0] - int(length // 2), point[1]), (point[0] + int(length // 2), point[1]), color, width, length) cv2.line(img_rgb, (point[0], point[1] - int(length // 2)), (point[0], point[1] + int(length // 2)), color, width, length) cross(result_frame, centro, [255, 0, 0], 1, 17) # cv2.imshow('video', result_frame) # cv2.waitKey(1) return centro, result_frame, result_tuples, reconheceu
def roda_todo_frame(imagem): #função usada pelo subscriber recebe_imagem global cv_image global centro global media_vermelho global area_vermelho global media_azul global area_azul global results global tracking global contador global centro_tracking global fps global initBB global area_tracking global WIDTH global HEIGTH now = rospy.get_rostime() imgtime = imagem.header.stamp lag = now - imgtime delay = lag.secs if delay > atraso and check_delay == True: return try: antes = time.clock() cv_image = bridge.compressed_imgmsg_to_cv2(imagem, "bgr8") centro, media_vermelho, area_vermelho, media_azul, area_azul = cm.identifica_cor( cv_image) WIDTH, HEIGTH, g = cv_image.shape if tracking == NADA: results = mb.detect(cv_image, alvo) else: tracking_update() if len(results) > 0: contador += 1 else: contador = 0 if contador > 3: tracking = TRACKING pi_x = results[0][2][0] pi_y = results[0][2][1] pf_x = results[0][3][0] pf_y = results[0][3][1] w = pf_x - pi_x h = pf_y - pi_y centro_x = pi_x + w / 2 centro_y = pi_y + h / 2 area_tracking = w * h centro_tracking = (centro_x, centro_y) initBB = (pi_x, pi_y, w, h) tracker.init(cv_image, initBB) fps = FPS().start() results = [] tracking_update() depois = time.clock() cv2.imshow("Camera", cv_image) except CvBridgeError as e: print('ex', e)