寻找从图像中提取表格信息的 Python 程序

问题描述 投票:0回答:1

我们有纸质发票,是纸质的。我们拍摄这些发票的图像,并希望提取表格区域的单元格中包含的信息,并将它们导出为 CSV 或类似格式。

表格包含多列,单元格包含数字和单词。

我一直在寻找基于 ML 的 Python 程序来执行此操作,希望这是一个相对简单的任务(或者我可能错了),但遇到一个程序时运气不佳。

我可以检测水平和垂直线,并将它们组合起来定位单元格。但是检索单元格中包含的信息似乎是有问题的。

我能得到帮助吗?

我遵循了 this reference 中的一个过程,但是遇到了“bitnot”的错误:

import pytesseract
extract=[]
for i in range(len(order)):
    for j in range(len(order[i])):
        inside=''
        if(len(order[i][j])==0):
            extract.append(' ')
        else:
            for k in range(len(order[i][j])):
                side1,side2,width,height = order[i][j][k][0],order[i][j][k][1], order[i][j][k][2],order[i][j][k][3]
                final_extract = bitnot[side2:side2+h, side1:side1+width]
                final_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1))
                get_border = cv2.copyMakeBorder(final_extract,2,2,2,2, cv2.BORDER_CONSTANT,value=[255,255])
                resize = cv2.resize(get_border, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
                dil = cv2.dilate(resize, final_kernel,iterations=1)
                ero = cv2.erode(dil, final_kernel,iterations=2)
                ocr = pytesseract.image_to_string(ero)
                if(len(ocr)==0):
                    ocr = pytesseract.image_to_string(ero, config='--psm 3')
                inside = inside +" "+ ocr
            extract.append(inside)

a = np.array(extract)
dataset = pd.DataFrame(a.reshape(len(hor), total))
dataset.to_excel("output1.xlsx")

我得到的错误是这样的:

final_extract = bitnot[side2:side2+h, side1:side1+width]
NameError: name 'bitnot' is not defined`
python machine-learning tabular information-extraction
1个回答
0
投票

In similar shoes, 正在尝试做同样的事情,也找到了你引用的文章。我认为它可能会复制这篇原始文章的部分内容(Medium 上的付费专区:https://towardsdatascience.com/a-table-detection-cell-recognition-and-text-extraction-algorithm-to-convert-tables-to -excel-files-902edcf289ec).

作者之前定义了变量 bitnot,作为在组合的垂直和水平线内核上采用 cv2.bitwise_not 函数的输出。

© www.soinside.com 2019 - 2024. All rights reserved.