call custom service in python [closed]

asked 2017-04-04 11:32:01 -0500

ebbeowulf gravatar image

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', 'image_data'] args are(<perception_core.srv._fr.fr object="" at="" 0x7f24681ffd50="">,)

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

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by tfoote
close date 2018-06-15 17:25:13.491516

Comments

What is the dtype for x_data?

lindzey gravatar image lindzey  ( 2017-04-05 04:01:56 -0500 )edit

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

ebbeowulf gravatar image ebbeowulf  ( 2017-04-05 15:27:46 -0500 )edit

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

lindzey gravatar image lindzey  ( 2017-04-05 16:45:51 -0500 )edit

Type and size:

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

>>> x_data.shape
(1357200,)
ebbeowulf gravatar image ebbeowulf  ( 2017-04-05 17:10:00 -0500 )edit