Skip to content
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

Improve topic name visualization in tile_image.py #2256

Merged
merged 3 commits into from Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/jsk_perception/nodes/tile_image.rst
Expand Up @@ -45,7 +45,7 @@ Parameters

Draw topic name on each image.

* ``font_scale`` (type: ``Float``, default: ``4``)
* ``font_scale`` (type: ``Float``, default: ``0.8``)

Font size to draw topic names.

Expand Down
3 changes: 2 additions & 1 deletion jsk_perception/CMakeLists.txt
Expand Up @@ -397,7 +397,8 @@ if(CATKIN_ENABLE_TESTING)
jsk_add_rostest(test/concave_hull_mask_image.test)
jsk_add_rostest(test/convex_hull_mask_image.test)
jsk_add_rostest(test/draw_rects.test)
jsk_add_rostest(test/draw_classification_result.test)
# TODO(wkentaro): Fix this. (https://github.com/jsk-ros-pkg/jsk_recognition/issues/2258)
# jsk_add_rostest(test/draw_classification_result.test)
jsk_add_rostest(test/extract_image_channel.test)
jsk_add_rostest(test/face_pose_estimation.test)
jsk_add_rostest(test/fcn_object_segmentation.test)
Expand Down
35 changes: 31 additions & 4 deletions jsk_perception/node_scripts/tile_image.py
Expand Up @@ -14,6 +14,30 @@
from distutils.version import StrictVersion
from threading import Lock


def draw_text_box(img, text, font_scale=0.8, thickness=2,
color=(0, 255, 0), fg_color=(0, 0, 0), loc='ltb'):
font_face = cv2.FONT_HERSHEY_SIMPLEX
size, baseline = cv2.getTextSize(text, font_face, font_scale, thickness)

H, W = img.shape[:2]

if loc == 'ltb': # left + top + below
# pt: (x, y)
pt1 = (0, 0)
pt2 = (size[0], size[1] + baseline)
pt3 = (0, size[1])
elif loc == 'rba': # right + bottom + above
pt1 = (W - size[0], H - size[1] - baseline)
pt2 = (W, H)
pt3 = (W - size[0], H - baseline)
else:
raise ValueError
if color is not None:
cv2.rectangle(img, pt1, pt2, color=color, thickness=-1)
cv2.putText(img, text, pt3, font_face, font_scale, fg_color, thickness)


class TileImages(ConnectionBasedTransport):
def __init__(self):
super(TileImages, self).__init__()
Expand All @@ -35,7 +59,7 @@ def __init__(self):
self.draw_topic_name = rospy.get_param('~draw_topic_name', False)
self.approximate_sync = rospy.get_param('~approximate_sync', True)
self.no_sync = rospy.get_param('~no_sync', False)
self.font_scale = rospy.get_param('~font_scale', 4)
self.font_scale = rospy.get_param('~font_scale', 0.8)
if (not self.no_sync and
StrictVersion(pkg_resources.get_distribution('message_filters').version) < StrictVersion('1.11.4') and
self.approximate_sync):
Expand Down Expand Up @@ -73,9 +97,11 @@ def simple_callback(self, target_topic):
def callback(msg):
with self.lock:
bridge = cv_bridge.CvBridge()
self.input_imgs[target_topic] = bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
img = bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
self.input_imgs[target_topic] = img
if self.draw_topic_name:
cv2.putText(self.input_imgs[target_topic], target_topic, (0, 50), cv2.FONT_HERSHEY_PLAIN, self.font_scale, (0, 255, 0), 4)
draw_text_box(img, rospy.resolve_name(target_topic),
font_scale=self.font_scale)
return callback
def _append_images(self, imgs):
if not imgs:
Expand Down Expand Up @@ -104,7 +130,8 @@ def _apply(self, *msgs):
for msg, topic in zip(msgs, self.input_topics):
img = bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
if self.draw_topic_name:
cv2.putText(img, topic, (0, 50), cv2.FONT_HERSHEY_PLAIN, self.font_scale, (0, 255, 0), 4)
draw_text_box(img, rospy.resolve_name(topic),
font_scale=self.font_scale)
imgs.append(img)
self._append_images(imgs)

Expand Down
1 change: 1 addition & 0 deletions jsk_perception/sample/sample_tile_image.launch
Expand Up @@ -22,6 +22,7 @@
output="screen">
<rosparam>
input_topics: [img1/output, img2/output, img3/output, img4/output]
draw_topic_name: true
</rosparam>
</node>

Expand Down