Python卡证识别脚本2.0版本,采用百度智能云OCR方案,速度和准确度都UP!UP!!

Python卡证识别脚本2.0版本

需要安装requests,pandas库,另外关于token的获取,可以自行到百度智能云官方查看如何获取哦,有视频讲解,很方便的。

import requests
import base64
import pandas as pd
import os

'''
身份证识别
'''

class baidu_auto_id_card_ocr():
    def __init__(self):
        self.request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
        self.access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
        self.headers = {'content-type': 'application/x-www-form-urlencoded'}
        self.file_path = r"D:/id_card"

    def read_img(self):
        list = []
        for img in os.listdir(self.file_path):
            f = open(f"{self.file_path}/{img}","rb")

            img = base64.b64encode(f.read())
            list.append(img)
        return list


    def get_response(self):
        global list
        list = []
        for image in self.read_img():
            params = {"id_card_side":"front","image":image}
            request_url = self.request_url + "?access_token=" + self.access_token
            response = requests.post(request_url, data=params, headers=self.headers)
            if response:
                name = response.json()["words_result"]["姓名"]["words"]
                nation = response.json()["words_result"]["民族"]["words"]
                address = response.json()["words_result"]["住址"]["words"]
                number = response.json()["words_result"]["公民身份号码"]["words"]
                birth = response.json()["words_result"]["出生"]["words"]
                list.append([name,nation,address,number,birth])
        print(list)
        return list

    def write_to_excel(self):
        df = pd.DataFrame(self.get_response(),columns=["姓名","民族","地址","身份证号","出生年月"])
        df.to_excel(r'D:/id_card/识别结果.xlsx',index=False)
        return df



if __name__ == '__main__':
    id_card = baidu_auto_id_card_ocr()
    id_card.read_img()
    id_card.write_to_excel()

以上,如有问题,可随时沟通哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值