首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python-tesseract获取Hocr输出

如何使用python-tesseract获取Hocr输出
EN

Stack Overflow用户
提问于 2015-12-13 14:10:32
回答 3查看 12.6K关注 0票数 5

我使用pytesseract得到了非常好的结果,但它不能保留双空格,它们对我来说真的很重要。因此,我决定检索hocr输出,而不是纯text.But;there似乎不是使用pytessearct指定配置文件的任何方式。

那么,是否可以使用pytesseract指定cofiguration文件,或者是否有一些默认的配置文件可以更改以获得hocr输出?

代码语言:javascript
复制
#run method from pytessearct.py
def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False, config=None):
    '''
    runs the command:
        `tesseract_cmd` `input_filename` `output_filename_base`

    returns the exit status of tesseract, as well as tesseract's stderr output

    '''
    command = [tesseract_cmd, input_filename, output_filename_base]

    if lang is not None:
        command += ['-l', lang]

    if boxes:
        command += ['batch.nochop', 'makebox']

    if config:
        command += shlex.split(config)
    #command+=['C:\\Program Files (x86)\\Tesseract-OCR\\tessdata\\configs\\hocr']
    #print "command:",command
    proc = subprocess.Popen(command,
            stderr=subprocess.PIPE)
    return (proc.wait(), proc.stderr.read())
EN

回答 3

Stack Overflow用户

发布于 2016-01-05 21:05:10

您可以在Python中使用另一个库来使用Tesseract:pyslibtesseract

图片:

代码:

代码语言:javascript
复制
import pyslibtesseract

tesseract_config = pyslibtesseract.TesseractConfig(psm=pyslibtesseract.PageSegMode.PSM_SINGLE_LINE, hocr=True)
print(pyslibtesseract.LibTesseract.simple_read(tesseract_config, 'phrase0.png'))

输出:

代码语言:javascript
复制
  <div class='ocr_page' id='page_1' title='image ""; bbox 0 0 319 33; ppageno 0'>
   <div class='ocr_carea' id='block_1_1' title="bbox 0 0 319 33">
    <p class='ocr_par' dir='ltr' id='par_1_1' title="bbox 10 13 276 25">
     <span class='ocr_line' id='line_1_1' title="bbox 10 13 276 25; baseline 0 0"><span class='ocrx_word' id='word_1_1' title='bbox 10 14 41 25; x_wconf 75' lang='eng' dir='ltr'><strong>the</strong></span> <span class='ocrx_word' id='word_1_2' title='bbox 53 13 97 25; x_wconf 84' lang='eng' dir='ltr'><strong>book</strong></span> <span class='ocrx_word' id='word_1_3' title='bbox 111 13 129 25; x_wconf 79' lang='eng' dir='ltr'><strong>is</strong></span> <span class='ocrx_word' id='word_1_4' title='bbox 143 17 164 25; x_wconf 83' lang='eng' dir='ltr'>on</span> <span class='ocrx_word' id='word_1_5' title='bbox 178 14 209 25; x_wconf 75' lang='eng' dir='ltr'><strong>the</strong></span> <span class='ocrx_word' id='word_1_6' title='bbox 223 14 276 25; x_wconf 76' lang='eng' dir='ltr'><strong>table</strong></span> 
     </span>
    </p>
   </div>
  </div>
票数 6
EN

Stack Overflow用户

发布于 2016-02-29 20:10:31

这对我很有效:)

代码语言:javascript
复制
from pytesseract import pytesseract
pytesseract.run_tesseract('image.png', 'output', lang=None, boxes=False, config="hocr")

其中:image.png是除了这个python文件之外的图像文件。将在这些文件旁边生成名为output.hocr的输出文件。在文本编辑器中打开此文件以查看hocr输出。

票数 5
EN

Stack Overflow用户

发布于 2015-12-15 14:53:58

只需在命令的末尾添加hocr,如下所示

input_filename output_filename_base hocr

输出文件将是html文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34248492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档