Skip to content

Commit a41f956

Browse files
committed
Make class for service client
1 parent cfe7109 commit a41f956

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

image_recognition_color_extractor/scripts/get_colors_stream

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,25 @@ from sensor_msgs.msg import Image
77

88
from image_recognition_msgs.srv import Recognize
99

10-
def extract_color_client(img_msg):
11-
12-
rospy.wait_for_service('extract_color')
13-
try:
14-
extract_color = rospy.ServiceProxy('extract_color', Recognize)
15-
return extract_color(img_msg)
16-
except rospy.ServiceException as e:
17-
print("Service call failed: %s"%e)
18-
19-
# Create a callback function for the subscriber.
20-
def callback(msg):
21-
22-
# Simply print out values in our custom message.
23-
colors = extract_color_client(msg)
24-
rospy.loginfo(colors)
10+
class ExtractColorClient(object):
11+
def __init__(self, image_topic, color_service):
12+
self.image_sub = rospy.Subscriber(image_topic, Image, self.color_callback)
13+
self.color_proxy = rospy.ServiceProxy(color_service, Recognize)
14+
self.color_proxy.wait_for_service(timeout=20)
2515

16+
def color_callback(self, msg):
17+
# Simply print out values in our custom message.
18+
colors = self.color_proxy(msg)
19+
rospy.loginfo(colors)
2620

2721
if __name__ == '__main__':
2822

29-
rospy.init_node('color_extractor_stream')
30-
3123
parser = argparse.ArgumentParser(description='Get dominant colors from image')
3224
parser.add_argument('--topic', default='/image', type=str, help='Topic')
3325
args = parser.parse_args()
34-
sub = rospy.Subscriber(args.topic, Image, callback)
35-
rospy.loginfo('Node has been started')
26+
27+
rospy.init_node('color_extractor_stream')
28+
29+
extract_color_client = ExtractColorClient(args.topic, 'extract_color')
3630

3731
rospy.spin()

0 commit comments

Comments
 (0)