【问题标题】:How to extract numbers from a complex captcha如何从复杂的验证码中提取数字
【发布时间】:2019-10-25 05:48:17
【问题描述】:

我正在尝试为以下图像解析验证码

!https://ibb.co/35X723J

我尝试过使用 tessaract

data = br.open(captchaurl).read()
b = bytearray(data)
save = open(filename, 'wb')
save.write(data)
save.close()
ctext= pytesseract.image_to_string(Image.open(filename))

【问题讨论】:

  • 我认为 tesseract 无法读取它。它需要更清晰的图像

标签: python tesseract captcha python-tesseract


【解决方案1】:

这是一种解决方法。你需要清除一点图像,但你不会得到完美的结果。请尝试以下操作:

try:
    from PIL import Image
except ImportError:
    import Image
import pytesseract
import cv2

file = 'sample.jpg'

img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, None, fx=10, fy=10, interpolation=cv2.INTER_LINEAR)
img = cv2.medianBlur(img, 9)
th, img = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4,8))
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite("sample2.jpg", img)


file = 'sample2.jpg'
text = pytesseract.image_to_string(file)
print(''.join(x for x in text if x.isdigit()))

【讨论】:

    【解决方案2】:

    选项 1:

    我认为使用 Pytesseract 应该可以解决这个问题。我尝试了您的代码,当我将精确裁剪的验证码图像作为输入到 pytesseract 中时,它给了我以下结果:

    输入图像:

    输出:

    print(ctext)
     '436359 oS'
    

    我建议您不要将完整的页面 url 作为 pytesseract 的输入。而是将确切的图片 url 指定为“https://i.ibb.co/RGn9fF5/Jpeg-Image-CS2.jpg”,这将只包含图片。

    对于输出中多余的 'oS' 字符,您可以进行字符串操作以删除输出中除数字以外的字符。

    re.sub("[^0-9]", "", ctext)
    

    选项 2:

    您还可以使用 google 的 OCR 来完成此操作,从而为您提供准确的结果而不会出错。虽然我已经向你展示了它的网络界面,但谷歌有很好的 python 库,你可以通过它使用 python 本身来完成这个。看起来像这样:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 2015-04-19
      相关资源
      最近更新 更多