簡體   English   中英

將 HOCR 輸出轉換為字符串的策略是什么(用於正則表達式)?

[英]What are the strategies to convert an HOCR output to a string (for regex purposes)?

我正在使用 Pytesseract 並希望將 HOCR 輸出轉換為字符串。 當然,這樣的功能是在 Pytesseract 中實現的,但我想更多地了解完成它的可能策略 thx

from pytesseract import image_to_pdf_or_hocr
hocr_output = image_to_pdf_or_hocr(image, extension='hocr')

由於hOCR是一種 .xml,我們可以使用 .xml 解析器。

但首先我們需要將tesseract的二進制輸出轉換為str:

from pytesseract import image_to_pdf_or_hocr

hocr_output = image_to_pdf_or_hocr(image, extension='hocr')
hocr = hocr_output.decode('utf-8')

現在我們可以使用xml.etree來解析它:

import xml.etree.ElementTree as ET

root = ET.fromstring(hocr)

xml.etree 為我們提供了一個文本迭代器,我們可以將其結果連接到單個字符串中:

text = ''.join(root.itertext())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM