【问题标题】:OCR small image with python用python进行OCR小图像
【发布时间】:2018-01-17 22:49:39
【问题描述】:

我需要用 python 2.7 ocr 小图像,包含价格。

如您所见,图像非常小,并且包含一些值。

我的目标是解码为:654.10

我尝试使用 tesseract,但没有运气。

import pytesseract
print(pytesseract.image_to_string(Image.open('example.png') , lang='eng', boxes=False,config='--psm 10 --eom 3 -c tessedit_char_whitelist=€0123456789'))

我明白了:

€553 1

我尝试使用在线转换器并像魅力一样工作 (https://convertio.co/it/ocr/),所以我认为这是可能的。

有人有更好的主意吗?

谢谢

(对不起我的英语不好)

更新:

我尝试在没有任何运气的情况下对图像进行阈值...再次...

import cv2
img = cv2.imread('cropped.png')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold2 = cv2.threshold(grayscaled,125,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('threshold.jpeg',threshold2)
print(pytesseract.image_to_string(Image.open('threshold.jpeg') , lang='eng', boxes=False,config='--psm 10 --eom 3 -c tessedit_char_whitelist=0123456789'))

输出:553 0 图片输出:

ps。我裁剪了原始图像,删除了€符号......但仍然出现错误。

谢谢

【问题讨论】:

  • 预处理图像以使文本更清晰可能会有所帮助。类似于 Photoshop 中的“阈值”。
  • @Paandittya 不幸的是它不起作用......无论如何......另一个想法?谢谢
  • 所以我正在考虑将图像大小调整为 2x-3x,然后尝试从中获取文本。在这个过程中,我登陆了这个线程Link。如果您还没有尝试过,请尝试一下。这似乎与您面临的类似问题。希望这能解决问题。
  • @Paandittya 非常感谢。
  • 不客气,伙计:)

标签: python-2.7 ocr python-tesseract


【解决方案1】:

我通过调整图像大小然后应用阈值来解决我的问题。

此代码将增加图像的尺寸:

    basewidth = 300
    img = Image.open(saved_location)
    wpercent = (basewidth/float(img.size[0]))
    hsize = int((float(img.size[1])*float(wpercent)))
    img = img.resize((basewidth,hsize), Image.ANTIALIAS)
    img.save(saved_location)

感谢用户在 cmets 发帖。

【讨论】:

    猜你喜欢
    • 2018-06-20
    • 1970-01-01
    • 2020-01-25
    • 2015-11-25
    • 2019-06-04
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多