【问题标题】:How to extract text from table in image?如何从图像中的表格中提取文本?
【发布时间】:2020-04-09 17:51:22
【问题描述】:

我有结构化表格图像中的数据。数据如下:

我尝试使用以下代码从该图像中提取文本:

import pytesseract
from PIL import Image

value=Image.open("data/pic_table3.png")
text = pytesseract.image_to_string(value, lang="eng")    
print(text)

而且,这里是输出:

EA 域

传统角色

未来的角色

技术 e 封闭平台 ¢ 开放平台

e 物理 e 虚拟化 应用程序和 |e 专有 e 跨组织 集成 e 孤立的复合材料 e P2P 集成应用程序

e EAI 技术 e 软件即服务

e 企业系统 e 面向服务

e 自动化交易架构

e “信息化”

互动

但是,预期的数据输出应该根据列和行对齐。我该怎么做?

【问题讨论】:

    标签: python ocr tesseract text-extraction python-tesseract


    【解决方案1】:

    在将图像放入 OCR 之前,您必须对图像进行预处理以移除表格线和点。这是一种使用 OpenCV 的方法。

    1. 加载图像、灰度和 Otsu 的阈值
    2. 删除水平线
    3. 删除垂直线
    4. 使用轮廓区域过滤扩展以连接文本并去除点
    5. 按位与重构图像
    6. OCR

    这是处理后的图像:

    Pytesseract 的结果

    EA Domains Traditional role Future role
    Technology Closed platforms Open platforms
    Physical Virtualized
    Applications and Proprietary Inter-organizational
    Integration Siloed composite
    P2P integrations applications
    EAI technology Software as a Service
    Enterprise Systems Service-Oriented
    Automating transactions Architecture
    “‘Informating”
    interactions
    

    代码

    import cv2
    import pytesseract
    
    pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    
    # Load image, grayscale, and Otsu's threshold
    image = cv2.imread('1.png')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    # Remove horizontal lines
    horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
    detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
    cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        cv2.drawContours(thresh, [c], -1, (0,0,0), 2)
    
    # Remove vertical lines
    vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,15))
    detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
    cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        cv2.drawContours(thresh, [c], -1, (0,0,0), 3)
    
    # Dilate to connect text and remove dots
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,1))
    dilate = cv2.dilate(thresh, kernel, iterations=2)
    cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        area = cv2.contourArea(c)
        if area < 500:
            cv2.drawContours(dilate, [c], -1, (0,0,0), -1)
    
    # Bitwise-and to reconstruct image
    result = cv2.bitwise_and(image, image, mask=dilate)
    result[dilate==0] = (255,255,255)
    
    # OCR
    data = pytesseract.image_to_string(result, lang='eng',config='--psm 6')
    print(data)
    
    cv2.imshow('thresh', thresh)
    cv2.imshow('result', result)
    cv2.imshow('dilate', dilate)
    cv2.waitKey()
    

    【讨论】:

    • 非常感谢您的回答@nathancy !但是,您的结果仍然不符合我的期望。有没有其他方法可以根据每一列和每一行对齐文本?
    • 您可以根据垂直线划分每一列,然后一次将其放入 Pytesseract 中,但它仍然不会与行对齐。不幸的是,这要困难得多。最好的情况是让它从上到下逐行读取(当前解决方案)或逐列读取。但在任何一种情况下,它都不会与每一列和每一行对齐,因为 Pytesseract 只读取图像上的原始文本。您可以尝试使用additional configuration options 来获得您想要的结果。
    【解决方案2】:

    您可能希望首先检测细胞,如图所示。您可以使用霍夫线变换(OpenCV 提供的库)来实现。之后,您可以使用检测到的线条来选择 ROI,然后提取每个单元格的文本。

    详细解释请访问我的blogpost

    【讨论】:

    • 非常感谢!我遵循了您的指南并取得了一些不错的结果,但如果页面结构不完全是表格(例如货物清单、账单等),我会遇到导致 ROI 超出范围等问题。有关如何处理此类半结构化图像的任何提示?
    猜你喜欢
    • 2022-07-15
    • 2018-08-29
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 2022-07-15
    • 2014-07-31
    相关资源
    最近更新 更多