当前位置:网站首页>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()
边栏推荐
- Sourcery插件(自动提升代码质量)
- 活体检测FaceBagNet阅读笔记
- Cholesterol-PEG-Amine CLS-PEG-NH2 胆固醇-聚乙二醇-氨基科研用
- Evaluating Machine Learning Models - Excerpt
- Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
- 计算图像数据集均值和方差
- pytorch学习笔记10——卷积神经网络详解及mnist数据集多分类任务应用
- CAS: 1403744-37-5 DSPE-PEG-FA Phospholipid-Polyethylene Glycol-Folic Acid for Scientific Research
- 超参数优化-摘抄
- DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
猜你喜欢

MySQL free installation download and configuration tutorial

Cholesterol-PEG-DBCO Cholesterol-Polyethylene Glycol-Diphenylcyclooctyne Chemical Reagent

ROS之service编程的学习和理解

The content of the wangeditor editor is transferred to the background server for storage

random.randint函数用法

MySQL 入门:Case 语句很好用

mPEG-DMPE 甲氧基-聚乙二醇-双肉豆蔻磷脂酰乙醇胺用于形成隐形脂质体

【Rhapsody学习笔记】1:Hello World

mPEG-DSPE 178744-28-0 Methoxy-polyethylene glycol-phosphatidylethanolamine linear PEG phospholipids

DSPE-PEG-Biotin,CAS:385437-57-0,磷脂-聚乙二醇-生物素可延长循环半衰期
随机推荐
cocos2d-x implements cross-platform directory traversal
OpenCV中的图像数据格式CV_8U定义
【Rhapsody学习笔记】4:Relations
【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境
When solving background-size:cover, the picture is covered but not displayed completely?
Tensorflow边用边踩坑
Cholesterol-PEG-Azide CLS-PEG-N3 胆固醇-聚乙二醇-叠氮 MW:3400
MySQL 出现 The table is full 的解决方法
DingTalk H5 micro-app login authentication
Picture-in-Picture API in the browser
Shell/Vim相关list
Solution for MySQL The table is full
使用 OpenCV 提取图像的 HOG、SURF 及 LBP 特征 (含代码)
pyspark.ml feature transformation module
2021年软件测试面试题大全
自然语言处理相关list
The array technique, my love
Rejection sampling note
Redis-哈希
多元线性回归方程原理及其推导