带有 Tesseract 的空字符串

问题描述 投票:0回答:2

我正在尝试从一个大文件中读取不同的裁剪图像,并且我设法读取其中的大部分图像,但当我尝试使用超正方体读取它们时,其中一些图像会返回空字符串。

代码就是这一行:

pytesseract.image_to_string(cv2.imread("img.png"), lang="eng")

我可以尝试什么来阅读这些图像吗?

提前致谢

编辑:

python opencv ocr tesseract python-tesseract
2个回答
11
投票

在将图像传递到

pytesseract
之前对图像进行阈值处理可以提高准确性。

import cv2
import numpy as np

# Grayscale image
img = Image.open('num.png').convert('L')
ret,img = cv2.threshold(np.array(img), 125, 255, cv2.THRESH_BINARY)

# Older versions of pytesseract need a pillow image
# Convert back if needed
img = Image.fromarray(img.astype(np.uint8))

print(pytesseract.image_to_string(img))

打印出来的

5.78 / C02

编辑: 仅对第二张图像进行阈值处理会返回

11.1
。另一个有用的步骤是将页面分割模式设置为“将图像视为单个文本行”。使用配置
--psm 7
。在第二张图像上执行此操作会返回
11.1 "202 '
,引号来自顶部的部分文本。要忽略这些,您还可以通过配置
-c tessedit_char_whitelist=0123456789.%
设置使用白名单搜索哪些字符。一切都在一起:

pytesseract.image_to_string(img, config='--psm 7 -c tessedit_char_whitelist=0123456789.%')

这将返回

11.1  202
。显然 pytesseract 很难处理这个百分比符号,我不知道如何通过图像处理或配置更改来改进它。


0
投票

伙计,你也帮了我很多!非常感谢

© www.soinside.com 2019 - 2024. All rights reserved.