selective_search.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 
5 import cv_bridge
6 from jsk_topic_tools import ConnectionBasedTransport
7 from jsk_recognition_msgs.msg import RectArray, Rect
8 from sensor_msgs.msg import Image
9 import dynamic_reconfigure.server
10 from jsk_perception.cfg import SelectiveSearchConfig
11 import dlib
12 import cv2
13 
14 class SelectiveSearch(ConnectionBasedTransport):
15  def __init__(self):
16  super(SelectiveSearch, self).__init__()
17  dynamic_reconfigure.server.Server(
18  SelectiveSearchConfig, self._cb_dyn_reconfig)
19  self.pub_ = self.advertise('~output', RectArray, queue_size=10)
20  self.debug_pub_ = self.advertise('~debug', Image, queue_size=1)
21 
22  def subscribe(self):
23  self.sub_ = rospy.Subscriber('~input', Image, self._apply, queue_size=1)
24 
25  def unsubscribe(self):
26  self.sub_.unregister()
27  def _cb_dyn_reconfig(self, config, level):
28  self.min_size = config.min_size
29  self.max_size = config.max_size
30  return config
31  def _apply(self, img_msg):
32  bridge = cv_bridge.CvBridge()
33  img_bgr = bridge.imgmsg_to_cv2(img_msg, desired_encoding='bgr8')
34  img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
35  rects = []
36  dlib.find_candidate_object_locations(img_rgb, rects,
37  min_size=self.min_size)
38  ros_rect_array = RectArray()
39  ros_rect_array.header = img_msg.header
40  for d in rects:
41  if (d.right() - d.left()) * (d.bottom() - d.top()) > self.max_size:
42  continue
43  cv2.rectangle(img_bgr, (d.left(), d.top()), (d.right(), d.bottom()), (255, 0, 0), 3)
44  ros_rect_array.rects.append(Rect(x=d.left(), y=d.top(), width=d.right() - d.left(), height=d.bottom() - d.top()))
45  imgmsg = bridge.cv2_to_imgmsg(img_bgr, encoding='bgr8')
46  self.debug_pub_.publish(imgmsg)
47  self.pub_.publish(ros_rect_array)
48 
49 
50 if __name__ == '__main__':
51  rospy.init_node('selective_search')
52  selective_search = SelectiveSearch()
53  rospy.spin()


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27