当前位置:网站首页>ROS service transfer pictures
ROS service transfer pictures
2022-07-31 06:33:00 【xp_fangfei】
之前一直用Topicto transfer pictures,近期由于项目需要,需要用service进行图片的传输,Checked a lot of blogs, there are very few detailed explanations,So write a blog record to facilitate follow-up inquiries,Also to help everyone.
topic和serviceThe transfer format for pictures is the same,It's just that the way they communicate is different.
Only the source code is introduced here,对于如何创建service工作空间,How to compile in my上一篇博客中有详细的介绍.
srv文件
img.srv
sensor_msgs/Image image // 图像转换到sensor_msgs/Image格式进行传输
---
string a
string b
C++版本
ros和opencvSee how to transfer pictures between them: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(); //Images are converted into message format for transmission
// 初始化learn_service::img的请求数据
learn_service::img srv;
srv.request.image = *image_srv;
// 请求服务调用
img_client .call(srv);
// TO-DO: Process the result of the service call
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;
//The image has message format to convert intoMat格式
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和opencvSee how to transfer pictures between them: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')
#Convert pictures to message format
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()
边栏推荐
- ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘
- Remote file xxx is mapped to the local path xxx and can't be found. You can continue debugging....
- 日志jar包冲突,及其解决方法
- Pytorch学习笔记7——处理多维特征的输入
- Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use
- DSPE-PEG-Azide DSPE-PED-N3 磷脂-聚乙二醇-叠氮脂质PFG
- 用pytorch里的children方法自定义网络
- Tensorflow相关list
- Pytorch Daily Practice - Predicting Surviving Passengers on the Titanic
- Tensorflow related list
猜你喜欢

Cholesterol-PEG-Azide CLS-PEG-N3 胆固醇-聚乙二醇-叠氮 MW:3400

Detailed explanation of mysql transaction principle

Fluorescein-PEG-DSPE 磷脂-聚乙二醇-荧光素荧光磷脂PEG衍生物

【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

Cholesterol-PEG-Thiol CLS-PEG-SH 胆固醇-聚乙二醇-巯基

Cholesterol-PEG-Amine CLS-PEG-NH2 胆固醇-聚乙二醇-氨基科研用

Pytorch Daily Practice - Predicting Surviving Passengers on the Titanic

pyspark.ml feature transformation module

Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....

unicloud cloud development record
随机推荐
2021年软件测试面试题大全
Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use
Pytorch study notes 7 - processing input of multi-dimensional features
wangeditor编辑器内容传至后台服务器存储
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘
Cholesterol-PEG-NHS NHS-PEG-CLS 胆固醇-聚乙二醇-活性酯可修饰小分子材料
DSPE-PEG-Thiol DSPE-PEG-SH phospholipid-polyethylene glycol-thiol liposome for later use
random.randint函数用法
活体检测FaceBagNet阅读笔记
pyspark.ml feature transformation module
DSPE-PEG-Thiol DSPE-PEG-SH 磷脂-聚乙二醇-巯基脂质体制备用
IDEA概述和安装及调试
钉钉企业内部-H5微应用开发
Attention based ASR(LAS)
Cholesterol-PEG-NHS NHS-PEG-CLS cholesterol-polyethylene glycol-active ester can modify small molecular materials
虚拟机查看端口号进程
Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
Software Testing Interview Questions 2021
When solving background-size:cover, the picture is covered but not displayed completely?