简体   繁体   English

如何从python中的验证码图像中提取数字?

[英]how to extract numbers from captcha image in python?

I want to extract numbers from captcha image, so I tried this code from this answer this answer :我想从验证码图像中提取数字,所以我从这个答案这个答案中尝试了这个代码:

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()))

and it worked fine for this image:它适用于这张图片:
在此处输入图片说明
outPut: 436359
But, when I tried it on this image:但是,当我在这张图片上尝试时:
在此处输入图片说明
It gave me nothing, outPut: .它什么也没给我, outPut:
How can I modify my code to get the numbers as a string from the second image?如何修改我的代码以从第二张图像中获取数字作为字符串?

Often, getting OCR just right on an image like this has to do with the order and parameters of the transformations.通常,在像这样的图像上获得正确的 OCR 与转换的顺序和参数有关。 For example, in the following code snippet, I first convert to grayscale, then erode the pixels, then dilate, then erode again.例如,在下面的代码片段中,我首先转换为灰度,然后腐蚀像素,然后膨胀,然后再次腐蚀。 I use threshold to convert to binary (just blacks and whites) and then dilate and erode one more time.我使用阈值转换为二进制(只是黑色和白色),然后再膨胀和腐蚀一次。 This for me produces the correct value of 859917 and should be reproducible.这对我来说产生了正确的 859917 值并且应该是可重现的。

import cv2
import numpy as np
import pytesseract

file = 'sample2.jpg'
img = cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ekernel = np.ones((1,2),np.uint8)
eroded = cv2.erode(gray, ekernel, iterations = 1)
dkernel = np.ones((2,3),np.uint8)
dilated_once = cv2.dilate(eroded, dkernel, iterations = 1)
ekernel = np.ones((2,2),np.uint8)
dilated_twice = cv2.erode(dilated_once, ekernel, iterations = 1)
th, threshed = cv2.threshold(dilated_twice, 200, 255, cv2.THRESH_BINARY)
dkernel = np.ones((2,2),np.uint8)
threshed_dilated = cv2.dilate(threshed, dkernel, iterations = 1)
ekernel = np.ones((2,2),np.uint8)
threshed_eroded = cv2.erode(threshed_dilated, ekernel, iterations = 1)
text = pytesseract.image_to_string(threshed_eroded)
print(''.join(x for x in text if x.isdigit()))

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

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