draw_classification_result.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # To avoid jsk_perception dependency, Copied from
4 # https://github.com/jsk-ros-pkg/jsk_recognition/blob/master/jsk_perception/node_scripts/draw_classification_result.py
5 # https://github.com/jsk-ros-pkg/jsk_recognition/blob/master/jsk_recognition_utils/python/jsk_recognition_utils/color.pyx
6 
7 from __future__ import absolute_import
8 from __future__ import division
9 from __future__ import print_function
10 
11 import cv2
12 import cv_bridge
13 from distutils.version import LooseVersion
14 from jsk_recognition_msgs.msg import ClassificationResult
15 from jsk_topic_tools import ConnectionBasedTransport
16 import message_filters
17 import numpy as np
18 import rospy
19 from sensor_msgs.msg import Image
20 
21 
22 def bitget(byteval, idx):
23  return ((byteval & (1 << idx)) != 0)
24 
25 
26 def labelcolormap(N=256):
27  """Color map (RGB) considering label numbers N.
28  Args:
29  N (``int``): Number of labels.
30  """
31  cmap = np.zeros((N, 3),
32  dtype=np.float32)
33  for i in range(0, N):
34  id = i
35  r, g, b = 0, 0, 0
36  for j in range(0, 8):
37  r = np.bitwise_or(r, (bitget(id, 0) << 7-j))
38  g = np.bitwise_or(g, (bitget(id, 1) << 7-j))
39  b = np.bitwise_or(b, (bitget(id, 2) << 7-j))
40  id = (id >> 3)
41  cmap[i, 0] = r / 255.
42  cmap[i, 1] = g / 255.
43  cmap[i, 2] = b / 255.
44  return cmap
45 
46 
47 class DrawClassificationResult(ConnectionBasedTransport):
48 
49  def __init__(self):
50  super(self.__class__, self).__init__()
51  self.pub = self.advertise('~output', Image, queue_size=1)
52  self.cmap = labelcolormap(255)
53 
54  def subscribe(self):
55  self.sub = message_filters.Subscriber('~input', ClassificationResult)
56  self.sub_img = message_filters.Subscriber('~input/image', Image)
58  [self.sub, self.sub_img], queue_size=10)
59  sync.registerCallback(self._draw)
60 
61  def unsubscribe(self):
62  self.sub.unregister()
63  self.sub_img.unregister()
64 
65  def _draw(self, cls_msg, imgmsg):
66  bridge = cv_bridge.CvBridge()
67  rgb = bridge.imgmsg_to_cv2(imgmsg, desired_encoding='rgb8')
68 
69  n_results = len(cls_msg.labels)
70  for i in range(n_results):
71  label = cls_msg.labels[i]
72  color = self.cmap[label % len(self.cmap)] * 255
73  legend_size = int(rgb.shape[0] * 0.1)
74  rgb[:legend_size, :] = (np.array(color) * 255).astype(np.uint8)
75 
76  label_name = cls_msg.label_names[i]
77  if len(label_name) > 16:
78  label_name = label_name[:10] + '..' + label_name[-4:]
79  label_proba = cls_msg.label_proba[i]
80  title = '{0}: {1:.2%}'.format(label_name, label_proba)
81  (text_w, text_h), baseline = cv2.getTextSize(
82  title, cv2.FONT_HERSHEY_PLAIN, 1, 1)
83  scale_h = legend_size / (text_h + baseline)
84  scale_w = rgb.shape[1] / text_w
85  scale = min(scale_h, scale_w)
86  (text_w, text_h), baseline = cv2.getTextSize(
87  label_name, cv2.FONT_HERSHEY_SIMPLEX, scale, 1)
88  if LooseVersion(cv2.__version__).version[0] < 3:
89  line_type = cv2.CV_AA
90  else: # for opencv version > 3
91  line_type = cv2.LINE_AA
92  cv2.putText(rgb, title, (0, text_h - baseline),
93  cv2.FONT_HERSHEY_PLAIN + cv2.FONT_ITALIC,
94  scale, (255, 255, 255), 1,
95  line_type)
96 
97  out_msg = bridge.cv2_to_imgmsg(rgb, encoding='rgb8')
98  out_msg.header = imgmsg.header
99  self.pub.publish(out_msg)
100 
101 
102 if __name__ == '__main__':
103  rospy.init_node('draw_classification_result')
105  rospy.spin()
draw_classification_result.DrawClassificationResult.__init__
def __init__(self)
Definition: draw_classification_result.py:49
draw_classification_result.DrawClassificationResult.sub
sub
Definition: draw_classification_result.py:55
draw_classification_result.bitget
def bitget(byteval, idx)
Definition: draw_classification_result.py:22
message_filters::Subscriber
draw_classification_result.labelcolormap
def labelcolormap(N=256)
Definition: draw_classification_result.py:26
draw_classification_result.DrawClassificationResult.pub
pub
Definition: draw_classification_result.py:51
draw_classification_result.DrawClassificationResult.unsubscribe
def unsubscribe(self)
Definition: draw_classification_result.py:61
draw_classification_result.DrawClassificationResult
Definition: draw_classification_result.py:47
draw_classification_result.DrawClassificationResult.cmap
cmap
Definition: draw_classification_result.py:52
draw_classification_result.DrawClassificationResult.sub_img
sub_img
Definition: draw_classification_result.py:56
message_filters::TimeSynchronizer
draw_classification_result.DrawClassificationResult.subscribe
def subscribe(self)
Definition: draw_classification_result.py:54
draw_classification_result.DrawClassificationResult._draw
def _draw(self, cls_msg, imgmsg)
Definition: draw_classification_result.py:65


sound_classification
Author(s):
autogenerated on Fri May 16 2025 03:12:55