【发布时间】:2021-11-01 08:21:06
【问题描述】:
我尝试使用 pytesseract 执行此操作,但它只能识别打印的文本,并且无法与我的图像一起使用。我没有时间写自己的神经网络,想用一个现成的解决方案作为pytesseract。我知道它可以做到这一点,但这段代码运行不正确。
import cv2
import pytesseract
import imutils
image = cv2.imread('test/task.jpg')
image = imutils.resize(image, width=700)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(image, lang='eng', config='--psm 6')
print(data)
cv2.imshow('thresh', image)
cv2.imwrite('images/thresh.png', thresh)
cv2.waitKey()
此代码返回错误答案。
ti | ee
ares” * ae
de le lc ld
我做错了什么?
附:我使用自适应阈值转换了我的图像,它看起来像这样,但代码仍然可以正常工作(现在我只需调用 image_to_string() 方法并转换良好的图像)
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\USER\AppData\Local\Tesseract-OCR\tesseract.exe'
image = cv2.imread('output.png')
data = pytesseract.image_to_string(image, lang='eng', config='--psm 6')
print(data)
cv2.waitKey(0)
a Oe '
Pee ee
eee ee ee ee
re
eB
STI AT TTT
“Shen if
ae 6
jal ne
yo l
a) Ne es oe
Seaneaeer =
ee es ee
a en ee
ee rt
【问题讨论】:
-
你的阈值图片是什么样子的?
-
@dennlinger,我已将其添加到帖子中
-
让我这样说吧:作为人类,你能从图像中得到正确的输出吗?如果答案是“否”,那么您的程序也将无法做到。看来你转换图片的方式有问题。
-
正如@dennlinger 提到的,您必须事先使图像清晰。我认为自适应阈值在这张图片上会做得很好。请尝试一下。
-
@LasvenLoke,我做到了,现在图像转换正确(我在帖子中添加了照片),但代码仍然不正确。 (输出在帖子中)
标签: python machine-learning neural-network image-recognition python-tesseract