简体   繁体   English

无法使用 pytesseract.image_to_string 从图像中读取文本

[英]Unable to read text from Image using pytesseract.image_to_string

这就是验证码的样子。

The problem here is I need to remove the lines and write code to recognize the characters.这里的问题是我需要删除这些行并编写代码来识别字符。 Till now I have seen solutions, where the char was in solid, but this has char with double border.到目前为止,我已经看到了解决方案,其中 char 是实心的,但是这个 char 有双边框。

For this specific captcha , there's quite a simple solution.对于这个特定的验证码,有一个非常简单的解决方案。 But, there's no guarantee for this approach to work on other, even very similar captchas – due to the "nature" of captchas as already mentioned in the comments, and in general when dealing with image-processing tasks with limited provided input data.但是,由于评论中已经提到验证码的“性质”,并且通常在处理提供有限输入数据的图像处理任务时,不能保证这种方法适用于其他甚至非常相似的验证码。

  • Read the image as grayscale.将图像读取为灰度。

  • Threshold the image at nearly white cutoff.将图像阈值设置为接近白色的截止点。

    阈值

  • Flood fill the "background" with black.洪水用黑色填充“背景”。

    洪水填满

  • Run pytesseract with -psm 6 option.使用-psm 6选项运行pytesseract

That'd be the whole code:这就是整个代码:

import cv2
import pytesseract

# Read image as grayscale
img = cv2.imread('FuZEJ.png', cv2.IMREAD_GRAYSCALE)

# Threshold at nearly white cutoff
thr = cv2.threshold(img, 224, 255, cv2.THRESH_BINARY)[1]

# Floodfill "background" with black
ff = cv2.floodFill(thr, None, (0, 0), 0)[1]

# OCR using pytesseract
text = pytesseract.image_to_string(ff, config='--psm 6').replace('\n', '').replace('\f', '')
print(text)
# xwphs

Caveat: I use a special version of Tesseract from the Mannheim University Library .警告:我使用曼海姆大学图书馆的特殊版本的 Tesseract。

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
PyCharm:       2021.1.1
OpenCV:        4.5.1
pytesseract:   5.0.0-alpha.20201127
----------------------------------------

I would try a mask:我会尝试一个面具:

import cv2
import numpy as np

def process(img): # To process the image
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, img_gray = cv2.threshold(img_gray, 224, 255, cv2.THRESH_TOZERO_INV)
    img_blur = cv2.GaussianBlur(img_gray, (7, 7), 6)
    img_canny = cv2.Canny(img_blur, 0, 100)
    return cv2.dilate(img_canny, np.ones((1, 5)), iterations=1)

def get_mask(img): # To generate the mask
    mask = np.zeros(img.shape[:2], 'uint8')
    contours, _ = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    for cnt in contours:
        cv2.drawContours(mask, [cnt], -1, 255, -1)
    return mask

def crop(img, mask): # To mask an image and use white background
    bg = np.full(img.shape, 255, 'uint8')
    fg = cv2.bitwise_or(img, img, mask=mask)            
    fg_back_inv = cv2.bitwise_or(bg, bg, mask=cv2.bitwise_not(mask))
    return cv2.bitwise_or(fg, fg_back_inv)

img = cv2.imread("image.png")
img = cv2.pyrUp(cv2.pyrUp(img)) # To enlarge image by 4x
cv2.imshow("Masked Image", crop(img, get_mask(img)))
cv2.waitKey(0)

Before:前:

在此处输入图像描述

After:后:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM