Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 网络摄像头条形码阅读器:将多个条形码数据导出到记事本_Python_Arrays_List_Opencv_Set - Fatal编程技术网

Python 网络摄像头条形码阅读器:将多个条形码数据导出到记事本

Python 网络摄像头条形码阅读器:将多个条形码数据导出到记事本,python,arrays,list,opencv,set,Python,Arrays,List,Opencv,Set,我修改了我在网上找到的一些代码,这些代码使用opencv/pyzbar从网络摄像头读取条形码 它可以读取多个条形码,但当我疲于将其写入记事本时,只会显示1个数据 我曾尝试将读取的数据保存到数组/列表并导出,但不起作用 如何让它将所有不同的条形码写入记事本 #import libraries 我没有阅读所有代码,但您似乎使用了“w”模式打开文件,每次这样做时,文件都会在写入之前被擦除 您可能需要对append使用mode='a' 对于第二个问题,只需稍微移动循环,使其在else语句中执行。在代

我修改了我在网上找到的一些代码,这些代码使用opencv/pyzbar从网络摄像头读取条形码

它可以读取多个条形码,但当我疲于将其写入记事本时,只会显示1个数据

我曾尝试将读取的数据保存到数组/列表并导出,但不起作用

如何让它将所有不同的条形码写入记事本

#import libraries


我没有阅读所有代码,但您似乎使用了“w”模式打开文件,每次这样做时,文件都会在写入之前被擦除

您可能需要对append使用mode='a'

对于第二个问题,只需稍微移动循环,使其在else语句中执行。在代码中,它将忽略if语句写入文件

if barcode_info in code:
    print("Duplicate")
else:
    code.append(barcode_info)
    print (code)
    #set ={}
    #set.update(barcode_info)
    with open("barcode_result.txt", mode ='a') as file:
        for x in code:
            file.write("Recognized Barcode:" + x +"\n")
哦,对不起,我以前没见过你

#3 Export to text 
code = []
if barcode_info in code: #this will never happen
    print("Duplicate")
您必须将代码=[]移出循环,就像在程序初始化时一样

比如:

    #2 Loop till Esc is pressed
code = []
while ret:
    ret, frame = camera.read()
    frame = read_barcodes(frame)
    cv2.imshow('Barcode/QR code reader', frame)
    if cv2.waitKey(1) & 0xFF == 27:
        break

#loop terminated, now write
with open("barcode_result.txt", mode ='a') as file:
        for x in code:
            file.write("Recognized Barcode:" + x +"\n")

#3 Close webcam
我没有测试,因为我不能使用你的库

def read_barcodes(frame):
...
#3 Export to text 
if barcode_info in code:
    print("Duplicate")
else:
    code.append(barcode_info)
    print (code)
    ...

谢谢它现在可以写入多个数据,但如何让它写入不同的条形码数据呢。
    #2 Loop till Esc is pressed
code = []
while ret:
    ret, frame = camera.read()
    frame = read_barcodes(frame)
    cv2.imshow('Barcode/QR code reader', frame)
    if cv2.waitKey(1) & 0xFF == 27:
        break

#loop terminated, now write
with open("barcode_result.txt", mode ='a') as file:
        for x in code:
            file.write("Recognized Barcode:" + x +"\n")

#3 Close webcam
def read_barcodes(frame):
...
#3 Export to text 
if barcode_info in code:
    print("Duplicate")
else:
    code.append(barcode_info)
    print (code)
    ...