是指改成这样吗#!/usr/bin/env python3 import os import sys import cv2 from pyzbar import pyzbar def main(image_folder_path, output_file_name): img_files = [f for f in os.listdir(image_folder_path) if f.endswith(('.jpg'))] 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)) with open(output_file_name,'w') as f: for file_name,qr_content in qr_codes_found: f.write(f"{file_name}: {qr_content}\n") if __name__ == "__main__": image_folder_path = '/home/lk' output_file_name = 'qr_codes_found.txt' main(image_folder_path,output_file_name)
时间: 2024-04-12 08:26:39 浏览: 129
是的,你可以在 `main` 函数中添加一个打印语句来输出 `img_files` 列表,以确保它包含了你预期的图像文件。你已经做得很接近了,只是缩进有些问题。请使用以下代码进行更正:
```python
def main(image_folder_path, output_file_name):
img_files = [f for f in os.listdir(image_folder_path) if f.endswith(('.jpg'))]
qr_codes_found = []
print("Image files:")
for img_file in img_files:
print(img_file)
# ... 其余代码保持不变 ...
```
这将在 `main` 函数的 `img_files` 循环之前打印出图像文件的文件名。确保缩进正确,并运行代码,你将看到打印输出的图像文件列表。
如果你仍然遇到问题,请提供更多详细信息,我将尽力帮助你解决。
相关问题
#!/usr/bin/env python2.7 import os import sys import cv2 from pyzbar import pyzbar 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 = [] 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)) with open(output_file_name,'w') as f: for file_name,qr_content in qr_codes_found: f.write(f"{file_name}: {qr_content}\n") if __name__ == "__main__": image_folder_path = '/home/spurs/qr_test/' output_file_name = 'qr_codes_found.txt' main(image_folder_path,output_file_name)
这段代码是一个完整的脚本,用于在给定的图像文件夹中查找并解码 QR 码,并将解码结果保存到一个文件中。
脚本首先导入了需要的库和模块,包括 `os`、`sys`、`cv2` 和 `pyzbar`。
然后定义了 `main` 函数,这个函数是用来查找和解码 QR 码的核心部分,具体的功能和之前解释的一样。
最后,在 `__name__ == "__main__"` 的条件下,脚本设置了图像文件夹路径和输出文件名,并调用了 `main` 函数来执行查找和解码操作。
如果你运行这个脚本,它将会在指定的图像文件夹中查找并解码 QR 码,并将解码结果保存到一个名为 `qr_codes_found.txt` 的文件中。
将#!/usr/bin/env python2.7 -- coding: UTF-8 -- import time import cv2 from PIL import Image import numpy as np from PIL import Image if name == 'main': rtsp_url = "rtsp://127.0.0.1:8554/live" cap = cv2.VideoCapture(rtsp_url) #判断摄像头是否可用 #若可用,则获取视频返回值ref和每一帧返回值frame if cap.isOpened(): ref, frame = cap.read() else: ref = False #间隔帧数 imageNum = 0 sum=0 timeF = 24 while ref: ref,frame=cap.read() sum+=1 #每隔timeF获取一张图片并保存到指定目录 #"D:/photo/"根据自己的目录修改 if (sum % timeF == 0): # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转变成Image frame = Image.fromarray(np.uint8(frame)) frame = np.array(frame) # RGBtoBGR满足opencv显示格式 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) imageNum = imageNum + 1 cv2.imwrite("/root/Pictures/Pictures" + str(imageNum) + '.png', frame) print("success to get frame") #1毫秒刷新一次 k = cv2.waitKey(1) #按q退出 #if k==27:则为按ESC退出 if k == ord('q'): cap.release() break和#!/usr/bin/env python2.7 coding=UTF-8 import os import sys import cv2 from pyzbar import pyzbar 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": image_folder_path = '/root/Pictures' output_file_name = 'qr_codes_found.txt' main(image_folder_path,output_file_name)合并一下
您可以将两个代码合并为一个ROS节点,以便在同一个程序中运行。下面是合并后的代码:
```python
#!/usr/bin/env python2.7
# -*- coding: UTF-8 -*-
import time
import cv2
from PIL import Image
import numpy as np
from PIL import Image
import os
import sys
from pyzbar import pyzbar
from std_msgs.msg import String
import rospy
def capture_and_recognize():
# 初始化ROS节点
rospy.init_node('capture_and_recognize_node', anonymous=True)
# 创建图像识别结果的发布者
pub = rospy.Publisher('recognition_result', String, queue_size=10)
# 设置摄像头参数
rtsp_url = "rtsp://127.0.0.1:8554/live"
cap = cv2.VideoCapture(rtsp_url)
# 判断摄像头是否可用
# 若可用,则获取视频返回值ref和每一帧返回值frame
if cap.isOpened():
ref, frame = cap.read()
else:
ref = False
# 间隔帧数
imageNum = 0
sum = 0
timeF = 24
while ref:
ref, frame = cap.read()
sum += 1
# 每隔timeF获取一张图片并保存到指定目录
# "D:/photo/"根据自己的目录修改
if (sum % timeF == 0):
# 格式转变,BGRtoRGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 转变成Image
frame = Image.fromarray(np.uint8(frame))
frame = np.array(frame)
# RGBtoBGR满足opencv显示格式
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
imageNum = imageNum + 1
cv2.imwrite("/root/Pictures/Pictures" + str(imageNum) + '.png', frame)
print("success to get frame")
# 图像识别
img_path = "/root/Pictures/Pictures" + str(imageNum) + '.png'
img = cv2.imread(img_path)
barcodes = pyzbar.decode(img)
qr_codes_found = []
for barcode in barcodes:
if barcode.type == 'QRCODE':
qr_data = barcode.data.decode("utf-8")
qr_codes_found.append(qr_data)
unique_qr_codes = []
for qr_content in qr_codes_found:
if qr_content not in unique_qr_codes:
unique_qr_codes.append(qr_content)
# 发布图像识别结果
for qr_content in unique_qr_codes:
pub.publish(qr_content)
# 1毫秒刷新一次
k = cv2.waitKey(1)
# 按q退出
# 如果按下的是q键,则退出循环
if k == ord('q'):
cap.release()
break
if __name__ == '__main__':
try:
capture_and_recognize()
except rospy.ROSInterruptException:
pass
```
在这个合并的代码中,我们使用了ROS的Python库rospy来创建一个ROS节点。我们初始化了一个节点,创建了一个用于发布图像识别结果的话题,并在适当的位置发布识别结果。您可以根据需要进一步调整代码以满足您的机器人运行环境的要求。
阅读全文