当前位置:   article > 正文

利用python识别身份证号后获取年龄和性别信息_python身份证号码提取年月日和性别

python身份证号码提取年月日和性别

利用python识别身份证号后获取年龄和性别信息

1. 实验目的

利用python识别身份证号后,从身份证号中获取年龄和性别信息

2. 主代码

1. 身份证号码识别

# !/usr/bin/python
#-*-coding:utf-8-*-
import sys
import importlib
importlib.reload(sys)
# reload(sys)
# sys.setdefaultencoding('utf-8')

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))
    return code

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

2. 获取年龄和性别信息

import datetime


class GetInformation(object):
    def __init__(self, id):
        self.id = id
        self.birth_year = int(self.id[6:10])
        self.birth_month = int(self.id[10:12])
        self.birth_day = int(self.id[12:14])

    def get_birthday(self):
        # 通过身份证号获取出生日期
        birthday = "{0}-{1}-{2}".format(self.birth_year, self.birth_month, self.birth_day)
        return birthday

    def get_sex(self):
        # 男生:1 女生:0
        num = int(self.id[16:17])
        if num % 2 == 0:
            return 0
        else:
            return 1

    def get_age(self):
        # 获取年龄
        now = (datetime.datetime.now() + datetime.timedelta(days=1))
        year = now.year
        month = now.month
        day = now.day

        if year == self.birth_year:
            return 0
        else:
            if self.birth_month > month or (self.birth_month == month and self.birth_day > day):
                return year - self.birth_year - 1
            else:
                return year - self.birth_year

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

3. 主程序

if __name__ == '__main__':
    pic_path="id.png"
    code=identity_OCR(pic_path)
    time2 = time.time()
    print(u'总共耗时:' + str(time2 - time1) + 's')
    id = code
    birthday = GetInformation(id).get_birthday()  # 1990-11-11
    age = GetInformation(id).get_age()  # 28
    sex = GetInformation(id).get_sex()  # 1
    print('nowtime:', datetime.datetime.now().date())
    print('birthday:', birthday)
    print('age:', age)
    print('sex:', sex)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/427134
推荐阅读
相关标签
  

闽ICP备14008679号