Python tesseract提高了OCR的准确性

时间:2016-11-19 13:26:34

标签: python machine-learning ocr tesseract python-tesseract

我有非常简单的照片,但是tesseract没有成功地给我正确答案。

代码:

pytesseract.image_to_string(image, lang='eng')

enter image description here

示例图片给出

的结果
SARVN PRIM E N EU ROPTICS\nBLU EPRINT

我还尝试将自己的单词添加到字典中,如果它能让事情变得更好,但仍然没有。

pytesseract.image_to_string(image, lang='eng', config="--user-words words.txt")

我的单词列表看起来像这样

SARYN
PRIME
NEUROPTICS
BLUEPRINT

我该如何处理这个问题,也许我必须在预测之前转换图像?文本颜色可能在几种颜色之间变化,但背景总是黑色

2 个答案:

答案 0 :(得分:2)

尝试反转图像,然后执行二值化/阈值处理以在使用尝试OCR之前在白色背景上获取黑色文本。

有关使用Python进行图像二值化的提示,请参阅this post

当然,输入图像的质量越好,文本越清晰,OCR结果就越好。

我使用外部工具将其更改为白底黑色并获得下面的图像。

Inverted and Binarized

答案 1 :(得分:0)

我有一个四步解决方案

<头>
结果
平滑 enter image description here
阈值 enter image description here
上采样
+
腐蚀
enter image description here
enter image description here
Pytesseract SARYN PRIME NEUVROPTICS
蓝图

Code


import cv2
import pytesseract

img = cv2.imread('j0nNV.png')
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blr = cv2.GaussianBlur(gry, (3, 3), 0)
thr = cv2.threshold(blr, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
(h_thr, w_thr) = thr.shape[:2]
s_idx = 0
e_idx = int(h_thr/2)

for _ in range(0, 2):
    crp = thr[s_idx:e_idx, 0:w_thr]
    (h_crp, w_crp) = crp.shape[:2]
    crp = cv2.resize(crp, (w_crp*2, h_crp*2))
    crp = cv2.erode(crp, None, iterations=1)
    s_idx = e_idx
    e_idx = s_idx + int(h_thr/2)
    txt = pytesseract.image_to_string(crp)
    print(txt)
    cv2.imshow("crp", crp)
    cv2.waitKey(0)