我正在使用Pytesseract,并希望将HOCR输出转换为字符串。当然,这样的函数是在Pytesseract中实现的,但我想了解更多关于如何实现它的可能策略。
from pytesseract import image_to_pdf_or_hocr
hocr_output = image_to_pdf_or_hocr(image, extension='hocr')发布于 2019-11-18 07:37:30
因为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 iterator,我们可以将其结果连接到一个字符串中:
text = ''.join(root.itertext())https://stackoverflow.com/questions/57433342
复制相似问题