Skip to main content
 首页 » 编程设计

python之如何仅在 pytesser 中启用数字

2025年05月04日9xing901022

我正在运行 pytesser 以在 python 中对图像进行 OCR。我第一次从页面上抓取图像时,它很好,但在接下来的几页中准确性变差,直到 87+1 为 $+$

奇怪,嗯?我的猜测是因为 pytesser(来自 tesseract for python 的端口)是为了识别单词而构建的,并将你的 OCR 放入下一个问题的上下文中。所以,没有办法禁用它,我只能将它设置为数字吗?但是 pytesser 没有太多关于它的文档,所以我继续查看 tesseract 常见问题解答,但我并没有真正得到代码。

Use

TessBaseAPI::SetVariable("tessedit_char_whitelist", "0123456789"); 

BEFORE calling an Init function or put this in a text file called tessdata/configs/digits:

tessedit_char_whitelist 0123456789 

and then your command line becomes:

tesseract image.tif outputbase nobatch digits 

Warning: Until the old and new config variables get merged, you must have the nobatch parameter too.

我猜它适用于 C 或 C++。有没有办法在 python 中做到这一点?或者更好的是,禁用 OCR 上下文?

请您参考如下方法:

在 python 中:

import tesseract 
ocr = tesseract.TessBaseAPI(); 
ocr.Init(".","eng",tesseract.OEM_TESSERACT_ONLY) 
ocr.SetVariable("tessedit_char_whitelist", "0123456789") 

你可能还想:

ocr.SetVariable("classify_enable_learning", "0") 
ocr.SetVariable("classify_enable_adaptive_matcher", "0")