当前位置:网站首页>ROS之service传输图片
ROS之service传输图片
2022-07-31 05:16:00 【xp_fangfei】
之前一直用Topic来进行图片的传输,近期由于项目需要,需要用service进行图片的传输,查了不少博客很少有详细的解释,故写博客记录一下方便后续查询,也为了帮助大家。
topic和service对于图片的传输格式是一样的,只不过两者的通信方式不一样。
在此只介绍源码的编写,对于如何创建service工作空间,如何编译在我的上一篇博客中有详细的介绍。
srv文件
img.srv
sensor_msgs/Image image // 图像转换到sensor_msgs/Image格式进行传输
---
string a
string b
C++版本
ros和opencv之间图片如何传输详见:Converting between ROS images and OpenCV images (C++)
客户端程序
client_img.cpp
#include <ros/ros.h>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include "learn_service/img.h"
int main(int argc, char** argv)
{
ros::init(argc, argv, "client_img");
ros::NodeHandle node;
// 发现/cam服务后,创建一个服务客户端,连接名为/service_img的service
ros::service::waitForService("/cam");
ros::ServiceClient img_client = node.serviceClient<learn_service::img>("/cam");
//读取图片
cv::Mat image = cv::imread("../data/6.png");
sensor_msgs::ImagePtr image_srv = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg(); //图片转换成消息格式进行传输
// 初始化learn_service::img的请求数据
learn_service::img srv;
srv.request.image = *image_srv;
// 请求服务调用
img_client .call(srv);
// TO-DO: 处理服务调用结果
return 0;
};
服务端程序
server_img.cpp
#include <ros/ros.h>
#include "learn_service/img.h"
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
// service回调函数,输入参数req,输出参数res
bool cam_result_callback(learn_service::img::Request &req,
learn_service::img::Response &res)
{
cv_bridge::CvImagePtr cv_ptr;
//图像有消息格式转换成Mat格式
cv_ptr = cv_bridge::toCvCopy(req.image, sensor_msgs::image_encodings::BGR8);
cv::imshow("view", cv_ptr->image);
cv::waitKey(0);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "server_img");
ros::NodeHandle node;
// 创建一个名为/cam的server,注册回调函数cam_result_callback
ros::ServiceServer img_service = node.advertiseService("/cam", cam_result_callback);
// 循环等待回调函数
ros::spin();
return 0;
}
python版本
ros和opencv之间图片如何传输详见:Converting between ROS images and OpenCV images (Python)
客户端程序
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import rospy
import cv2
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
from learn_service.srv import img, imgRequest
def Img_client():
# ROS节点初始化
rospy.init_node('client_img')
# 发现/img服务后,创建一个服务客户端,连接名为/img的service
rospy.wait_for_service('/img')
bridge = CvBridge()
#读取图片
image = cv2.imread('/home/ubuntu/ros_practice/src/learn_service/data/6.png')
#转换图片为消息格式
image_message = bridge.cv2_to_imgmsg(image, "bgr8")
# try:
img_client = rospy.ServiceProxy('/img', img)
# 请求服务调用,输入请求数据
response = img_client(image_message)
return response.a
# except rospy.ServiceException, e:
# print "Service call failed: %s"%e
if __name__ == "__main__":
#服务调用并显示调用结果
Img_client()
服务端程序
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import rospy
import cv2
from learn_service.srv import img, imgResponse
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
def ImgCallback(req):
bridge = CvBridge()
cv_image = bridge.imgmsg_to_cv2(req.image, "bgr8")
cv2.imshow("view", cv_image)
cv2.waitKey(0)
# 反馈数据
#return imgResponse("OK1", "OK2")
def img_server():
# ROS节点初始化
rospy.init_node('server_img')
# 创建一个名为/show_person的server,注册回调函数personCallback
s = rospy.Service('/img', img, ImgCallback)
# 循环等待回调函数
print("Ready to show img informtion")
rospy.spin()
if __name__ == "__main__":
img_server()
边栏推荐
- cocos2d-x-3.x 修改和纪录
- 如何修改数据库密码
- unicloud 发布后小程序提示连接本地调试服务失败,请检查客户端是否和主机在同一局域网下
- TransactionTemplate transaction programmatic way
- quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
- Web Screenshots and Reverse Proxy
- CMOS管原理,及其在推挽电路中的应用
- Access database query
- cv2.resize()是反的
- The browser looks for events bound or listened to by js
猜你喜欢

腾讯云GPU桌面服务器驱动安装

著名网站msdn.itellyou.cn原理分析

深度学习知识点杂谈

Android software security and reverse analysis reading notes

unicloud cloud development record

小米手机短信定位服务激活失败

After unicloud is released, the applet prompts that the connection to the local debugging service failed. Please check whether the client and the host are under the same local area network.
![[Cloud native] Simple introduction and use of microservice Nacos](/img/06/b0594208d5b0cbf3ae8edd80ec12c4.png)
[Cloud native] Simple introduction and use of microservice Nacos

For penetration testing methods where the output point is a timestamp (take Oracle database as an example)

变分自编码器VAE实现MNIST数据集生成by Pytorch
随机推荐
Navicat从本地文件中导入sql文件
Several solutions for mysql startup error The server quit without updating PID file
this指向问题
360 加固 file path not exists.
数据库 | SQL查询进阶语法
对js的数组的理解
For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
如何修改数据库密码
Why does read in bash need to cooperate with while to read the contents of /dev/stdin
sql 添加 default 约束
Sqlite A列数据复制到B列
Numpy常用函数
qt:cannot open C:\Users\某某某\AppData\Local\Temp\main.obj.15576.16.jom for write
cv2.imread()
cocos2d-x-3.2创建项目方法
flutter arr 依赖
sql add default constraint
The latest MySql installation teaching, very detailed
cocos2d-x-3.2图片灰化效果
Sqlite column A data is copied to column B