Skip to content

Commit

Permalink
Change seginfo msg type definition (HiroIshida#16)
Browse files Browse the repository at this point in the history
* Change Seginfo msg type definition

* Update readme
  • Loading branch information
HiroIshida authored Mar 1, 2022
1 parent 72e646a commit 71f39ea
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_custom_target(${PROJECT_NAME}_setup_package ALL

find_package(catkin REQUIRED
std_msgs
sensor_msgs
message_generation
)

Expand All @@ -21,6 +22,7 @@ add_message_files(
generate_messages(
DEPENDENCIES
std_msgs
sensor_msgs
)

if(CATKIN_ENABLE_TESTING)
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ Example for using the published topic from the node above is [masked_image_publi
- Input image
- `~debug_image` (`sensor_msgs/Image`)
- debug image
- `~segmentation_image` (`sensor_msgs/Image` with `8UC1` encoding)
- Segmentation image. Suppose detected class number is 14, image is filled with 0~14 uint8 values. Note that 0 means background label.
- `~debug_segmentation_image` (`sensor_msgs/Image` with `8UC1` encoding)
- Say detected class number is 14, `~segmentation_image` in grayscale image is almost completely dark and not good for debugging. Therefore this topic scale the value to [0 ~ 255] so that grayscale image is human-friendly.
- `~segmentation_info` (`detic_ros/SegmentationInfo`)
- class name list and confidence score list corresponding to `~segmentation_image`. Note that `score` of `background` class is always 1.0
- class name list, confidence score list and segmentation image with `8UC1` encoding. The image is filled by 0 and positive integers indicating segmented object number. These indexes correspond to those of class name list and confidence score list. Note that index 0 is always reserved for 'background' instance and the confidence of the that instance is always 1.0.

As for rosparam, see [node_cofig.py](./node_script/node_config.py).

12 changes: 6 additions & 6 deletions example/masked_image_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ class SampleNode:

def __init__(self, mask_class_name='bottle'):
sub_image = message_filters.Subscriber(rospy.get_param('~in_image'), Image)
sub_segmentation = message_filters.Subscriber(rospy.get_param('~segmentation'), Image)
sub_info = message_filters.Subscriber(rospy.get_param('~seginfo'), SegmentationInfo)
sub_list = [sub_image, sub_segmentation, sub_info]
sub_list = [sub_image, sub_info]
ts = message_filters.ApproximateTimeSynchronizer(sub_list, 100, 10.0)
ts.registerCallback(self.callback)

self.pub = rospy.Publisher(rospy.get_param('~out_image'), Image)
self.class_name = mask_class_name

def callback(self, msg_image, msg_segmentation, msg_info: SegmentationInfo):
def callback(self, msg_image, msg_info: SegmentationInfo):
rospy.loginfo('rec messages')

# find label number corresponding to desired object class name
Expand All @@ -38,12 +37,13 @@ def callback(self, msg_image, msg_segmentation, msg_info: SegmentationInfo):
except ValueError:
return

assert msg_image.width == msg_segmentation.width
assert msg_image.height == msg_segmentation.height
seg_img = msg_info.segmentation
assert msg_image.width == seg_img.width
assert msg_image.height == seg_img.height
bridge = CvBridge()
img = bridge.imgmsg_to_cv2(msg_image, desired_encoding='passthrough')

seg_matrix = segmentation_image_to_nparray(msg_segmentation)
seg_matrix = segmentation_image_to_nparray(seg_img)
mask_indexes = np.where(seg_matrix==label_index)

masked_img = copy.deepcopy(img)
Expand Down
1 change: 1 addition & 0 deletions msg/SegmentationInfo.msg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
std_msgs/Header header
string[] detected_classes
float32[] scores
sensor_msgs/Image segmentation
Binary file not shown.
3 changes: 1 addition & 2 deletions node_script/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def __init__(self, node_config: Optional[NodeConfig]=None):
# https://answers.ros.org/question/220502/image-subscriber-lag-despite-queue-1/?answer=220505?answer=220505#post-id-22050://answers.ros.org/question/220502/image-subscriber-lag-despite-queue-1/?answer=220505?answer=220505#post-id-220505
self.sub = rospy.Subscriber('~input_image', Image, self.callback, queue_size=1, buff_size=2**24)
self.pub_debug_image = rospy.Publisher('~debug_image', Image, queue_size=1)
self.pub_segmentation_image = rospy.Publisher('~segmentation_image', Image, queue_size=1)
if node_config.out_debug_segimage:
self.pub_debug_segmentation_image = rospy.Publisher('~debug_segmentation_image', Image, queue_size=10)

Expand Down Expand Up @@ -109,7 +108,6 @@ def callback(self, msg: Image):
assert data.shape == (seg_img.height, seg_img.width)
seg_img.data = data.flatten().astype(np.uint8).tolist()
assert set(seg_img.data) == set(list(range(len(instances.pred_masks)+1)))
self.pub_segmentation_image.publish(seg_img)

if self.node_config.out_debug_segimage:
debug_data = copy.deepcopy(data)
Expand All @@ -127,6 +125,7 @@ def callback(self, msg: Image):
seginfo.detected_classes = class_names_detected
seginfo.scores = [1.0] + instances.scores.tolist() # confidence with 1.0 about background detection
seginfo.header = msg.header
seginfo.segmentation = seg_img
self.pub_info.publish(seginfo)

time_elapsed_total = (rospy.Time.now() - time_start).to_sec()
Expand Down
4 changes: 2 additions & 2 deletions test/test_detic_ros_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def all_subscribed() -> bool:
return all(bool_list)

def cb_debug_image(msg): cb_data['msg1'] = msg
def cb_segimage(msg): cb_data['msg2'] = msg
def cb_debug_segimage(msg): cb_data['msg2'] = msg
def cb_info(msg): cb_data['msg3'] = msg
def cb_test_image(msg): cb_data['msg4'] = msg

rospy.Subscriber('/docker/detic_segmentor/debug_image', Image, cb_debug_image)
rospy.Subscriber('/docker/detic_segmentor/debug_segmentation_image', Image, cb_segimage)
rospy.Subscriber('/docker/detic_segmentor/debug_segmentation_image', Image, cb_debug_segimage)
rospy.Subscriber('/docker/detic_segmentor/segmentation_info', SegmentationInfo, cb_info)
rospy.Subscriber('/test_out_image', Image, cb_test_image)

Expand Down

0 comments on commit 71f39ea

Please sign in to comment.