从图像中读取文本,由于字体而面临问题

分享于2023年02月22日 ocr python python-tesseract tesseract 问答
【问题标题】:Reading text from image, facing problem because of the font从图像中读取文本,由于字体而面临问题
【发布时间】:2023-02-21 18:13:29
【问题描述】:

我正在尝试读取此图像并在图像中进行算术运算。出于某种原因,我无法阅读 7,因为它的字体。我对图像处理比较陌生。你能帮我解决问题吗?我尝试对图像进行像素化,但这没有帮助。

import cv2
import pytesseract
from PIL import Image

img = cv2.imread('modules/visual_basic_math/temp2.png', cv2.IMREAD_GRAYSCALE)

thresh = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]

print(pytesseract.image_to_string(img, config='--psm 6'))

我得到的回应是 -

+44 849559
+46653% 14
+7776197
+6415995
+*9156346
x4463310
+54Q%433
+1664 20%


【解决方案1】:

现在,tesseract 有点过时了。还有更强大的库。我推荐 PaddleOCR。安装它:

  • pip install paddlepaddle
  • pip install paddleocr

然后:


from paddleocr import PaddleOCR

ocr = PaddleOCR(use_angle_cls=True, lang='es')
predictions = ocr.ocr("ietDJ.png")[0]

filtered_text = []
for pred in predictions:
    filtered_text.append(pred[-1][0])

filtered_text = [t.replace(" ", "") for t in filtered_text] # Remove spaces


['+4487559', '+4665714', '+7776157', ':6415995', ':9156346', 'x4463310', '-54q7433', '+1664207']

输出不完全正确(除法符号是 : 其中一个是错误的)。此外,它还会将 9 与 q 混淆。但是,结果更好,库的使用和 tesseract 一样舒服。

希望能帮助到你!

【讨论】: