python 身份证识别器_python 识别身份证号码

# !/usr/bin/python

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

import sys

import time

time1 = time.time()

from PIL import Image

import pytesseract

###########二值化算法

def binarizing(img, threshold):

pixdata = img.load()

w, h = img.size

for y in range(h):

for x in range(w):

if pixdata[x, y] < threshold:

pixdata[x, y] = 0

else:

pixdata[x, y] = 255

return img

###########去除干扰线算法

def depoint(img): # input: gray image

pixdata = img.load()

w, h = img.size

for y in range(1, h - 1):

for x in range(1, w - 1):

count = 0

if pixdata[x, y - 1] > 245:

count = count + 1

if pixdata[x, y + 1] > 245:

count = count + 1

if pixdata[x - 1, y] > 245:

count = count + 1

if pixdata[x + 1, y] > 245:

count = count + 1

if count > 2:

pixdata[x, y] = 255

return img

########身份证号码识别

def identity_OCR(pic_path):

#####身份证号码截图

img1 = Image.open(pic_path)

w, h = img1.size

##将身份证放大3倍

out = img1.resize((w * 3, h * 3), Image.ANTIALIAS)

region = (125 * 3, 200 * 3, 370 * 3, 250 * 3)

# 裁切身份证号码图片

cropImg = out.crop(region)

# 转化为灰度图

img = cropImg.convert('L')

# 把图片变成二值图像。

img1 = binarizing(img, 100)

img2 = depoint(img)

code = pytesseract.image_to_string(img2)

print("识别该身份证号码是:" + str(code))

if __name__ == '__main__':

pic_path = "./1.png"

identity_OCR(pic_path)

time2 = time.time()

print(u'总共耗时:' + str(time2 - time1) + 's')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值