吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2856|回复: 27
收起左侧

[Python 原创] 使用百度云获取身份证信息,python源代码

  [复制链接]
oenrjtt 发表于 2023-3-22 08:24
第一次发帖,不知道 是不是合规,未编译直接发代码,能用到的自行修改
若有违规,请版主第一时间删帖
以下代码适合于在文件夹中多个文件批量提取身份证信息

[Python] 纯文本查看 复制代码
from aip import AipOcr
import os
import imghdr
import sys
#import filetype


#代{过}{滤}理服务器访问设置设置
#os.environ["http_proxy"] = "http://localhost:8888"
#os.environ["https_proxy"] = "http://localhost:8888"


#百度云账号,自己填写上自己的百度云开发帐号信息
APP_ID='99999999'
API_KEY='xxxxxxxxxxxxxxxxxxxxx'
SECRET_KEY='xxxxxxxxxxxxxxxxxxxxxx'
#调用百度云接口
client=AipOcr(APP_ID,API_KEY,SECRET_KEY)
def get_file_content(filePath):
    with open(filePath,'rb') as fp:
            return fp.read()

def duqusfzxx(imagefile):
    image=get_file_content(imagefile)
    #识别身份证正面
    idCardSide="front"
    #识别身份证背面
    #idCardSide="back"
    client.idcard(image,idCardSide)
    options={}
    options["detect_direction"]="true" #是否检测图像朝向,默认不检测
    options["detect_risk"]="false"  #是否开启身份证风险类型(身份证复印件)
    #带参数调用身份证识别
    result=client.idcard(image,idCardSide,options)
    #识别为一个list
    #    {'姓名': {'words': '奥巴马', 'location': {'top': 52, 'left': 61, 'width': 34, 'height': 17}}, '民族': {'words': '哈尼', 'location': {'top': 69, 'left': 137, 'width': 9, 'height': 14}}, '住址': {'words': '盛顿特区宜宾法尼亚大道1600号白官', 'location': {'top': 113, 'left': 70, 'width': 118, 'height': 49}}, '公民身份号码': {'words': '', 'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0}}, '出生': {'words': '19610804', 'location': {'top': 90, 'left': 68, 'width': 101, 'height': 26}}, '性别': {'words': '男', 'location': {'top': 78, 'left': 65, 'width': 14, 'height': 15}}}
    id_msg=''
    if isinstance(result,dict):
        words=result['words_result']
        #print(words)
        
        #身份证用下面的两个
        #id_msg=words[u'姓名']['words']+'\t'+words[u'公民身份号码']['words']
        id_msg=words[u'姓名']['words']+'\t'+words[u'公民身份号码']['words']+'\t'+words[u'住址']['words']
        
        print(id_msg)
        #print(words[u'姓名']['words'])
        #print(words[u'公民身份号码']['words'])
        
        #以上内容 建议使用 姓名\t身份证号码方式显示


def bianli(rootDir):
    imgType_list={'jpg','bmp','png','jpeg','jfif'}
    for root,dirs,files in os.walk(rootDir):
        for file in files:
            filepath=os.path.join(root,file)
            #print(imghdr.what(file))
            #print(filetype.guess(filepath))                   
            try:
                #print(filetype.guess(filepath).mime)
                #f(imghdr.what(filepath) in imgType_list):
                f=filepath.rfind(('.'))
                if filepath[f+1:] in imgType_list:
                    duqusfzxx(filepath)                
            except:
                print(filepath+"\t 出错!")



#直接调用,修改一下文件夹即可,也可以写成参数格式,懒得修改了
bianli(r'D:\05.身份验证')


sfz照片正面图片(如图1)
在相应文件夹中运行代码,可以获取姓名、性别、家庭住址、身份证号码等信息(如图2)
图片1.jpg 图片2.jpg

免费评分

参与人数 3吾爱币 +2 热心值 +2 收起 理由
快乐的小驹 + 1 + 1 谢谢@Thanks!
xieyi2003 + 1 用心讨论,共获提升!
xbb + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

快乐的小驹 发表于 2024-1-19 13:25
大神,为啥我想把结果批量写入到表格,每行只写入一个姓。
[Python] 纯文本查看 复制代码
from aip import AipOcr
import os
from openpyxl import Workbook

#百度云账号,自己填写上自己的百度云开发帐号信息
APP_ID='999999999'
API_KEY='999999999'
SECRET_KEY='999999999'
#调用百度云接口
client=AipOcr(APP_ID,API_KEY,SECRET_KEY)
def get_file_content(filePath):
    with open(filePath,'rb') as fp:
            return fp.read()
 
def duqusfzxx(imagefile):
    image=get_file_content(imagefile)
    #识别身份证正面
    idCardSide="front"
    #识别身份证背面
    #idCardSide="back"
    client.idcard(image,idCardSide)
    options={}
    options["detect_direction"]="true" #是否检测图像朝向,默认不检测
    options["detect_risk"]="false"  #是否开启身份证风险类型(身份证复印件)
    #带参数调用身份证识别
    result=client.idcard(image,idCardSide,options)
    
    id_msg = []
    if isinstance(result,dict):
        words=result['words_result']
        id_msg=words[u'姓名']['words']+'\t'+words[u'性别']['words']+'\t'+words[u'住址']['words']+'\t'+words[u'公民身份号码']['words']+'\t'+words[u'民族']['words']+'\t'+words[u'出生']['words']
         
        print(id_msg)

    return id_msg
    
def bianli(rootDir):
    wb = Workbook()
    ws = wb.active
    ws.append(['姓名', '性别', '公民身份号码', '住址', '民族', '出生日期'])

    imgType_list = {'jpg', 'bmp', 'png', 'jpeg', 'jfif'}
    for root, dirs, files in os.walk(rootDir):
        for file in files:
            filepath = os.path.join(root, file)
            try:
                f = filepath.rfind(('.'))
                if filepath[f + 1:] in imgType_list:
                    duqusfzxx(filepath)
                    id_msg = duqusfzxx(filepath)
                    if id_msg:
                        ws.append(id_msg[0].split('\t'))
            except Exception as e:
                print(e)
    wb.save("C:\\Users\\Administrator\\Desktop\\123\\识别结果.xlsx")
 
 
#直接调用,修改一下文件夹即可,也可以写成参数格式,懒得修改了
bianli(r'C:\Users\Administrator\Desktop\新建文件夹 (2)')
yk156511 发表于 2023-3-23 10:02
IMG_TYPES = ('jpg', 'bmp', 'png', 'jpeg', 'jfif')
ID_CARD_SIDE = 'front'
OCR_OPTIONS = {'detect_direction': 'true', 'detect_risk': 'false'}

def duqusfzxx(imagefile):
    with open(imagefile, 'rb') as f:
        image = f.read()
    client = get_aip_ocr()
    result = client.idcard(image, ID_CARD_SIDE, OCR_OPTIONS)
    #...

def bianli(rootDir):
    for root, dirs, files in os.walk(rootDir):
        for file in files:
            filepath = os.path.join(root, file)
            if filepath.endswith(IMG_TYPES):
                try:
                  
 楼主| oenrjtt 发表于 2023-3-22 08:26
本人纯小白,代码写的挺垃圾的,各位大神见笑了
binstc 发表于 2023-3-22 10:54
前排学习
89684828 发表于 2023-3-22 11:02
标记一下,谢谢楼主!
jjoobb123 发表于 2023-3-22 11:04
谢谢分享,多多学习。
摇滚的鱼学破解 发表于 2023-3-22 11:25
谢谢分享,学习了
starcloud105 发表于 2023-3-22 11:41
666,这个我之前也学过,也是使用百度云的接口调用
xiliangjingmach 发表于 2023-3-22 12:11
牛的牛的
bossangel 发表于 2023-3-22 12:58
谢谢分享,试试看
xixicoco 发表于 2023-3-22 14:18
ocr识别啊,我还以为是获取账号信息呢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-8 00:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表