我正在尝试用Python中的tesseract检测一些数字。下面您将看到我的起始图像以及我可以将其简化为的内容。这是我用来获取它的代码。
import pytesseract
import cv2
import numpy as np
pytesseract.pytesseract.tesseract_cmd = "C:\\Users\\choll\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe"
image = cv2.imread(r'64normalwart.png')
lower = np.array([254, 254, 254])
upper = np.array([255, 255, 255])
image = cv2.inRange(image, lower, upper)
image = cv2.bitwise_not(image)
#Uses a language that should work with minecraft text, I have tried with and without, no luck
text = pytesseract.image_to_string(image, lang='mc')
print(text)
cv2.imwrite("Wartthreshnew.jpg", image)
cv2.imshow("Image", image)
cv2.waitKey(0)
我最终得到了白色背景上的黑色数字,这看起来不错,但超立方体仍然无法检测到这些数字。我还注意到数字非常参差不齐,但我不知道如何解决这个问题。有人建议我如何让 tesseract 能够识别这些数字吗?
您的问题出在页面分割模式上。 Tesseract 以不同的方式分割每个图像。当您没有选择合适的 PSM 时,它将进入模式 3,该模式是自动的,可能不适合您的情况。我刚刚尝试过您的图像,它与 PSM 6 完美配合。
df = pytesseract.image_to_string(np.array(image),lang='eng', config='--psm 6')
这些是目前可用的所有 PSM:
0 Orientation and script detection (OSD) only.
1 Automatic page segmentation with OSD.
2 Automatic page segmentation, but no OSD, or OCR.
3 Fully automatic page segmentation, but no OSD. (Default)
4 Assume a single column of text of variable sizes.
5 Assume a single uniform block of vertically aligned text.
6 Assume a single uniform block of text.
7 Treat the image as a single text line.
8 Treat the image as a single word.
9 Treat the image as a single word in a circle.
10 Treat the image as a single character.
11 Sparse text. Find as much text as possible in no particular order.
12 Sparse text with OSD.
13 Raw line. Treat the image as a single text line,
bypassing hacks that are Tesseract-specific.
使用 pytesseract.image_to_string(img, config='--psm 8') 或尝试不同的配置来查看图像是否会被识别。此处有用的链接Pytesseract OCR 多个配置选项
使用
pytesseract.image_to_string(img, lang='mal+eng', config='--psm 6 --oem 3 -c tessedit_char_unblacklist=0123456789')