如何从图像中提取信息

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

我正在尝试从image(png)中提取一些特定信息。

我尝试使用下面的代码提取文本

import cv2
import pytesseract
import os
from PIL import Image
import sys

def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)

    # Convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)

    # Write the image after apply opencv to do some ...
    cv2.imwrite("thres.png", img)
    # Recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open("invoice.png"))
    os.remove("invoice.png")

    return result

if __name__ == '__main__':
    from sys import argv

    if len(argv)<2:
        print("Usage: python image-to-text.py relative-filepath")
    else:
        print('--- Start recognize text from image ---')
        for i in range(1,len(argv)):
            print(argv[i])
            print(get_string(argv[i]))
            print()
            print()

        print('------ Done -------')

但是我想从特定字段中提取数据。

例如

 a) INVOICE NO.
 b) CUSTOMER NO.
 c) SUBTOTAL
 d) TOTAL
 e) DATE

如何从下面的图像“发票”中提取所需的信息?

PFB

enter image description here

python image-processing deep-learning nlp
1个回答
0
投票

如果必填字段遵循相似的模式,请使用正则表达式或基于规则的相似方法。如果它没有模式,并且您有权访问类似的数据集,请训练NER标记模型。也有开源的NER标签库。简单的NER搜索将使您可以访问此类库。希望这会有所帮助。

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