Pytesseract OCR 多配置选项

71

我在使用pytesseract时遇到了一些问题。我需要配置Tesseract,使其能够识别单个数字,同时也只能接受数字,因为数字零经常与字母'O'混淆。

像这样:

target = pytesseract.image_to_string(im,config='-psm 7',config='outputbase digits')
4个回答

172

tesseract-4.0.0a 支持以下 psm。如果您想进行单个字符识别,请设置 psm = 10。如果您的文本仅包含数字,则可以将 tessedit_char_whitelist=0123456789 设置为。

Tesseract-4.0.0a支持以下psm。如果您想进行单个字符识别,请设置psm = 10。如果您的文本仅包含数字,则可以将tessedit_char_whitelist = 0123456789设置为。
Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
                        bypassing hacks that are Tesseract-specific.

这里是使用多个参数的image_to_string的示例用法。

target = pytesseract.image_to_string(image, lang='eng', boxes=False, \
        config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')

6
这不是一个新问题,而是你解决方案的后续,直接从你提供的内容中推断出来。如果你能提一下用于白名单参数的Tesseract版本,会很有帮助。请再仔细阅读我的评论,你就会理解了。 - SKR
5
想要了解OEM是什么意思的人,请点击这里 https://wilsonmar.github.io/tesseract/ - X.C.
可以请您看一下这里与Tesseract相关的问题吗:https://dev59.com/ZGAKtIcB2Jgan1zneSex? - Istiaque Ahmed
可以使用string.digits(来自Python字符串模块)而不是硬编码。https://docs.python.org/3/library/string.html - Raleigh L.

14

页面分割模式:

  1. 仅方向和脚本检测(OSD)。

  2. 带有OSD的自动页面分割。

  3. 自动页面分割,但没有OSD或OCR。(未实施)

  4. 完全自动页面分割,但没有OSD。(默认)

  5. 假设文本是可变大小的单列。

  6. 假设文本是垂直对齐的单一块统一文本。

  7. 假定文本为单一块统一文本。

  8. 将图像视为单一文本行。

  9. 将图像视为单个单词。

  10. 将图像视为圆形中的单个单词。

  11. 将图像视为单个字符。

  12. 稀疏文本。按任意顺序尽可能多地查找文本。

  13. 带有OSD的稀疏文本。

  14. 原始行。将图像视为单个文本行,绕过Tesseract特定的黑客程序。

OCR引擎模式:

  1. 仅使用旧版引擎。
  2. 仅使用神经网络LSTM引擎。
  3. 使用旧版和LSTM引擎。
  4. 默认情况下,根据可用性确定。

我找到了一个全面的配置选项列表: https://muthu.co/all-tesseract-ocr-options/ - undefined

4
你遇到问题的原因是因为在4.0版本中字符限制不起作用。你需要强制使用旧模式(oem 0)来限制找到的字符数。Tesseract团队中存在某个尚未解决的漏洞。

我已经尝试过使用oem=0,但效果不佳。然而,有三个选项可供选择:tessedit_char_blacklist(不识别字符的黑名单)、tessedit_char_whitelist(识别字符的白名单)和tessedit_char_unblacklist(覆盖tessedit_char_blacklist的字符列表)。 - Vilq
修复在4.1版中,我想是这样的吧? - jtlz2

1
Tesseract版本5.0.0-alpha可以使用以下命令:(使用psm=13和oem=1或3)

pytesseract.image_to_string(export_image ,lang='eng', config='--psm 13 --oem 1 -c tessedit_char_whitelist=ABCDEFG0123456789')

请注意,采用了“eng”训练数据集:https://github.com/tesseract-ocr/tessdata_fast/blob/master/eng.traineddata

注:在单个字符的二进制输入图像上进行测试,大小约为+-60x60px。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接