-
Notifications
You must be signed in to change notification settings - Fork 12
Converted C++ aruco node/package to python #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MidnightRaider06
wants to merge
8
commits into
umdloop:main
Choose a base branch
from
MidnightRaider06:refactor/aruco_python
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a78fd04
Converted C++ aruco node/package to python
MidnightRaider06 d019c66
Merge branch 'main' into refactor/aruco_python
MidnightRaider06 3ffd230
Changed topics to parameters, removed dead code
MidnightRaider06 66a8c2b
Merge branch 'refactor/aruco_python' of github.com:MidnightRaider06/a…
MidnightRaider06 062ee35
Publish Detection2D instead of pose and image
MidnightRaider06 d568c8e
Make behavior tree and its aruco plugins work with new Detection2D me…
MidnightRaider06 3c37447
Merge branch 'main' into refactor/aruco_python
MidnightRaider06 59be2c8
Merge branch 'main' into refactor/aruco_python
mdurrani808 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,5 @@ pygame | |
| catkin_pkg | ||
| empy==3.3.4 | ||
| lark | ||
| numpy | ||
| numpy<2.0 | ||
| opencv-contrib-python==4.10.0.84 | ||
This file was deleted.
Oops, something went wrong.
Empty file.
172 changes: 172 additions & 0 deletions
172
src/subsystems/navigation/aruco_bt/aruco_bt/aruco_node.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import numpy as np | ||
| import cv2 | ||
| import cv2.aruco | ||
|
|
||
| import rclpy | ||
| from rclpy.node import Node | ||
| from rclpy.duration import Duration | ||
| from rclpy.time import Time | ||
| from sensor_msgs.msg import Image, CameraInfo | ||
| from geometry_msgs.msg import PoseStamped | ||
| from vision_msgs.msg import Detection2D, BoundingBox2D, ObjectHypothesisWithPose | ||
| from cv_bridge import CvBridge | ||
| import tf2_ros | ||
| import tf2_geometry_msgs | ||
|
|
||
|
|
||
| class ArUcoNode(Node): | ||
| def __init__(self): | ||
| super().__init__('aruco_node') | ||
|
|
||
| self.declare_parameter('marker_size', 0.2) | ||
| self.declare_parameter('image_topic', '/zed/zed_node/left/image_rect_color') | ||
| self.declare_parameter('camera_info_topic', '/zed/zed_node/left/camera_info') | ||
|
|
||
| sim = self.get_parameter('use_sim_time').get_parameter_value().bool_value | ||
| self.marker_size_ = self.get_parameter('marker_size').get_parameter_value().double_value | ||
| image_topic = self.get_parameter('image_topic').get_parameter_value().string_value | ||
| camera_info_topic = self.get_parameter('camera_info_topic').get_parameter_value().string_value | ||
|
|
||
| if sim: | ||
| self.marker_size_ = 0.50 | ||
|
|
||
| self.get_logger().info(f'sim={"true" if sim else "false"}') | ||
| self.get_logger().info(f'marker_size={self.marker_size_:.3f} meters') | ||
| self.get_logger().info(f'Subscribing to image feed: {image_topic}') | ||
| self.get_logger().info(f'Subscribing to camera info: {camera_info_topic}') | ||
|
|
||
| aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_50) | ||
| aruco_params = cv2.aruco.DetectorParameters() | ||
| self.aruco_detector_ = cv2.aruco.ArucoDetector(aruco_dict, aruco_params) | ||
|
|
||
| self.tf_buffer_ = tf2_ros.Buffer() | ||
| self.tf_listener_ = tf2_ros.TransformListener(self.tf_buffer_, self) | ||
|
|
||
| self.bridge_ = CvBridge() | ||
|
|
||
| self.image_sub_ = self.create_subscription( | ||
| Image, image_topic, self.image_callback, 1) | ||
|
|
||
| self.camera_info_sub_ = self.create_subscription( | ||
| CameraInfo, camera_info_topic, self.camera_info_callback, 10) | ||
|
|
||
| self.detection_pub_ = self.create_publisher(Detection2D, '/aruco_detection', 10) | ||
|
|
||
| self.marker_id_ = -1 | ||
| self.corners_ = [] | ||
| self.all_corners_ = [] | ||
| self.camera_info_received_ = False | ||
| self.camera_matrix_ = None | ||
| self.dist_coeffs_ = None | ||
| self.rvec_ = None | ||
| self.tvec_ = None | ||
|
|
||
| def camera_info_callback(self, msg): | ||
| if not self.camera_info_received_: | ||
| self.camera_matrix_ = np.array(msg.k, dtype=np.float64).reshape(3, 3) | ||
| self.dist_coeffs_ = np.array(msg.d, dtype=np.float64).reshape(-1, 1) | ||
| self.camera_info_received_ = True | ||
| self.get_logger().info('Camera info received') | ||
| self.destroy_subscription(self.camera_info_sub_) | ||
| self.camera_info_sub_ = None | ||
|
|
||
| def image_callback(self, msg): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this callback, we can have a generic node that subscribes to a detection 2d array and the source image and publish it, reason is we don't want to send this traffic across the netwrok if we don't have to |
||
| try: | ||
| cv_ptr = self.bridge_.imgmsg_to_cv2(msg, 'mono8') | ||
| except Exception as e: | ||
| self.get_logger().error(f'cv_bridge exception: {e}') | ||
| return | ||
|
|
||
| self.detect_aruco_markers(cv_ptr) | ||
|
|
||
| if self.marker_id_ >= 0 and len(self.corners_) > 0 and self.camera_info_received_: | ||
| pose = self.estimate_pose(msg) | ||
| if pose is not None: | ||
| xs = [pt[0] for pt in self.corners_] | ||
| ys = [pt[1] for pt in self.corners_] | ||
|
|
||
| detection = Detection2D() | ||
| detection.header.stamp = self.get_clock().now().to_msg() | ||
| detection.header.frame_id = 'map' | ||
| detection.bbox.center.position.x = float(sum(xs) / 4) | ||
| detection.bbox.center.position.y = float(sum(ys) / 4) | ||
| detection.bbox.size_x = float(max(xs) - min(xs)) | ||
| detection.bbox.size_y = float(max(ys) - min(ys)) | ||
|
|
||
| hypothesis = ObjectHypothesisWithPose() | ||
| hypothesis.hypothesis.class_id = str(self.marker_id_) | ||
| hypothesis.hypothesis.score = 1.0 | ||
| hypothesis.pose.pose = pose | ||
| detection.results.append(hypothesis) | ||
|
|
||
| self.detection_pub_.publish(detection) | ||
|
|
||
| def detect_aruco_markers(self, frame): | ||
| corners, ids, _ = self.aruco_detector_.detectMarkers(frame) | ||
|
|
||
| if ids is not None and len(ids) > 0: | ||
| self.marker_id_ = int(ids[0][0]) | ||
| self.all_corners_ = [corners[0]] | ||
| self.corners_ = [pt for pt in corners[0][0]] | ||
| else: | ||
| self.marker_id_ = -1 | ||
| self.corners_ = [] | ||
| self.all_corners_ = [] | ||
|
|
||
| def estimate_pose(self, msg): | ||
| if len(self.all_corners_) == 0: | ||
| return None | ||
|
|
||
| if self.marker_size_ <= 0: | ||
| self.get_logger().warn(f'Invalid marker_size_: {self.marker_size_:.3f}') | ||
| return None | ||
|
|
||
| half = self.marker_size_ / 2.0 | ||
| obj_points = np.array([[-half, half, 0],[ half, half, 0],[ half, -half, 0],[-half, -half, 0]], dtype=np.float32) | ||
|
|
||
| img_points = self.all_corners_[0].reshape(4, 2) | ||
| _, self.rvec_, self.tvec_ = cv2.solvePnP( | ||
| obj_points, img_points, | ||
| self.camera_matrix_, self.dist_coeffs_, | ||
| flags=cv2.SOLVEPNP_IPPE_SQUARE) | ||
|
|
||
| pose_camera = PoseStamped() | ||
| pose_camera.header.frame_id = msg.header.frame_id | ||
|
|
||
| pose_camera.pose.position.x = float(self.tvec_[0][0]) | ||
| pose_camera.pose.position.y = float(self.tvec_[1][0]) | ||
| pose_camera.pose.position.z = float(self.tvec_[2][0]) | ||
|
|
||
| pose_camera.pose.orientation.x = 0.0 | ||
| pose_camera.pose.orientation.y = 0.0 | ||
| pose_camera.pose.orientation.z = 0.0 | ||
| pose_camera.pose.orientation.w = 1.0 | ||
|
|
||
| try: | ||
| pose_camera.header.stamp = Time().to_msg() | ||
| pose_map = self.tf_buffer_.transform(pose_camera, 'map', timeout=Duration(seconds=0.1)) | ||
|
|
||
| tf_map_base = self.tf_buffer_.lookup_transform( | ||
| 'map', 'base_footprint', Time(), | ||
| timeout=Duration(seconds=0.1)) | ||
|
|
||
| pose_map.pose.orientation.x = tf_map_base.transform.rotation.x | ||
| pose_map.pose.orientation.y = tf_map_base.transform.rotation.y | ||
| pose_map.pose.orientation.z = tf_map_base.transform.rotation.z | ||
| pose_map.pose.orientation.w = tf_map_base.transform.rotation.w | ||
|
|
||
| return pose_map.pose | ||
| except Exception as ex: | ||
| self.get_logger().warn(f'Could not transform to map frame: {ex}') | ||
| return None | ||
|
|
||
|
|
||
| def main(args=None): | ||
| rclpy.init(args=args) | ||
| node = ArUcoNode() | ||
| rclpy.spin(node) | ||
| rclpy.shutdown() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [develop] | ||
| script_dir=$base/lib/aruco_bt | ||
| [install] | ||
| install_scripts=$base/lib/aruco_bt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import os | ||
| from glob import glob | ||
| from setuptools import setup | ||
|
|
||
| package_name = 'aruco_bt' | ||
|
|
||
| setup( | ||
| name=package_name, | ||
| version='0.0.0', | ||
| packages=[package_name], | ||
| data_files=[ | ||
| ('share/ament_index/resource_index/packages', ['resource/' + package_name]), | ||
| ('share/' + package_name, ['package.xml']), | ||
| (os.path.join('share', package_name, 'launch'), glob('launch/*.py')), | ||
| ], | ||
| install_requires=['setuptools'], | ||
| zip_safe=True, | ||
| maintainer='abhinavkota06', | ||
| maintainer_email='abhinav.kota06@gmail.com', | ||
| description='Python package for aruco detection and pose estimation', | ||
| license='TODO: License declaration', | ||
| tests_require=['pytest'], | ||
| entry_points={ | ||
| 'console_scripts': [ | ||
| 'aruco_node = aruco_bt.aruco_node:main', | ||
| ], | ||
| }, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standardize on just publishing a vision_msgs/Detection2DArray
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or another message, use your judgement