Skip to content

Commit 99782b1

Browse files
committed
[extract_color] Call service from client
1 parent ee8607b commit 99782b1

File tree

1 file changed

+27
-12
lines changed
  • image_recognition_color_extractor/scripts

1 file changed

+27
-12
lines changed

image_recognition_color_extractor/scripts/get_colors

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,33 @@ from __future__ import print_function
55
import argparse
66

77
import cv2
8-
from image_recognition_color_extractor import ColorExtractor
98

10-
# Assign description to the help doc
11-
parser = argparse.ArgumentParser(description='Get dominant colors from image')
12-
parser.add_argument('image', type=argparse.FileType('r'), nargs='+', help='Image')
13-
parser.add_argument('--total-colors', default=3, type=int, help='Total colors')
14-
parser.add_argument('--dominant-range', default=10, type=int, help='Dominant range')
9+
import rospy
1510

16-
args = parser.parse_args()
11+
from image_recognition_msgs.srv import Recognize
12+
13+
from cv_bridge import CvBridge
14+
15+
def extract_color_client(img_msg):
16+
17+
rospy.wait_for_service('extract_color')
18+
try:
19+
extract_color = rospy.ServiceProxy('extract_color', Recognize)
20+
return extract_color(img_msg)
21+
except rospy.ServiceException as e:
22+
print("Service call failed: %s"%e)
23+
24+
25+
if __name__ == "__main__":
26+
27+
# Assign description to the help doc
28+
parser = argparse.ArgumentParser(description='Get dominant colors from image')
29+
parser.add_argument('image', type=argparse.FileType('r'), nargs='+', help='Image')
30+
args = parser.parse_args()
31+
bridge = CvBridge()
32+
33+
for image in args.image:
34+
print(image.name)
35+
colors = extract_color_client(bridge.cv2_to_imgmsg(cv2.imread(image.name)))
36+
print(colors)
1737

18-
color_extractor = ColorExtractor(args.total_colors, args.dominant_range)
19-
for image in args.image:
20-
print(image.name)
21-
colors = color_extractor.recognize(cv2.imread(image.name))
22-
print('Colors:', colors)

0 commit comments

Comments
 (0)