【问题标题】:How to decode multiple images barcode with opencv如何使用opencv解码多个图像条形码
【发布时间】:2021-12-28 01:38:23
【问题描述】:

我可以读取每个图像的代码,但是当图像完成后,我没有按 Esc 关闭窗口,并且使用此代码如何同时解码文件夹中的多个图像时间?

这些是我的代码

import cv2
import numpy as np
from pyzbar.pyzbar import decode

paths = r'\QrCodeBarCode\Images'
#img = cv2.imread('1.png')
# cap = cv2.VideoCapture(0)
# cap.set(3,640)
# cap.set(4,480)

for img in paths:
    while True:

        # success, img = cap.read()
        for barcode in decode(img):
            myData = barcode.data.decode('utf-8')
            print(myData)
            pts = np.array([barcode.polygon],np.int32)
            pts = pts.reshape((-1,1,2))
            cv2.polylines(img,[pts],True,(255,0,255),5)
            pts2 = barcode.rect
            cv2.putText(img,myData,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,
                        0.9,(0,255,0),2)
        cv2.imshow('Result',img)
        cv2.waitKey(1)

【问题讨论】:

  • 您可以将图像保存到文件中而不是使用 cv2.imshow 吗?试试with open(filename, 'wb') as openfile: openfile.write(imagebytes...)
  • 您也可以使用进度条库来显示程序的进度。 pypi.org/project/progressbar2
  • 我希望上面的代码同时适用于多个图像
  • 您需要立即查看图像吗?还是可以将它们保存到文件中?

标签: python python-3.x opencv


【解决方案1】:

使用os.listdir读取目录中的所有数据,然后检查每个图像

import cv2
import numpy as np
from pyzbar.pyzbar import decode

paths = r'\QrCodeBarCode\Images'

for filename in os.listdir(paths):
    img_path = os.path.join(paths, filename)
    img = cv2.imread(img_path)
    for barcode in decode(img):
        myData = barcode.data.decode('utf-8')
        print(myData)
        pts = np.array([barcode.polygon],np.int32)
        pts = pts.reshape((-1,1,2))
        cv2.polylines(img,[pts],True,(255,0,255),5)
        pts2 = barcode.rect
        cv2.putText(img,myData,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,
                    0.9,(0,255,0),2)
    cv2.imwrite('result_' + filename, img)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多