#!/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 = [] 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-19 10:28:35 浏览: 11
你好!对于你的代码,我注意到你正在尝试解析一组图片文件中的 QR 码,并将其写入一个文本文件。根据你的描述,代码没有报错,但是生成的文本文件没有内容。有几个可能的原因会导致这种情况发生。 首先,请确保指定的图像文件夹路径 `image_folder_path` 包含正确的图片文件,并且这些图片中包含 QR 码。你可以通过打印 `img_files` 列表来检查它是否包含预期的文件。 其次,请确保你已经正确安装了相关的依赖库 `pyzbar` 和 `cv2`。如果没有正确安装,代码将无法解码 QR 码。 此外,你可以检查一下 QR 码的解码过程是否正常。你可以尝试在循环中打印 `qr_data`,看看它是否能够正确地解码 QR 码内容。 最后,请确保你具有对目标文件夹和输出文件的写入权限。你可以尝试更改输出文件名或更改输出文件所在的目录,以确保你具有适当的权限。 如果你仍然无法解决问题,请提供更多的详细信息,以便我能够更好地帮助你解决这个问题。
相关问题

#!/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(('.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' output_file_name = 'qr_codes_found.txt' main(image_folder_path,output_file_name) 运行后报错FileNotFoundError: [Errno 2] No such file or directory: 'home'

该错误是由于找不到指定的文件夹路径而引起的。请确保您提供的图像文件夹路径是正确的,并且在运行脚本之前已经存在。 根据您的代码,您将图像文件夹路径设置为'home'。这可能是一个相对路径,如果是这样,请确保当前工作目录是包含该文件夹的文件夹。否则,您可以尝试使用绝对路径来指定图像文件夹路径。 另外,请注意,在调用`main`函数之前,您需要将代码放在一个名为`if __name__ == "__main__":`的条件语句块中。这将确保在作为脚本运行时才执行`main`函数。 以下是更新后的代码示例: ```python #!/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(('.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 = '/path/to/your/image/folder' output_file_name = 'qr_codes_found.txt' main(image_folder_path,output_file_name) ``` 请将`/path/to/your/image/folder`替换为您的实际图像文件夹路径。

#!/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)合并成一个代码

#!/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 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': 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退出 # 如果按下的是q键,则退出循环 if k == ord('q'): cap.release() image_folder_path = '/root/Pictures' output_file_name = 'qr_codes_found.txt' main(image_folder_path,output_file_name)无法生成所需文本

最新推荐

recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写
recommend-type

matlab基于RRT和人工势场法混合算法的路径规划.zip

matlab基于RRT和人工势场法混合算法的路径规划.zip
recommend-type

matlab基于matlab的两步定位软件定义接收机的开源GNSS直接位置估计插件模块.zip

matlab基于matlab的两步定位软件定义接收机的开源GNSS直接位置估计插件模块.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。