使用基于OpenCV的img2table库,轻松从图片和PDF中识别、提取表格内容

img2table是一个基于OpenCV的Python库,专为处理PDF和图像中的表格而设计。采用先进的神经网络解决方案,为用户提供了实用、轻量级的替代方案,尤其适用于在CPU上执行。

图片[1]-使用基于OpenCV的img2table库,轻松从图片和PDF中识别、提取表格内容-山海云端论坛

库的特点:

  1. 在图像和PDF文件中准确识别表格,包括表格单元边界框。
  2. 支持多种OCR服务/工具(Tesseract、PaddleOCR、AWS Textract、Google Vision和Azure OCR),实现表格内容的精准提取。
  3. 处理复杂的表格结构,包括合并单元格。
  4. 实现图像的倾斜和旋转矫正。
  5. 提取的表格以简单的对象形式返回,包括一个Pandas DataFrame表示。
  6. 提供将表格导出为Excel文件的选项,保留原始结构。

支持的文件格式:

图像: 支持多种图像格式,不支持多页图像。

PDF格式: 支持本机和扫描的PDF文件。

一、安装

<code>pip install img2table<em>#标准安装支持Tesseract</em></code><code>pip install img2table[paddle]<em>#用于Paddle OCR</em></code><code>pip install img2table[easyocr]<em>#用于 EasyOCR</em></code><code>pip install img2table[gcp]<em>#用于 Google Vision OCR</em></code><code>pip install img2table[aws]<em>#用于 AWS Textract OCR</em></code><code>pip install img2table[azure]<em>#用于 Azure 认知服务 OCR</em></code>

二、使用

图像文件实例化如下:

<code>from img2table.document import Image</code>
<code>image = Image(src,detect_rotation=False)</code>
<code>"""</code><code>说明参数</code><code>src :str或bytes 或pathlib.Path或BytesIO,图片文件格式</code><code>detect_rotation:bool,可选,默认False,检测并纠正图像的倾斜/旋转</code><code>"""</code>

PDF文件

<code>from img2table.document import PDF</code>
<code>pdf = PDF(src, </code><code>pages=[0, 2],</code><code> detect_rotation=False,</code><code> pdf_text_extraction=True)</code>
<code>"""</code><code>参数说明</code><code>src :  str或bytes 或pathlib.Path或BytesIO,PDF文件格式</code><code>pages : 列表, 可选, 默认None。要处理的 PDF 页面索引列表。如果为 None,则处理所有页面</code><code>detect_rotation:bool,可选,默认False,检测并纠正从 PDF 中提取的图像的倾斜/旋转</code><code>pdf_text_extraction:bool,可选,默认True,从原生 PDF 的 PDF 文件中提取文本</code><code>"""</code>

三、OCR

img2table为多个 OCR 服务和工具提供接口,以便解析表内容。

如果可能(即对于原生 PDF),将直接从文件中提取 PDF 文本,并且不会调用 OCR 服务/工具。以Tesseract为例

<code>from img2table.ocr import TesseractOCR</code>
<code>ocr = TesseractOCR(n_threads=1, </code><code>lang="eng", </code><code>psm=11,</code><code> tessdata_dir="...")</code>

四、表提取

使用文档的方法可以从 PDF 页面/图像中一次提取多个表格。使用类方法extract_tables返回

<code>from img2table.ocr import TesseractOCR</code><code>from img2table.document import Image</code>
<code><em># Instantiation of OCR</em></code><code>ocr = TesseractOCR(n_threads=1, lang="eng")</code>
<code><em># Instantiation of document, either an image or a PDF</em></code><code>doc = Image(src)</code>
<code><em># Table extraction</em>extracted_tables = doc.extract_tables(ocr=ocr, implicit_rows=False, borderless_tables=False, min_confidence=50)"""参数说明ocr :OCRInstance,可选,默认None用于解析文档文本的 OCR 实例。如果为 None,则不会提取单元格内容implicit_rows:bool,可选,默认False指示是否应标识隐式行的布尔值 - 检查相关示例borderless_tables:bool,可选,默认False指示是否在有边框表的顶部提取无边框表的布尔值。min_confidence:int、可选、默认50OCR 处理文本的最低置信度,从 0(最差)到 99(最好)"""                   </code>
<code>ExtractedTable 类用于对从文档中提取的表进行建模。</code>
<code>属性</code><code>bbox:BBox,表边界框</code><code>title : str,提取的表标题</code><code>content : OrderedDict,以行索引为键,以对象列表为值的字典TableCell</code><code>df : pd.DataFrame,表的 Pandas DataFrame 表示形式</code><code>html : str,表格的 HTML 表示形式</code>

五、返回提取结果

图像

Image类的extract_tables方法返回ExtractedTable对象的列表。

PDF格式

PDF类的extract_tables方法返回一个OrderedDict对象,该对象将页面索引作为键和ExtractedTable对象的列表。

<code>output = {</code><code> 0: [ExtractedTable(...), ...],</code><code> 1: [],</code><code> ...</code><code> last_page: [ExtractedTable(...), ...]</code><code>}</code>

Excel 导出

从文档中提取的表格可以导出为 xlsx 文件。生成的文件由每个提取的表的一个工作表组成。

方法参数在方法中很常见。

<code>from img2table.ocr import TesseractOCR</code><code>from img2table.document import Image</code>
<code><em># Instantiation of OCR</em></code><code>ocr = TesseractOCR(n_threads=1, lang="eng")</code>
<code><em># Instantiation of document, either an image or a PDF</em></code><code>doc = Image(src)</code>
<code><em># Extraction of tables and creation of a xlsx file containing tables</em></code><code>doc.to_xlsx(dest=dest,</code><code> ocr=ocr,</code><code> implicit_rows=False,</code><code> borderless_tables=False,</code><code> min_confidence=50)</code>
图片[2]-使用基于OpenCV的img2table库,轻松从图片和PDF中识别、提取表格内容-山海云端论坛
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容