gpt4 book ai didi

python - 是什么导致 pytesseract 根据是否使用 opencv 或枕头读取双行图像的顶部或底部文本行?

转载 作者:行者123 更新时间:2023-12-02 15:59:16 26 4
gpt4 key购买 nike

编辑:感谢 Nathancy,我忘了处理解决阅读问题的图像。仍然想知道是什么让 Tesseract 只读取未处理图像的顶部或底部(相同的图像,两种不同的结果)

原件:
我有一个包含两行文本的图像:
random test image for pytesseract
当我使用 PIL Image 在 python (IDLE Python 3.6) 中打开图像并使用 pytesseract 提取字符串时,它只能正确提取最后一行/底线。文本的上一行是乱码。(见下面的代码部分)但是,当我使用 opencv 打开图像并使用 pytesseract 提取字符串时,它只会正确提取顶部/上一行,同时将第二/文本的底行。(另见下面的代码部分)
这是代码:

>>> from PIL import Image, ImageFilter
>>> import pytesseract
>>> pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
>>> import cv2

>>> img = Image.open(r"C:\Users\user\MyImage.png")
>>> img2 = cv2.imread(r"C:\Users\user\MyImage.png", cv2.IMREAD_COLOR)


>>> print(pytesseract.image_to_string(img2))
Pet Sock has 448/600 HP left
A ae eee PER eats ae

>>> print(pytesseract.image_to_string(img))
Le TL
JHE has 329/350 HP left.

当我使用 pytesseract.image_to_boxes在两个 imgimg2它将使用不同的字母为某些位置显示相同的边界框(仅显示包含相同框的 2 条提取线)
>>> print(pytesseract.image_to_boxes(img2))
A 4 6 10 16 0

>>> print(pytesseract.image_to_boxes(img))
J 4 6 10 16 0

当我使用 pytesseract.image_to_data在两个 imgimg2它在正确读取的行上显示出非常高 (95+) 的置信度,在乱码的行上显示出非常低 (30-) 的置信度。 Excel table output of image_to_data编辑:excel表是相应的img2和img
我摆弄了 psm 配置值(我都试过了),除了在设置上创建更多垃圾:5、7、8、9、10、13;还有一些给出错误:0, 2;它给出的结果与默认值没有不同(我相信是 3)
我一定是在犯一些新手错误,但我无法理解为什么会发生这种情况。如果有人能在正确的方向上发光,那就太棒了。
该图像只是我放置的 OCR 测试的合适但随机的图像。除了尝试 pytesseract 之外,别无其他意图。

最佳答案

每当使用 Pytesseract 执行 OCR 时,重要的是对图像进行预处理,以便 文字为黑色,背景为白色 .我们可以用简单的阈值来做到这一点

enter image description here

Pytesseract 的输出

Pet Sock has 448/600 HP left
JHE has 329/359 HP left.

代码
import cv2
import pytesseract

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

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]

data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

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

关于python - 是什么导致 pytesseract 根据是否使用 opencv 或枕头读取双行图像的顶部或底部文本行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58824094/

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