python - Tesseract OCR 无法检测数字

标签 python opencv image-processing tesseract python-tesseract

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

相关文章:

python tesseract 结果在句子之间给出了不必要的额外行间隙

python - Django 管理员 : Grouped data in the list

python - 在skimage中裁剪图像?

c++ - 转换V4L2_PIX_FMT_YUYV(YUV 4 :2:2) to V4L2_PIX_FMT_YVU420(YUV 4:2:0)

python - Django 优化方法中的循环

python - 获取唯一项目的有序列表的最佳/最pythonic方式

c++ - 使用固定内核进行多次迭代的膨胀/腐 eclipse 是否类似于具有更大尺寸的等效内核的膨胀/腐 eclipse

java - 图像处理与字符提取

从 ROS kinetic 链接的 opencv3

c++ - 如何在纹理映射过程后合并两个 .obj 文件?