【问题标题】:How to process this captcha image for Pytesseract?如何为 Pytesseract 处理这个验证码图像?
【发布时间】:2021-01-28 19:19:13
【问题描述】:

我想用 Pytesseract 自动解决像这样的验证码(所有这些验证码都是红色背景和白色字母)

我一直在尝试处理图像以使 Pytesseract 能够读取它,但没有成功。很高兴收到您处理此图像的想法。这是我的代码:

import cv2
import pytesseract

tessdata_dir_config = '--tessdata-dir "C:\\Program Files\\Tesseract-OCR\\tessdata"'
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

img = cv2.imread("captcha.png")
img = cv2.resize(img, None, fx=2, fy=2)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
adaptive = cv2.adaptiveThreshold(
    gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 85, 20)
print((pytesseract.image_to_string(img, config=tessdata_dir_config)).strip())
print((pytesseract.image_to_string(gray, config=tessdata_dir_config)).strip())
print((pytesseract.image_to_string(adaptive, config=tessdata_dir_config)).strip())

cv2.imshow("Captcha", img) # Output: IMQW
cv2.imshow("Gray", gray) # Output: IMOW
cv2.imshow("Adaptive", adaptive) # Output: IMOW,

cv2.waitKey(7000)

【问题讨论】:

  • Pytesseract 工作得更好,但使用普通文本。对于非标准文本(如验证码),您可能需要自己学习Deep Network,但这并不容易。
  • CAPTCHA 的重点在于 OCR 难以阅读。

标签: python ocr python-tesseract


【解决方案1】:

我有一个三步解决方案


    1. 调整大小
    1. 结束
    1. 阈值

第 1 步:调整大小


调整图像大小使 OCR 算法能够检测输入图像中的字符或数字笔划。

第 2 步:关闭


Closing是一种形态学运算,旨在去除输入图像中的小孔。

如果我们仔细观察QW 字符由许多小孔组成。

第 3 步:阈值


我们将应用simple-threhsolding 对图像进行二值化。我们的目标是从图像中移除任何残留的伪影。

Resize Closing Threshold

结果:

IMQW

代码:


import cv2
from pytesseract import image_to_string

img = cv2.imread("QUfxY.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(h, w) = gry.shape[:2]
gry = cv2.resize(gry, (w*2, h*2))
cls = cv2.morphologyEx(gry, cv2.MORPH_CLOSE, None)
thr = cv2.threshold(cls, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
txt = image_to_string(thr)
print(txt)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2020-01-15
    相关资源
    最近更新 更多