Tesseract OCR 无法检测数字
Tesseract OCR having trouble detecting numbers
我正在尝试在 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 能够识别这些数字有建议?
Starting Image
What I end up with
使用 pytesseract.image_to_string(img, config='--psm 8') 或尝试不同的配置以查看图像是否会被识别。这里有用link
您的问题出在分页模式上。 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.
我正在尝试在 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 能够识别这些数字有建议?
Starting Image
What I end up with
使用 pytesseract.image_to_string(img, config='--psm 8') 或尝试不同的配置以查看图像是否会被识别。这里有用link
您的问题出在分页模式上。 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.