Send compressed image in a service request using roslibjs

asked 2016-09-26 20:03:18 -0500

waspinator gravatar image

updated 2016-09-29 10:53:18 -0500

In the example showing basic roslibjs functionality they show how to send a service request. The data they send is just two ints, which is what the AddTwoInts service expects.

I'm trying to create a service that expects a sensor_msgs/CompressedImage.

I have a service file with the following:

sensor_msgs/CompressedImage original_image
sensor_msgs/CompressedImage modified_image

and I try to call the function using roslibjs

    var request = new ROSLIB.ServiceRequest({
      original_image: {
        header: {
          seq: 1,
          stamp: "",
          frame_id: ""
        format: "jpeg",
        data: compressed_image

    ProcessImageClient.callService(request, function(result) {

but I'm not sure what I should be putting in for header. This seems to get a response from the service, but it doesn't make much sense. All I'm trying to do for now is send back the image, but instead I get almost nothing back. This is my service handler:

def handle_process_image(req):
    print "This is a test: returning back original image"
    return ProcessImageResponse(req.original_image)

def process_image_server():
    s = rospy.Service('process_image', ProcessImage, handle_process_image)
    print "Ready to process image."

if __name__ == "__main__":

and this is what I get as a response.

Object { modified_image: {
     data: "AAAAAA==",
     format: "jpeg",
     header: Object

My data is obviously mangled, as it's always "AAAAAA==".

My question is, how do I create a proper sensor_msgs/CompressedImage object to send with my service request, and how do I send a sensor_msgs/CompressedImage back to roblibjs?


EDIT: I figured out that I wasn't actually sending the image data, so now that am it's working. Still not sure what to put for the header. seems to be binary data instead of text base64 encoded data.

This is what I get when I ranrospy.loginfo(, whereas I expected a readable character string representing the base64 image. the output of is <type 'str'>

                                                                                                                                                                                               u8��.��`��$���ؚ֯��|�Z�'v,�U9?Z��e"-���*�W2�,1�F?:�0��w&��}Q������$��Et����}���k����m��1O�WO��J��F���?�β�����+��{�����vQ���Sb-qu���=+��oF����j�)��Ob�����d��b��P?�?��u��AET�(��(��(��(��(��(��(����(�Q%J:V��u- ��
edit retag flag offensive close merge delete


Were you able to solve this problem?

mpenmet gravatar image mpenmet  ( 2018-08-18 22:43:06 -0500 )edit

I would also like to see more details on this. I want to send a (compressed) image on a topic, but don't know what format I should choose to display the image with javascript/html.

Good practice would be to answer your own question if you have the solution to your original problem.

knxa gravatar image knxa  ( 2018-09-25 13:34:58 -0500 )edit

Hey, Compressed image can be displayed on JS using Canvas. The data from ros compressed image can be converted to base64 using atob function.

mpenmet gravatar image mpenmet  ( 2018-09-29 21:56:13 -0500 )edit