Trouble sending images via ROS Service

asked 2021-05-26 08:00:10 -0600

TELESPIELSTUBE gravatar image

Hello

I'm trying to send a .png via service from a service client written in C++ to a service server written in Python. If the server receives an image the server responses by sending an OK back to the client. Before the sending process the image gets stored in a vector on the client side, but something does not work on the client side, because I keep getting the Error message and the server seems not getting anything from the client. Any ideas?

Service client code:

Client.h:

#include <ros/ros.h>
#include <vector>
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/Image.h>
#include "imagineer/ImageAck.h"
#include "picture.h"

class Client
{
    public:    
        Client() 
        {
            int_subscriber.subscribe(node, "camera/integer", 1); 
            service_client = node.serviceClient<imagineer::ImageAck>("image_ack");
        }

        void send_image();
        void add_to_list(int digit, sensor_msgs::Image& image);    
        void callback(const sensor_msgs::ImageConstPtr& image);

        private:
            ros::NodeHandle node;
            ros::ServiceClient service_client;
            std::vector<Picture> storage;
            message_filters::Subscriber<sensor_msgs::Image> img_subscriber; 
};

Client.cpp:

#include "controller.h"
void Client::send_image()
{       
    imagineer::ImageAck service
    if (!storage.empty())
    {
        service.request.image = storage.back().get_image();
    }
    if (service_client.call(service))
    {
        ROS_INFO("Received number: %s", service.response.result);
    }
    else
    {
        ROS_ERROR("No OK received!");
    }
}

void Controller::add_to_list(sensor_msgs::Image& image)
{
    storage.push_back(Picture(image));
}

void Controller::callback(const sensor_msgs::ImageConstPtr& image)
{
    try
    { 
        sensor_msgs::Image save_image = *image;
        add_to_list(save_image);
        send_image();
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("Error: %s", e.what());
    }
}

The server code is almost unchanged from the tutorial:

Server.py

#!/usr/bin/env python  
from __future__ import print_function
import rospy, cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from imagineer.srv import ImageAck, ImageAckResponse

def callback(request, args):
    print('Got image')
    request.result = 'OK'
    return ImageAckResponse(request)

def main():
    rospy.init_node('ai_service')
    rospy.loginfo('Neural network node is running')
    rospy.Service('image_ack', ImageAck, callback) 
    rospy.spin()

if __name__ == '__main__':
    main()
edit retag flag offensive close merge delete

Comments

@shonigmann: what is this a duplicate of?


Edit: I'm guessing this one: #q379003.

gvdhoorn gravatar image gvdhoorn  ( 2021-05-27 01:43:18 -0600 )edit

That's the one

shonigmann gravatar image shonigmann  ( 2021-05-27 10:23:22 -0600 )edit