【问题标题】:How to extract the required parts of the text from the image instead of extracting all the text in an image using OCR?如何从图像中提取所需的文本部分,而不是使用 OCR 提取图像中的所有文本?
【发布时间】:2021-12-18 23:59:25
【问题描述】:

以下是一些交易图像,我将它们从 PDF 文件转换为图像 (jpg)。

从 PDF 转换的图像

  1. BCA Bank
  2. Maybank

现在,如何使用任何 OCR Python 包从图像(红色圆圈)中提取所需的文本部分,如下所示?

要提取的部分文本

  1. BCA Bank
  2. Maybank

注意:我将 PDF 文件转换为图像 (jpg) 的原因是某些 PDF 文件是扫描的 PDF 文件,这意味着它们不是原生 PDF 文件。 我在上面展示的图像从原生 PDF 文件转换而来的

【问题讨论】:

    标签: python pdf ocr image-segmentation opencv-python


    【解决方案1】:

    好的,所以我建议删去上面图片中的银行数据(如果这些是公司/人的真实陈述......)。作为一个实验,我将图像的大小调整为 3 倍(因此调整了像素坐标),并在此处做了几个部分的示例。

    #!/usr/bin/env python3
    import pytesseract
    from PIL import Image
    from pprint import pprint
    
    with Image.open("BCA_Bank.png") as img:
        img = img.resize((img.width*3, img.height*3))
        # Do a binary threshold on the image to make it solid black & white
        # the top two sections needed a different value than the bottom
        # because of font weight.
        topimg = img.convert("L").point(lambda p: 255 if p > 85 else 0).convert('1')
        bottomimg = img.convert("L").point(lambda p: 255 if p > 200 else 0).convert('1')
    
        sec1 = topimg.crop((29*3, 82*3, 358*3, 174*3))
        sec2 = topimg.crop((574*3, 83*3, 749*3, 163*3))
        tanggal = bottomimg.crop((41*3, 310*3, 86*3, 842*3))
        keterangan1 = bottomimg.crop((107*3, 291*3, 230*3, 757*3))
        keterangan2 = bottomimg.crop((239*3, 291*3, 388*3, 757*3))
    
        # This will open all 5 sections in temporary windows as image previews
        sec1.show()
        sec2.show()
        tanggal.show()
        keterangan1.show()
        keterangan2.show()
    
        # This could be abstracted so as not to be repetetive.
        sec1_text = pytesseract.image_to_string(sec1, config="--psm 6", lang="ind")
        sec2_text = pytesseract.image_to_string(sec2, config="--psm 6", lang="ind")
        tanggal_col = pytesseract.image_to_string(tanggal, config="--psm 4", lang="ind")
        keterangan1_col = pytesseract.image_to_string(keterangan1, config="--psm 4", lang="ind")
        keterangan2_col = pytesseract.image_to_string(keterangan2, config="--psm 4", lang="ind")
    
        headers = ["SEC1", "SEC2", "TANGGAL", "KETERANGAN1", "KETERANGAN2"]  #"CBG", "MUTASI", "SALDO"]
        col_data = [
            sec1_text.strip().splitlines(),
            sec2_text.strip().splitlines()
        ] + [
            [line.strip() for line in col.splitlines() if line]
            for col in [tanggal_col, keterangan1_col, keterangan2_col]
        ]
        pprint(dict(zip(headers, col_data)))
    

    这只有在所有工作表的大小和结构几乎完全相同时才有效——因为.crop 调用专门从图像中选择框。

    我不喜欢 pandas/numpy,所以我确信有一种更 Python 的方法可以使用这些库中的一个或两个来构造来自 tesseract 的解析数据。

    *注意关于--psm 选项:

    • --psm 4 的意思是“假设有一列可变大小的文本。”
    • --psm 6 表示“假设一个统一的文本块。”

    在终端中,输入 tesseract --help-psm 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多