Robotics StackExchange | Archived questions

call custom service in python

I am using Indigo, and having trouble calling a service from python.

I have defined a service for sending images and receiving a message:

#***** perception_core::FR.srv ******
# image size
int16 rows
int16 cols
int16 step
# copy of Mat data structure
int8[] image_data
---
int8[] bbox
float likelihood

The server side operation is implemented in C++, and works just fine. I use it regularly with other C++ programs. However, when calling it from python I am struggling:

from perception_core.srv import FR
rospy.wait_for_service('/ros_matching/FR_srv')
FR_srv = rospy.ServiceProxy('/ros_matching/FR_srv', FR)
img_in = imread('...')
img = np.array(img_in)
x_data = img.flatten()
bbox,likelihood = FR_srv(img.shape[0],img.shape[1],img.shape[1],x_data.tolist())

And I get the following error: *** SerializationError: field image_data[] exceeds specified width [int8]

I've also tried initializing the array beforehand:

request = FR()
request.rows = img.shape[0]
request.cols = img.shape[1]
request.step = img.shape[1]
request.image_data = x_data.tolist()
bbox,likelihood = FR_srv(request)

But this generates a different error:

*** TypeError: Invalid number of arguments, args should be ['rows', 'cols', 'step', 'imagedata'] args are(<perceptioncore.srv._FR.FR object at 0x7f24681ffd50>,)

Any thoughts or suggestions on what to try next would be greatly appreciated.

Asked by ebbeowulf on 2017-04-04 11:32:01 UTC

Comments

What is the dtype for x_data?

Asked by lindzey on 2017-04-05 04:01:56 UTC

x_data is the output of img.flatten(), so it should be a 1D numpy array.

Asked by ebbeowulf on 2017-04-05 15:27:46 UTC

Right, but containing what size of data? What does x_data.dtype give you?

Asked by lindzey on 2017-04-05 16:45:51 UTC

Type and size:

>>> x_data.dtype
dtype('uint8')

>>> x_data.shape
(1357200,)

Asked by ebbeowulf on 2017-04-05 17:10:00 UTC

Answers