python 驗證碼識別示例(一) 某個網站驗證碼識別

weixin_33686714發表於2018-08-03

某個招聘網站的驗證碼識別,過程如下

 

一: 原始驗證碼:

 

二: 首先對驗證碼進行分析,該驗證碼的數字顏色有變化,這個就是識別這個驗證碼遇到的比較難的問題,解決方法是使用PIL 中的  getpixel   方法進行變色處理,統一把非黑色的畫素點變成黑色

  

                              變色後的圖片

三: 通過觀察,發現該驗證碼有折線,需要對圖片進行降噪處理。

  

 

                                            降噪後的圖片

四:識別:

  這裡只是簡單的使用   pytesseract 模組進行識別

  識別結果如下:

        

 

     總共十一個驗證碼,識別出來了9個,綜合識別率是百分之八十。

 

總結:驗證碼識別只是簡單呼叫了一下Python的第三方庫,本驗證碼的識別難點如果給帶顏色的數字變色。

下面是程式碼:

二值化變色:

  

#-*-coding:utf-8-*-
from PIL import Image

def test(path):
    img=Image.open(path)
    w,h=img.size
    for x in range(w):
        for y in range(h):
            r,g,b=img.getpixel((x,y))
            if 190<=r<=255 and 170<=g<=255 and 0<=b<=140:
                img.putpixel((x,y),(0,0,0))
            if 0<=r<=90 and 210<=g<=255 and 0<=b<=90:
                img.putpixel((x,y),(0,0,0))
    img=img.convert('L').point([0]*150+[1]*(256-150),'1')
    return img

for i in range(1,13):
    path = str(i) + '.jpg'
    im = test(path)
    path = path.replace('jpg','png')
    im.save(path)

 

二:降噪

  

#-*-coding:utf-8-*-


# coding:utf-8
import sys, os
from PIL import Image, ImageDraw

# 二值陣列
t2val = {}


def twoValue(image, G):
    for y in xrange(0, image.size[1]):
        for x in xrange(0, image.size[0]):
            g = image.getpixel((x, y))
            if g > G:
                t2val[(x, y)] = 1
            else:
                t2val[(x, y)] = 0


# 根據一個點A的RGB值,與周圍的8個點的RBG值比較,設定一個值N(0 <N <8),當A的RGB值與周圍8個點的RGB相等數小於N時,此點為噪點
# G: Integer 影象二值化閥值
# N: Integer 降噪率 0 <N <8
# Z: Integer 降噪次數
# 輸出
#  0:降噪成功
#  1:降噪失敗
def clearNoise(image, N, Z):
    for i in xrange(0, Z):
        t2val[(0, 0)] = 1
        t2val[(image.size[0] - 1, image.size[1] - 1)] = 1

        for x in xrange(1, image.size[0] - 1):
            for y in xrange(1, image.size[1] - 1):
                nearDots = 0
                L = t2val[(x, y)]
                if L == t2val[(x - 1, y - 1)]:
                    nearDots += 1
                if L == t2val[(x - 1, y)]:
                    nearDots += 1
                if L == t2val[(x - 1, y + 1)]:
                    nearDots += 1
                if L == t2val[(x, y - 1)]:
                    nearDots += 1
                if L == t2val[(x, y + 1)]:
                    nearDots += 1
                if L == t2val[(x + 1, y - 1)]:
                    nearDots += 1
                if L == t2val[(x + 1, y)]:
                    nearDots += 1
                if L == t2val[(x + 1, y + 1)]:
                    nearDots += 1

                if nearDots < N:
                    t2val[(x, y)] = 1


def saveImage(filename, size):
    image = Image.new("1", size)
    draw = ImageDraw.Draw(image)

    for x in xrange(0, size[0]):
        for y in xrange(0, size[1]):
            draw.point((x, y), t2val[(x, y)])

    image.save(filename)
for i in range(1,12):
    path =  str(i) + ".png"
    image = Image.open(path).convert("L")
    twoValue(image, 100)
    clearNoise(image, 3, 2)
    path1 = str(i) + ".jpeg"
    saveImage(path1, image.size)

 

三:識別

  

#-*-coding:utf-8-*-

from PIL import Image
import pytesseract

def recognize_captcha(img_path):
    im = Image.open(img_path)
    # threshold = 140
    # table = []
    # for i in range(256):
    #     if i < threshold:
    #         table.append(0)
    #     else:
    #         table.append(1)
    #
    # out = im.point(table, '1')
    num = pytesseract.image_to_string(im)
    return num


if __name__ == '__main__':
    for i in range(1, 12):
        img_path = str(i) + ".jpeg"
        res = recognize_captcha(img_path)
        strs = res.split("\n")
        if len(strs) >=1:
            print (strs[0])

 

相關文章