7 from sensor_msgs.msg
import Image
8 from geometry_msgs.msg
import Twist
9 from dynamic_reconfigure.server
import Server
10 from leo_example_line_follower.cfg
import ColorMaskConfig
14 import tflite_runtime.interpreter
as tflite
19 color_mask_config =
None
21 def __init__(self, modelPath=None, velocityTopic=None, videoTopic=None):
27 except ValueError
as e:
28 rospy.logerr(
"Couldnt load tflite model: %s" % (modelPath))
35 self.
pub_mask = rospy.get_param(
"~pub_mask", default=
False)
37 self.
vel_pub = rospy.Publisher(velocityTopic, Twist, queue_size=10)
41 self.
mask_pub = rospy.Publisher(
"color_mask", Image, queue_size=1)
46 color_mask = cv2.inRange(
65 lower_mask = cv2.inRange(
79 upper_mask = cv2.inRange(
93 final_mask = lower_mask + upper_mask
102 if config.hue_min < config.hue_max
110 cv_img = self.
bridge.imgmsg_to_cv2(data, desired_encoding=
"bgr8")
118 rospy.logdebug(
"steering: %f, %f" % (steering[0], steering[1]))
124 vel_msg.linear.x = steering[0]
125 vel_msg.angular.z = steering[1]
131 img_msg = self.
bridge.cv2_to_imgmsg(mask, encoding=
"32FC1")
135 input_details = self.
interpreter.get_input_details()
136 output_details = self.
interpreter.get_output_details()
140 self.
interpreter.set_tensor(input_details[0][
"index"], [img])
145 linear_x = self.
interpreter.get_tensor(output_details[0][
"index"])[0][0]
146 angular_z = self.
interpreter.get_tensor(output_details[1][
"index"])[0][0]
147 rospy.logdebug(
"pred = (%f, %f)" % (linear_x, angular_z))
149 return linear_x, angular_z
153 hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
155 crop_img = hsv_img[200 : hsv_img.shape[0], :]
159 float_img = color_mask.astype(np.float32)
161 resized_img = cv2.resize(float_img, (160, 120))
163 final_img = resized_img / 255.0
165 return final_img[:, :, np.newaxis]
172 default=
"~/Downloads/saved_model.tflite",
176 help=
"path to the saved CNN model",
184 dest=
"velocity_topic",
185 help=
"name of the topic with Twist messages for rover",
190 default=
"camera/image_raw",
194 help=
"name of the topic with Image messages from rover",
198 if __name__ ==
"__main__":
199 rospy.init_node(
"line_follower")
201 parser = argparse.ArgumentParser(description=
"Record video from rover as images.")
204 rosargv = rospy.myargv(argv=sys.argv)
205 args = parser.parse_args(rosargv[1:])
207 follower =
LineFollower(args.model_path, args.velocity_topic, args.image_topic)