gpt4 book ai didi

python - Tesseract OCR 无法检测数字

转载 作者:行者123 更新时间:2023-12-05 04:47:13 24 4
gpt4 key购买 nike

我正在尝试在 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

最佳答案

您的问题与页面分割模式有关。 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 OCR 无法检测数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68562138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com