【问题标题】:How to make image more contrast, grayscale then get all characters exactly with PIL and pytesseract?如何使图像更具对比度,灰度然后完全使用 PIL 和 pytesseract 获得所有字符?
【发布时间】:2019-10-10 01:36:54
【问题描述】:

请在此处下载附件并保存为/tmp/target.jpg


可以看到jpg中有0244R,我用下面的python代码提取字符串:

from PIL import Image
import pytesseract
import cv2
filename = "/tmp/target.jpg"
image = cv2.imread(filename)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, threshold = cv2.threshold(gray,55, 255, cv2.THRESH_BINARY)
print(pytesseract.image_to_string(threshold))

我得到的是

0244K

正确的字符串是0244R,如何使图像更具对比度,灰度然后用PIL和pytesseract得到所有字符? 这是生成图像的网页:

http://www.crup.cn/ValidateCode/Index?t=0.14978241776661583

【问题讨论】:

  • 风险在于人们将提供仅适用于该图像的解决方案。你有生成这张图片的代码吗?
  • 上次您提出此问题stackoverflow.com/questions/57183997/… 时,向您展示了执行此类清洁所需的过程。这不是一个完美的过程。
  • @potential answerers,这是一个用于在中国人民大学出版社创建帐户和登录的机器人验证crup.cn/Account/Login 我不知道 OP 打算用它做什么,但如果你在中国,帮助规避此问题的 OP 可能不符合犹太教规。
  • @Scott 仅供参考!
  • 每天登录网站手动获取积分,我厌倦了这种操作,我想编写一个程序来登录并为我获取积分。

标签: python-3.x python-tesseract


【解决方案1】:

如果对输入图像应用adaptive-thresholdingbitwise-not 操作,结果将是:

现在,如果您删除特殊字符,例如(点、逗号等)

txt = pytesseract.image_to_string(bnt, config="--psm 6")
res = ''.join(i for i in txt if i.isalnum())
print(res)

结果将是:

O244R

代码:


import cv2
import pytesseract

img = cv2.imread("Aw6sN.jpg")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY_INV, 23, 100)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 6")
res = ''.join(i for i in txt if i.isalnum())
print(res)

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 2011-05-07
    • 2018-08-15
    • 1970-01-01
    • 2016-06-05
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多