import os
import pandas as pd
import requests
import base64
import tkinter as tk
from tkinter import filedialog
class IdCard_Distinguish:
def __init__(self):
self.func_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard'
self.access_token = '24.d379bcbcd7344f3e8a43db32245998ad.2592000.1624093312.282335-24211563'
self.request_url = self.func_url + "?access_token=" + self.access_token
self.headers = {'content-type': 'application/x-www-form-urlencoded'}
self.col = ['姓名','民族','住址','公民身份号码','出生','性别']
self.dirpath = self.user_select()
self.imgpath = self.get_imgname()
self.imgcode = self.img_decode()
self.info = self.img_distinguish()
def user_select(self):
tk.Tk().withdraw()
dirpath = filedialog.askdirectory()
return dirpath
def get_imgname(self):
lst_dir = []
dir_path = os.listdir(self.dirpath)
for fname in dir_path:
abs_path = os.path.join(self.dirpath, fname)
lst_dir.append(abs_path)
return lst_dir
def img_decode(self):
lst_img = []
for img in self.imgpath:
f = open(img, 'rb')
img_code = base64.b64encode(f.read())
lst_img.append(img_code)
return lst_img
def img_distinguish(self):
all_lst = []
for img in self.imgcode:
params = {"id_card_side":"front","image":img}
response = requests.post(self.request_url, data=params, headers=self.headers)
if response:
info_lst = []
for i in self.col:
info = response.json()['words_result'][i]['words']
info_lst.append(info)
all_lst.append(info_lst)
return all_lst
def save_as(self):
tk.Tk().withdraw()
file_path = filedialog.asksaveasfilename(title=u'输出文件')
df = pd.DataFrame(self.info,columns=self.col)
return df.to_excel(file_path,index=None)
if __name__ == '__main__':
obj = IdCard_Distinguish()
obj.save_as()
###将所需识别的身份证存于新建的同一文件夹下