Python Tesseract提高OCR准确性

3

我的图片很简单,但是tesseract在给我正确的答案方面并不成功。

代码:

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

这里输入图片描述

示例图片给出了一个结果

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个回答

2
尝试将图像反转,然后进行二值化/阈值处理,以获得白底黑字的图像,然后再使用OCR。当然,输入图像的质量越好、文本越清晰,OCR 的结果就会越好。
参考 this post 中有关在 Python 中对图像进行二值化的技巧。
我使用了一个外部工具将其改为黑白颜色,并得到了以下图像。

Inverted and Binarized


0

我有一个四步解决方案

结果
平滑处理 enter image description here
阈值处理 enter image description here
上采样
+
腐蚀处理
enter image description here
enter image description here
Pytesseract SARYN PRIME NEUVROPTICS
BLUEPRINT

代码


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)

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