bof_histogram_extractor.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 Extract BoF histogram in realtime.
5 """
6 
7 import gzip
8 import sys
9 if sys.version_info.major <= 2:
10  import cPickle as pickle
11 else: # for python3
12  import _pickle as pickle
13 from distutils.version import LooseVersion
14 from pkg_resources import get_distribution
15 
16 import numpy as np
17 from sklearn.preprocessing import normalize
18 
19 import cv_bridge
20 from jsk_recognition_msgs.msg import VectorArray
21 from jsk_recognition_utils import decompose_descriptors_with_label
22 from jsk_topic_tools import ConnectionBasedTransport
23 import message_filters
24 from posedetection_msgs.msg import Feature0D
25 from sensor_msgs.msg import Image
26 import rospy
27 
28 
29 class BoFHistogramExtractor(ConnectionBasedTransport):
30  def __init__(self):
31  super(BoFHistogramExtractor, self).__init__()
32 
33  self.queue_size = rospy.get_param('~queue_size', 10)
34 
35  # load bag of features
36  rospy.loginfo('Loading BoF data')
37  bof_data = rospy.get_param('~bof_data', None)
38  if bof_data is None:
39  quit()
40  with gzip.open(bof_data, 'rb') as f:
41  if sys.version_info.major <= 2:
42  self.bof = pickle.load(f)
43  else:
44  self.bof = pickle.load(f, encoding='latin1')
45  if (LooseVersion(get_distribution('scikit-learn').version) >=
46  LooseVersion('0.17')):
47  if 'n_jobs' not in self.bof.nn.__dict__ or not isinstance(self.bof.nn.n_jobs, int):
48  # In scikit-learn>=0.17.0,
49  # sklearn.neighbors.NearestNeighbors needs 'n_jobs' attribute.
50  # https://github.com/jsk-ros-pkg/jsk_recognition/issues/1669
51  self.bof.nn.n_jobs = 1
52  # noetic uses newer scikit-learn which uses n_samples_fit_
53  if 'n_ssamples_fit_' not in self.bof.nn.__dict__:
54  self.bof.nn.n_samples_fit_ = self.bof.nn._fit_X.shape[0]
55 
56  self._pub = self.advertise('~output', VectorArray, queue_size=1)
57  rospy.loginfo('Initialized BoF histogram extractor')
58 
59  def subscribe(self):
60  self._sub_feature = message_filters.Subscriber('~input', Feature0D)
61  self._sub_label = message_filters.Subscriber('~input/label', Image)
62  use_async = rospy.get_param('~approximate_sync', False)
63  if use_async:
64  slop = rospy.get_param('~slop', 0.1)
65  sync = message_filters.ApproximateTimeSynchronizer(
66  [self._sub_feature, self._sub_label],
67  queue_size=self.queue_size, slop=slop)
68  else:
70  [self._sub_feature, self._sub_label],
71  queue_size=self.queue_size)
72  rospy.logdebug('~approximate_sync: {}'.format(use_async))
73  sync.registerCallback(self._apply)
74 
75  def unsubscribe(self):
76  self._sub_feature.unregister()
77  self._sub_label.unregister()
78 
79  def _apply(self, feature_msg, label_msg):
80  bridge = cv_bridge.CvBridge()
81  label = bridge.imgmsg_to_cv2(label_msg)
82  desc = np.array(feature_msg.descriptors)
83  desc = desc.reshape((-1, feature_msg.descriptor_dim))
84  pos = np.array(feature_msg.positions)
85  pos = pos.reshape((-1, 2))
86  if label.sum() == 0:
87  rospy.logdebug('Skip image with only background label')
88  return
89  decomposed = decompose_descriptors_with_label(
90  descriptors=desc, positions=pos, label_img=label,
91  skip_zero_label=True)
92  X = np.array(list(decomposed.values()))
93  if X.size == 0:
94  return
95  X = self.bof.transform(X)
96  normalize(X, copy=False)
97  self._pub.publish(
98  VectorArray(
99  header=feature_msg.header,
100  vector_dim=X.shape[1],
101  data=X.reshape(-1),
102  ))
103 
104 
105 if __name__ == '__main__':
106  rospy.init_node('bof_histogram_extractor')
107  extractor = BoFHistogramExtractor()
108  rospy.spin()
node_scripts.bof_histogram_extractor.BoFHistogramExtractor._sub_feature
_sub_feature
Definition: bof_histogram_extractor.py:60
node_scripts.bof_histogram_extractor.BoFHistogramExtractor.bof
bof
Definition: bof_histogram_extractor.py:42
node_scripts.bof_histogram_extractor.BoFHistogramExtractor.__init__
def __init__(self)
Definition: bof_histogram_extractor.py:30
train_fcn_depth_prediction.transform
def transform(in_data)
Definition: train_fcn_depth_prediction.py:60
node_scripts.bof_histogram_extractor.BoFHistogramExtractor._pub
_pub
Definition: bof_histogram_extractor.py:56
message_filters::Subscriber
node_scripts.bof_histogram_extractor.BoFHistogramExtractor._sub_label
_sub_label
Definition: bof_histogram_extractor.py:61
node_scripts.bof_histogram_extractor.BoFHistogramExtractor._apply
def _apply(self, feature_msg, label_msg)
Definition: bof_histogram_extractor.py:79
node_scripts.bof_histogram_extractor.BoFHistogramExtractor.unsubscribe
def unsubscribe(self)
Definition: bof_histogram_extractor.py:75
node_scripts.bof_histogram_extractor.BoFHistogramExtractor.subscribe
def subscribe(self)
Definition: bof_histogram_extractor.py:59
node_scripts.bof_histogram_extractor.BoFHistogramExtractor.queue_size
queue_size
Definition: bof_histogram_extractor.py:33
message_filters::TimeSynchronizer
node_scripts.bof_histogram_extractor.BoFHistogramExtractor
Definition: bof_histogram_extractor.py:29


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Fri May 16 2025 03:11:16