【Pythons實現識別身份證號碼】

利用Python調研pytesseract庫來進行簡單的身份證號碼識別,在使用直接要安裝相關庫,具體流程百度

# coding=utf-8
from PIL import Image
import pytesseract
# 識別身份證

# 二值化
def binarizing(img, threshold):
    '''

    :param img:
    :param threshold:
    :return:
    '''
    pix = img.load()   # 返回一個用於讀取和修改像素的像素訪問對象
    m, n = img.size
    for y in range(n):
        for x in range(m):
            if pix[x, y] > threshold:
                pix[x, y] = 255
            else:
                pix[x, y] = 0

    return img

# 去除干擾線
def depoint(img):
    '''

    :param img:
    :return:
    '''

    pix = img.load()
    m, n = img.size
    for y in range(1, n-1):
        for x in range(1, m-1):
            count = 0
            if pix[x, y-1] > 245:
                count = count + 1
            if pix[x, y+1] > 245:
                count = count + 1
            if pix[x-1, y] > 245:
                count = count + 1
            if pix[x+1, y] > 245:
                count = count +1
            if count > 2:
                pix[x, y] = 255

    return img


if __name__ == '__main__':
    image = Image.open('idcard.png')
    image = image.convert('L')
    img = binarizing(image, 100)
    img = depoint(img)
    num = pytesseract.image_to_string(img)
    print(str(num))







運行結果:




參考鏈接:http://blog.csdn.net/u013421629/article/details/72677964

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章