#!/usr/bin/env python # -*- coding: utf-8 -*- import rospy from yolov8_ros_msgs.msg import BoundingBoxes def bounding_boxes_callback(data): # 处理接收到的消息 print('Received bounding boxes:') for box in data.bounding_boxes: print(' - Class: ', box.Class) print(' Probability: ', box.probability) print(' Xmin: ', box.xmin) print(' Ymin: ', box.ymin) print(' Xmax: ', box.xmax) print(' Ymax: ', box.ymax) if __name__ == '__main__': # 初始化 ROS 节点 rospy.init_node('subscriber_node', anonymous=True) # 创建一个订阅者对象,订阅 /yolov8/BoundingBoxes 话题 rospy.Subscriber('/yolov8/BoundingBoxes', BoundingBoxes, bounding_boxes_callback) # 运行 ROS 节点 rospy.spin()这里面那一些数据可以倒入到另一个python文件中的class中的def
时间: 2023-12-10 09:42:43 浏览: 132
可以将数据倒入到另一个python文件中的class中的def中,这样可以更好地实现代码的模块化和可重用性。具体实现方法可以参考以下代码:
```
# 定义一个类
class BoundingBoxesHandler:
def __init__(self):
# 初始化ROS节点
rospy.init_node('subscriber_node', anonymous=True)
# 创建一个订阅者对象,订阅 /yolov8/BoundingBoxes 话题
rospy.Subscriber('/yolov8/BoundingBoxes', BoundingBoxes, self.bounding_boxes_callback)
def bounding_boxes_callback(self, data):
# 处理接收到的消息
print('Received bounding boxes:')
for box in data.bounding_boxes:
print(' - Class: ', box.Class)
print(' Probability: ', box.probability)
print(' Xmin: ', box.xmin)
print(' Ymin: ', box.ymin)
print(' Xmax: ', box.xmax)
print(' Ymax: ', box.ymax)
def run(self):
# 运行 ROS 节点
rospy.spin()
# 实例化类并运行
if __name__ == '__main__':
handler = BoundingBoxesHandler()
handler.run()
```
这样,你就可以在其他Python文件中导入这个类,并使用它的方法来处理接收到的数据了。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)