使用zbar python的条形码检测不适用于所有文档

时间:2018-06-19 07:05:27

标签: python barcode barcode-scanner zbar

我正在尝试使用zbar识别/检测以下文档中的条形码。这是我从tutorial使用的代码,用于测试我所拥有的数据库。

from __future__ import print_function
import pyzbar.pyzbar as pyzbar
import numpy as np
import cv2
import imutils
import argparse

def decode(im):
    # Find barcodes and QR codes
    decodedObjects = pyzbar.decode(im)

    # Print results
    for obj in decodedObjects:
        print('Type : ', obj.type)
        print('Data : ', obj.data, '\n')

    return decodedObjects  # Display barcode and QR code location


def display(im, decodedObjects):
    # Loop over all decoded objects
    for decodedObject in decodedObjects:
        points = decodedObject.polygon

    # If the points do not form a quad, find convex hull
    if len(points) > 4:
        hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
        hull = list(map(tuple, np.squeeze(hull)))
    else:
        hull = points


    # Number of points in the convex hull
    n = len(hull)

    # Draw the convext hull
    for j in range(0, n):
        cv2.line(im, hull[j], hull[(j + 1) % n], (0, 255, 0), 50)  # Display results
        cv2.imshow("Results", imutils.resize(im, 500))
        cv2.waitKey(0)  # Main


def display_ppn(im, decoded_objects, draw='rect'):
    if draw == 'rect':
        all_barcodes = []
        for decoded_object in decoded_objects:
            points = [[x, y] for x, y in (decoded_object.polygon)]
            all_barcodes.append(points)
        print(all_barcodes)

    else:
        all_barcodes = []
        for decoded_object in decoded_objects:
            points = [[x, y] for x, y in (decoded_object.polygon)]
            all_barcodes.append(points)
        print(all_barcodes)
        for barcode in all_barcodes:
            cv2.polylines(im, [np.array(barcode)], True, (0, 255, 0), 3)

        cv2.imshow("Results", imutils.resize(im, 500))
        cv2.waitKey(0)


if __name__ == '__main__':
    # Creates parser
    parser = argparse.ArgumentParser()

    # parser arguments:
    parser.add_argument('image', type=str, help='Path to image of form')

    args = parser.parse_args()

    # Read image
    im = cv2.imread(args.image)

    decodedObjects = decode(im)
    display_ppn(im, decodedObjects)

虽然有些文件工作正常,但大多数文件都没有。有人可以帮助我理解为什么会这样,以及我如何获得100%检测?增加条形码的大小或类型会有帮助吗?我拥有的输入图像将始终被二进制化。

工作样本

Working Document 1 Working Document 2

样本失败

Failed Document 1 Failed Document 2

1 个答案:

答案 0 :(得分:0)

拉直可以帮助识别软件找到条形码,但是由于混叠,这些代码中的许多条形码的条和空格都变窄或变粗。我不希望100%成功读取每个代码。我建议您使用条形码周围的空白用软件隔离条形码标签,并对下面显示的字符执行OCR。