diff --git a/dennis_nodes/tutel.py b/dennis_nodes/tutel.py index dd99b82..a96e8df 100644 --- a/dennis_nodes/tutel.py +++ b/dennis_nodes/tutel.py @@ -134,7 +134,7 @@ class tutel(): #some additional info on screen id_str = "IDs: " + str(ids) + " Target_ID:" + str(self.target_id) cv2.putText(rgb_image, id_str, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) - depth_str = "Depth_State:(" + str(self.depth_state) + " Depth:" + str(self.last_depth) + depth_str = "Depth_State:" + str(self.depth_state) + " Depth:" + str(self.last_depth) cv2.putText(rgb_image, depth_str, (10,60), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) info_str = "Resolution: " + str(self.w) + "x" + str(self.h) + " Search_State: " + str(self.search) + " Max_Min_Depth: " + str(self.maxMinDepth) cv2.putText(rgb_image, info_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) @@ -165,6 +165,7 @@ class tutel(): else: x = (self.top_left[0],self.bottom_left[0]) if self.bottom_left[1] < self.bottom_right[1]: y = (self.bottom_left[1],self.bottom_right[1]) else: y = (self.bottom_right[1],self.bottom_left[1]) + cv2.line(depth_image, (x[0],y[0]), (x[1],y[1]), (0,255,0), 2) for i in range(x[0], x[1]): for j in range(y[0], y[1]): self.last_depth += depth_image[j][x] @@ -172,6 +173,7 @@ class tutel(): self.last_depth /= self.number_of_operations print(self.depth_counter, self.last_depth, self.number_of_operations) self.number_of_operations = 0 + cv2.imshow("Tutel_Depth", depth_image) if self.last_depth < self.maxMinDepth and self.last_depth != 0: self.depth_counter += 1 if self.depth_counter > 0: diff --git a/dennis_nodes/tutel_depth.py b/dennis_nodes/tutel_depth.py new file mode 100644 index 0000000..b88b939 --- /dev/null +++ b/dennis_nodes/tutel_depth.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python +from __future__ import print_function + +import roslib +import sys +import rospy +import cv2 +import time +import numpy as np +import serial +import subprocess +from std_msgs.msg import String +from sensor_msgs.msg import Image +from cv_bridge import CvBridge, CvBridgeError +from geometry_msgs.msg import Twist + +TARGET = 0 +DEPTH = 550 + +class Tutel_Depth(): + def __init__(self, tutel_topic : str, astra_rgb_topic : str, astra_depth_topic : str): + #needed variables, velocity factor is a multiplier for the bot, might be changed later + #target_id used to remember the last id the tutel found, set to 0 at the beginning + self.target_id = TARGET + + #auruco init, best dictionary we found atm + self.arucoDict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_ARUCO_ORIGINAL) + self.arucoParams = cv2.aruco.DetectorParameters_create() + + #minimal depth we recognize, still testing for values! + self.maxMinDepth = DEPTH + + #Tutel_control + #self.pub = rospy.Publisher(tutel_topic, Twist, queue_size = 1) + self.wait_publishers(astra_rgb_topic, astra_depth_topic) + #self.t = Twist() + + #cv related + #rgb related + self.rgb_bridge = CvBridge() + self.lastConernerTime = time.time() + #depth related + self.depth_bridge = CvBridge() + self.depth_state = False + self.last_depth = 10.0 + self.depth_counter = 0 + self.top_left = (-1,-1) + self.top_right = (-1,-1) + self.bottom_right = (-1,-1) + self.bottom_left = (-1,-1) + self.number_of_operations = 0 + + #ros node related + rospy.init_node("turtle_depth", anonymous=True) + + #update rate can be increased + self.rate = rospy.Rate(15) + + #Sub init + self.rgb_sub = rospy.Subscriber(astra_rgb_topic, Image, self.callback_rgb) + self.depth_sub = rospy.Subscriber(astra_depth_topic, Image, self.callback_depth) + + #we wait for both publishers, camera topics, to send information + def wait_publishers(self, astra_rgb_topic, astra_depth_topic): + while True: + if any(astra_rgb_topic in topic_list for topic_list in rospy.get_published_topics()) and any(astra_depth_topic in topic_list for topic_list in rospy.get_published_topics()): + break + print("Waiting for {} and {} to start.".format(astra_rgb_topic, astra_depth_topic)) + rospy.sleep(1) + + def callback_rgb(self, data): + try: + rgb_image = self.rgb_bridge.imgmsg_to_cv2(data, "bgr8") + (self.h, self.w) = rgb_image.shape[:2] + except CvBridgeError as e: + print(e) + + (corners, ids, rejected) = cv2.aruco.detectMarkers(rgb_image, self.arucoDict, parameters = self.arucoParams) + if corners: + x = np.where(ids == self.target_id) + if len(x[0]) > 0: + fitting_corners = corners[x[0][0]] + rgb_image = self.markDetected(rgb_image, fitting_corners) + self.lastConernerTime = time.time() + elif(self.lastConernerTime - time.time()) < 1./2: + self.depth_state = False + + #some additional info on screen + id_str = "IDs: " + str(ids) + " Target_ID:" + str(self.target_id) + cv2.putText(rgb_image, id_str, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) + depth_str = "Depth_State:" + str(self.depth_state) + " Depth:" + str(self.last_depth) + cv2.putText(rgb_image, depth_str, (10,60), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) + info_str = "Resolution: " + str(self.w) + "x" + str(self.h) + " Max_Min_Depth: " + str(self.maxMinDepth) + cv2.putText(rgb_image, info_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) + + self.rate.sleep() + cv2.imshow("Tutel", rgb_image) + cv2.waitKey(1) + + + def callback_depth(self, data): + if self.depth_state: + depth_image = self.depth_bridge.imgmsg_to_cv2(data, "passthrough") + if self.depth_counter == 0: + #pass + self.last_depth = 10.0 + x = (0,0) + y = (0,0) + #self.number_of_operations = 0 + #x and y are swapped, because + print(str(self.top_left[0]) + " " + str(self.top_right[0]) + " " + str(self.top_left[1]) + " " + str(self.bottom_right[1])) + if (self.top_left[0] - self.bottom_left[0]) in range (-10,10): + if self.top_left[0] < self.top_right[0]: x = (self.top_left[0],self.top_right[0]) + else: x = (self.top_right[0],self.top_left[0]) + if self.top_left[1] < self.bottom_left[1]: y = (self.top_left[1],self.bottom_left[1]) + else: y = (self.bottom_left[1],self.top_left[1]) + elif (self.top_left[0] - self.top_right[0]) in range (-10,10): + if self.bottom_left[0] < self.top_left[0]: x = (self.bottom_left[0],self.top_left[0]) + else: x = (self.top_left[0],self.bottom_left[0]) + if self.bottom_left[1] < self.bottom_right[1]: y = (self.bottom_left[1],self.bottom_right[1]) + else: y = (self.bottom_right[1],self.bottom_left[1]) + cv2.line(depth_image, (x[0],y[0]), (x[1],y[1]), (0,255,0), 2) + for i in range(x[0], x[1]): + for j in range(y[0], y[1]): + self.last_depth += depth_image[j][x] + self.number_of_operations += 1 + self.last_depth /= self.number_of_operations + print(self.depth_counter, self.last_depth, self.number_of_operations) + self.number_of_operations = 0 + cv2.imshow("Tutel_Depth", depth_image) + if self.last_depth < self.maxMinDepth and self.last_depth != 0: + self.depth_counter += 1 + if self.depth_counter > 0: + print("Reached our destination") + self.depth_counter = 0 + self.depth_state = False + self.last_depth = 10.0 + else: + self.depth_counter = 0 + + + def markDetected(self, image, corners): + thresh_left = self.w//2 - 100 + thresh_right = self.w//2 + 100 + for markerCorner in corners: + (self.top_left, self.top_right, self.bottom_right, self.bottom_left) = markerCorner.reshape((4,2)) + self.top_left = (int(self.top_left[0]), int(self.top_left[1])) + self.top_right = (int(self.top_right[0]), int(self.top_right[1])) + self.bottom_right = (int(self.bottom_right[0]), int(self.bottom_right[1])) + self.bottom_left = (int(self.bottom_left[0]), int(self.bottom_left[1])) + + cv2.line(image, self.top_left, self.bottom_right, (0,255,0), 2) + cv2.line(image, self.top_right, self.bottom_right, (0,255,0), 2) + + self.depth_state = True + + return image \ No newline at end of file