python - Tesseract 将 1 和 0 检测为 L 和 O

标签 python image-processing ocr tesseract python-tesseract

在此图像中,tesseract 检测到的文本为 LOOOPCS,但它是 1000PCS。我正在使用的命令是

tesseract "item_04.png" stdout --psm 6

我已经尝试了所有 psm 值 0 到 13

actual image

根据其他博客的建议以及 SO 和互联网上的问题,还尝试了图像裁剪和阈值处理。

after clipping and thresholding

after clipping only

还尝试了-c tessedit_char_whitelist=PCS0123456789,但得到了00PCS。 但我没有得到1000PCS。有人可以尝试这些并让我知道我错过了什么吗?

编辑:
根据 @nathancy 给出的建议,尝试使用 - cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU ,它适用于此 1 和 0,但对于下图失败。它被检测为 LL8gPcs:
another failing image

最佳答案

您需要对图像进行预处理。一种简单的方法是采用 Otsu 阈值,然后反转图像,使文本为黑色,背景为白色。这是使用 Pytesseract OCR 和 --psm 6 处理的图像和结果。

enter image description here

结果

1000PCS

代码

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Invert and perform text extraction
thresh = 255 - thresh
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

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

关于python - Tesseract 将 1 和 0 检测为 L 和 O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60127319/

相关文章:

Python Popen 输出到 C 程序,fget 在循环中读取相同的 stdin

python - 如何计算相对值再平衡的if语句/错误: "The truth value of a Series is ambiguous"

python - 为什么 numpy ma.average 比 arr.mean 慢 24 倍?

c++ - 相机自动白平衡

algorithm - SIFT算法能否在PC端实时快速提取特征?

dataset - 免费包含 OCR 基准测试集的资源

Python 登录脚本;用户名和密码位于单独的文件中

opencv - 在手写流程图中检测几何形状

python - Pyinstaller 和 Tesseract OCR

r - 用 R 做 OCR