""" Huhb3D ROS2 Depth + RGB + CameraInfo Publisher Node ==================================================== Publishes Huhb3D dataset frames as ROS2 sensor_msgs/Image (16UC1 depth, RGB8 color) with synchronized sensor_msgs/CameraInfo. CRITICAL NOTES on cv_bridge with 16-bit depth: 1. MUST use cv2.IMREAD_UNCHANGED to load 16-bit PNG - IMREAD_GRAYSCALE silently converts to 8-bit (HIGH BYTE LOST -> 99.7% error) - IMREAD_COLOR also loses precision 2. cv_bridge encoding for uint16 depth MUST be "16UC1" (mono16 also works) 3. depth_scale in scene_camera.json is 1.0 (values already in mm) 4. Zero pixels mean "no depth" (background), not 0mm distance 5. imshow of uint16 appears BLACK - must normalize to 0-255 first Usage: ros2 run huhb3d_ros_publisher --ros-args \ -p dataset_dir:=/path/to/sell_Huhb3D-Test-Precision-v4 \ -p object_name:=flange \ -p frame_rate:=10.0 \ -p loop:=true """ import rclpy from rclpy.node import Node from rclpy.qos import QoSProfile, QoSReliabilityPolicy, QoSHistoryPolicy from sensor_msgs.msg import Image, CameraInfo from cv_bridge import CvBridge import cv2 import numpy as np import json import argparse from pathlib import Path class Huhb3DPublisher(Node): def __init__(self): super().__init__('huhb3d_publisher') self.declare_parameter('dataset_dir', '') self.declare_parameter('object_name', 'flange') self.declare_parameter('frame_rate', 10.0) self.declare_parameter('loop', True) dataset_dir = self.get_parameter('dataset_dir').get_parameter_value().string_value obj_name = self.get_parameter('object_name').get_parameter_value().string_value fps = self.get_parameter('frame_rate').get_parameter_value().double_value self.loop = self.get_parameter('loop').get_parameter_value().bool_value if not dataset_dir: self.get_logger().error('dataset_dir parameter is required!') return self.obj_dir = Path(dataset_dir) / obj_name if not self.obj_dir.exists(): self.get_logger().error(f'Object directory not found: {self.obj_dir}') return self.bridge = CvBridge() self.frame_idx = 1 self.max_frames = self._count_frames() qos = QoSProfile( depth=10, reliability=QoSReliabilityPolicy.RELIABLE, history=QoSHistoryPolicy.KEEP_LAST, ) self.rgb_pub = self.create_publisher(Image, f'/huhb3d/{obj_name}/rgb', qos) self.depth_pub = self.create_publisher(Image, f'/huhb3d/{obj_name}/depth', qos) self.cam_info_pub = self.create_publisher(CameraInfo, f'/huhb3d/{obj_name}/camera_info', qos) self.scene_camera = self._load_scene_camera() interval = 1.0 / fps if fps > 0 else 0.1 self.timer = self.create_timer(interval, self._publish_frame) self.get_logger().info( f'Publishing {obj_name}: {self.max_frames} frames @ {fps:.1f} fps' ) def _count_frames(self): rgb_dir = self.obj_dir / 'rgb' if not rgb_dir.exists(): return 0 return len(list(rgb_dir.glob('frame_*.png'))) def _load_scene_camera(self): path = self.obj_dir / 'scene_camera.json' if not path.exists(): return {} with open(path) as f: return json.load(f) def _build_camera_info(self, frame_id): cam = self.scene_camera.get(str(frame_id), {}) K = cam.get('cam_K', [0]*9) msg = CameraInfo() msg.header.stamp = self.get_clock().now().to_msg() msg.header.frame_id = 'huhb3d_camera' msg.height = 600 msg.width = 800 msg.distortion_model = 'plumb_bob' msg.d = [0.0, 0.0, 0.0, 0.0, 0.0] msg.k = [float(x) for x in K] msg.r = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0] msg.p = [K[0], 0.0, K[2], 0.0, 0.0, K[4], K[5], 0.0, 0.0, 0.0, 1.0, 0.0] msg.binning_x = 0 msg.binning_y = 0 msg.roi.do_rectify = False return msg def _publish_frame(self): if self.frame_idx > self.max_frames: if self.loop: self.frame_idx = 1 else: self.get_logger().info('All frames published.') self.timer.cancel() return idx_str = f'{self.frame_idx:04d}' now = self.get_clock().now().to_msg() rgb_path = self.obj_dir / 'rgb' / f'frame_{idx_str}.png' depth_path = self.obj_dir / 'depth' / f'depth_{idx_str}.png' if rgb_path.exists(): rgb_cv = cv2.imread(str(rgb_path), cv2.IMREAD_COLOR) if rgb_cv is not None: rgb_msg = self.bridge.cv2_to_imgmsg(rgb_cv, encoding='rgb8') rgb_msg.header.stamp = now rgb_msg.header.frame_id = 'huhb3d_camera' self.rgb_pub.publish(rgb_msg) if depth_path.exists(): depth_cv = cv2.imread(str(depth_path), cv2.IMREAD_UNCHANGED) if depth_cv is not None: if depth_cv.dtype != np.uint16: self.get_logger().warn( f'Depth dtype={depth_cv.dtype}, expected uint16! ' f'Use IMREAD_UNCHANGED.' ) depth_msg = self.bridge.cv2_to_imgmsg(depth_cv, encoding='16UC1') depth_msg.header.stamp = now depth_msg.header.frame_id = 'huhb3d_camera' self.depth_pub.publish(depth_msg) cam_info_msg = self._build_camera_info(self.frame_idx) cam_info_msg.header.stamp = now self.cam_info_pub.publish(cam_info_msg) self.get_logger().debug(f'Published frame {idx_str}') self.frame_idx += 1 def main(args=None): rclpy.init(args=args) node = Huhb3DPublisher() rclpy.spin(node) node.destroy_node() rclpy.shutdown() if __name__ == '__main__': main()