接近Python的信心

2024-03-28 15:18:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图构建一个python-tesseract的OCR扩展,专门处理具有内部结构的数据表(例如,包含行和列的小计和总计,允许用户通过强制结构来提高准确性)。在

我正在尝试访问tesseract分配给多个结果的置信度(例如,来自无约束运行的所有结果以及来自字符限制为[0-9\.]的运行的所有结果)。在

我已经看到了一些关于访问GetHOCRTextapi方法的x_wconf属性的信息,但是还没有弄清楚如何从python api访问它。如何调用/访问此值?谢谢!在

我在OSX 10.10.3和Python2.7上使用了PythonTesseract 0.9.1。在


Tags: 方法用户属性字符结构数据表ocr行和列
1条回答
网友
1楼 · 发布于 2024-03-28 15:18:06

编辑

事实上我错了,我想的是pytesseract,而不是python tesseract。在

如果你去看看API源代码(baseapi_mini.h),你会发现有些函数对于你正在尝试的工作来说听起来非常有前途。你感兴趣的部分大约从第500行开始。在

  char* GetUTF8Text();

  /**
   * Make a HTML-formatted string with hOCR markup from the internal
   * data structures.
   * page_number is 0-based but will appear in the output as 1-based.
   */
  char* GetHOCRText(int page_number);
  /**
   * The recognized text is returned as a char* which is coded in the same
   * format as a box file used in training. Returned string must be freed with
   * the delete [] operator.
   * Constructs coordinates in the original image - not just the rectangle.
   * page_number is a 0-based page index that will appear in the box file.
   */
  char* GetBoxText(int page_number);
  /**
   * The recognized text is returned as a char* which is coded
   * as UNLV format Latin-1 with specific reject and suspect codes
   * and must be freed with the delete [] operator.
   */
  char* GetUNLVText();
  /** Returns the (average) confidence value between 0 and 100. */
  int MeanTextConf();
  /**
   * Returns all word confidences (between 0 and 100) in an array, terminated
   * by -1.  The calling function must delete [] after use.
   * The number of confidences should correspond to the number of space-
   * delimited words in GetUTF8Text.
   */
  int* AllWordConfidences();

  /**
   * Applies the given word to the adaptive classifier if possible.
   * The word must be SPACE-DELIMITED UTF-8 - l i k e t h i s , so it can
   * tell the boundaries of the graphemes.
   * Assumes that SetImage/SetRectangle have been used to set the image
   * to the given word. The mode arg should be PSM_SINGLE_WORD or
   * PSM_CIRCLE_WORD, as that will be used to control layout analysis.
   * The currently set PageSegMode is preserved.
   * Returns false if adaption was not possible for some reason.
   */

https://bitbucket.org/3togo/python-tesseract/src/9ce0abe168297513d648406be5482b52d38d883b/src/baseapi_mini.h?at=master

我最初的回答

为了做到这一点,你必须写你自己的包装。在

python-tesseract很好,因为它可以让您快速启动和运行,但它并不是我所说的复杂的。您可以阅读源代码并了解其工作原理,但以下是概要:

  1. 将输入图像写入临时文件

  2. 对该文件调用tesseract命令(从命令行)

  3. 返回结果

所以,如果你想做什么特别的事,这根本行不通。在

我有一个应用程序,在那里我需要高性能,等待文件写入磁盘,等待tesseract启动并加载映像并处理它等等所花费的时间实在太多了。在

如果我没有记错的话(我不再有访问源代码的权限),我使用ctypes加载tesseract进程,设置图像数据,然后调用GetHOCRText方法。然后,当我需要处理另一个图像时,我不必等待tesseract再次加载,我只需设置图像数据并再次调用GetHOCRText。在

因此,这并不是您问题的精确解决方案,也绝对不是您可以使用的代码片段。但希望它能帮助你朝着目标前进。在

下面是关于包装外部库的另一个问题:Wrapping a C library in Python: C, Cython or ctypes?

相关问题 更多 >