可是这样他并没有生成文本
时间: 2024-04-15 21:01:50 浏览: 24
非常抱歉,我之前的回答有误。看来我误解了您的要求。
要在接收到图像消息时执行文本生成逻辑,您需要将 `image_callback` 函数中的文本生成代码放在其中。
以下是经过修改的示例代码:
```python
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import os
from pyzbar import pyzbar
def image_callback(msg):
# 将ROS图像消息转换为OpenCV图像
bridge = CvBridge()
frame = bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
# 执行生成文本的逻辑
image_folder_path = '/root/Pictures'
output_file_name = '/root/Pictures/qr_codes_found.txt'
main(image_folder_path, output_file_name)
def main(image_folder_path, output_file_name):
img_files = [f for f in os.listdir(image_folder_path) if f.endswith('.png')]
qr_codes_found = []
print("Image files:")
for img_file in img_files:
print(img_file)
for img_file in img_files:
img_path = os.path.join(image_folder_path, img_file)
img = cv2.imread(img_path)
barcodes = pyzbar.decode(img)
for barcode in barcodes:
if barcode.type == 'QRCODE':
qr_data = barcode.data.decode("utf-8")
qr_codes_found.append((img_file, qr_data))
unique_qr_codes = []
for file_name, qr_content in qr_codes_found:
if qr_content not in unique_qr_codes:
unique_qr_codes.append(qr_content)
with open(output_file_name, 'w') as f:
for qr_content in unique_qr_codes:
f.write("{}\n".format(qr_content))
if __name__ == '__main__':
rospy.init_node('text_generation_node')
# 创建一个订阅器订阅图像消息
rospy.Subscriber('processed_image', Image, image_callback)
rospy.spin()
```
在这个修改后的示例代码中,我们将文本生成逻辑移动到了`image_callback`函数中,在接收到图像消息时执行。同时,我们将节点初始化代码和图像消息的订阅器添加到了脚本中。
请确保将`image_folder_path`和`output_file_name`设置为正确的路径和文件名。
希望这次的回答对您有所帮助!如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)