@@ -7,31 +7,25 @@ from sensor_msgs.msg import Image
7
7
8
8
from image_recognition_msgs .srv import Recognize
9
9
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 )
25
15
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 )
26
20
27
21
if __name__ == '__main__' :
28
22
29
- rospy .init_node ('color_extractor_stream' )
30
-
31
23
parser = argparse .ArgumentParser (description = 'Get dominant colors from image' )
32
24
parser .add_argument ('--topic' , default = '/image' , type = str , help = 'Topic' )
33
25
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' )
36
30
37
31
rospy .spin ()
0 commit comments