Python - pytesseract 对于相似图像不一致

Python - pytesseract not consistent for similar images

比如这张图returns睡耳

虽然这张图片 returns 是正确答案

两张图片的唯一区别是高度上有 2 个像素。

我尝试过应用一些阈值,但似乎没有帮助...

from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = Image.open(path)
print(pytesseract.image_to_string(image, lang='eng'))

您可以使用 OpenCV 执行一些预处理。思路是enlarge the image with imutils,使用Otsu的阈值得到二值图像,然后添加轻微的高斯模糊。为了获得最佳检测效果,图像的形式应该是要检测的所需文本为黑色,背景为白色。这是两张图片的预处理结果:

之前->之后

Pytesseract 对两张图片的输出结果相同

BigBootyHunter2

代码

import cv2
import pytesseract
import imutils

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('1.jpg')
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.waitKey()