4 Extract BoF histogram in realtime.
9 if sys.version_info.major <= 2:
10 import cPickle
as pickle
12 import _pickle
as pickle
13 from distutils.version
import LooseVersion
14 from pkg_resources
import get_distribution
17 from sklearn.preprocessing
import normalize
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
31 super(BoFHistogramExtractor, self).
__init__()
36 rospy.loginfo(
'Loading BoF data')
37 bof_data = rospy.get_param(
'~bof_data',
None)
40 with gzip.open(bof_data,
'rb')
as f:
41 if sys.version_info.major <= 2:
42 self.
bof = pickle.load(f)
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):
51 self.
bof.nn.n_jobs = 1
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]
56 self.
_pub = self.advertise(
'~output', VectorArray, queue_size=1)
57 rospy.loginfo(
'Initialized BoF histogram extractor')
62 use_async = rospy.get_param(
'~approximate_sync',
False)
64 slop = rospy.get_param(
'~slop', 0.1)
65 sync = message_filters.ApproximateTimeSynchronizer(
72 rospy.logdebug(
'~approximate_sync: {}'.format(use_async))
73 sync.registerCallback(self.
_apply)
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))
87 rospy.logdebug(
'Skip image with only background label')
89 decomposed = decompose_descriptors_with_label(
90 descriptors=desc, positions=pos, label_img=label,
92 X = np.array(list(decomposed.values()))
96 normalize(X, copy=
False)
99 header=feature_msg.header,
100 vector_dim=X.shape[1],
105 if __name__ ==
'__main__':
106 rospy.init_node(
'bof_histogram_extractor')