Pytesseract OCR 将“o”识别为“0”

问题描述 投票:0回答:1

我正在尝试使用 pytesseract 库读取此图像上的文本。

原始截图.png

这是我的代码:

path = 'original-screenshot.png'

image = cv2.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                cv2.THRESH_BINARY_INV, 101, -69)

cv2.imwrite('screenshot.png', image)

custom_config = r'--oem 3 --psm 7 -l eng -c tessedit_char_whitelist=abcdefghigklmnopqrstuvwxyz0123456789'
text = pytesseract.image_to_string(image, config=custom_config)

print(text)

处理后的图像:

截图.png

文本输出:

01991f5

预期输出:

o1991f5

python ocr tesseract python-tesseract
1个回答
0
投票

你必须调整图像大小。不过你的图片真的是极限了:

import cv2
import numpy as np
import pytesseract
 
img = cv2.imread('o.png',cv2.IMREAD_UNCHANGED)

grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 150, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

# resize image
scale_percent = 6.4 # percent of original size
width = int(blackAndWhiteImage.shape[1] * scale_percent / 100)
height = int(blackAndWhiteImage.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(blackAndWhiteImage, dim, interpolation = cv2.INTER_AREA)

# OCR resized Black & White image
pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
custom_config = r'--psm 13 --oem 3 -c tessedit_char_whitelist=abcdefghijklmnopqrstuvwxyz01234567890' 
tex = pytesseract.image_to_string(resized, config=custom_config)
print(tex)

# Display cropped image
cv2.imshow("cropped", resized)
 
# Save the cropped image
cv2.imwrite("Cropped Image.jpg", resized)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

结果: enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.