#!/usr/bin/env python2.7 # -*- coding: UTF-8 -*- 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() 如何让这个代码生成的文本变为excel文件
时间: 2024-04-17 11:29:26 浏览: 192
要将生成的文本内容保存为 Excel 文件,您可以使用 Python 的 openpyxl 库来实现。下面是修改后的代码,可将生成的文本保存为 Excel 文件:
```python
#!/usr/bin/env python2.7
# -*- coding: UTF-8 -*-
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import os
from pyzbar import pyzbar
from openpyxl import Workbook
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.xlsx'
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)
# 创建一个新的工作簿
wb = Workbook()
# 获取默认的工作表
sheet = wb.active
# 将数据写入工作表
for i, qr_content in enumerate(unique_qr_codes, start=1):
sheet.cell(row=i, column=1).value = qr_content
# 保存工作簿为Excel文件
wb.save(output_file_name)
if __name__ == '__main__':
rospy.init_node('text_generation_node')
# 创建一个订阅器订阅图像消息
rospy.Subscriber('processed_image', Image, image_callback)
rospy.spin()
```
在上面的代码中,我们使用 openpyxl 库来创建一个新的工作簿,并将数据写入工作表中。最后,我们使用 save() 方法将工作簿保存为 Excel 文件。
请确保已安装 openpyxl 库,可以使用以下命令进行安装:
```
pip install openpyxl
```
请根据您的需求修改文件路径和其他部分。
阅读全文