使用Python从图片中识别数字

3
我尝试从游戏截图中提取数字。 Text 我想要提取:
98 3430 5/10
from PIL import Image
import pytesseract 
image="D:/img/New folder (2)/1.png"
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
text = pytesseract.image_to_string(Image.open(image),lang='eng',config='--psm 5')
print(text)

输出是乱码。

‘t hl) keteeeees
ek pSlaerenen
JU) pgrenmnreserenny
Rates B
d dali eas. 5
cle aM (Sores
|, S| pgranmrerererecons
a cee 3
pea 3
oS :
(geo eenee
ey
=
es A

你能上传这张图片吗? - Arun
图片位于 https://i.imgur.com/QSOcVRF.png - Cesar
你需要一个更强大的预处理流程,才能正确地检测数字。必须尽可能清晰地进行字符分割,此外,文本也有变形,你需要对其进行矫正。 - stateMachine
2个回答

3

好的,所以我尝试将其转换为灰度、反转对比或使用不同的阈值,但似乎都相当不准确。 问题似乎在于倾斜和较小的数字。你没有更高分辨率的图像吗? 我能得到最精确的代码如下。

import cv2
import pytesseract
import imutils

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
img = cv2.imread('D:/img/New folder (2)/1.png') #test.png is your original image
img = imutils.resize(img, width=1400)
crop = img[340:530, 100:400]

data = pytesseract.image_to_string(crop,config=' --psm 1 --oem 3  -c tessedit_char_whitelist=0123456789/')
print(data)

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

否则,我建议您采用以下方法之一,如类似问题中所述,或者这篇文章


0

如果文本被设计包围,Tesseract 会受到很大的影响。

不妨尝试在 OpenCV 中使用 findContours(在轻微模糊和膨胀后)代替 Tesseract。

这样你就可以得到边界框,它可能也覆盖了那些文本。


最好将此留作注释。 - Jeppe

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