优秀的编程知识分享平台

网站首页 > 技术文章 正文

轻松提取身份证信息,让办公效率飞起来(python文字识别)

nanyue 2024-12-03 17:07:32 技术文章 6 ℃



import tkinter as tk
from tkinter import filedialog
import requests
import base64
import json
from PIL import Image, ImageTk

# 百度 AI 应用的 API Key 和 Secret Key,这个得提前申请
api_key = '你的百度API Key '
secret_key = '你的百度Secret Key'

def get_access_token():
    url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={api_key}&client_secret={secret_key}"
    response = requests.post(url)
    return response.json().get('access_token')

def recognize_id_card(image_path):
    access_token = get_access_token()
    url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
    with open(image_path, 'rb') as f:
        image_data = f.read()
    base64_data = base64.b64encode(image_data).decode()
    payload = {
        "image": base64_data,
        "id_card_side": "front",
        "access_token": access_token
    }
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(url, data=payload, headers=headers)
    return response.json()

def on_upload():
    file_path = filedialog.askopenfilename()
    if file_path:
        result = recognize_id_card(file_path)
        if 'words_result' in result:
            text_box.delete('1.0', tk.END)  # 清空文本框
            for item in result['words_result']:
                text_box.insert(tk.END, item['words'] + '\n')
            # 显示上传的照片
            image = Image.open(file_path)
            photo = ImageTk.PhotoImage(image)
            image_label.config(image=photo)
            image_label.image = photo
        else:
            text_box.delete('1.0', tk.END)  # 清空文本框
            text_box.insert(tk.END, "识别失败,未获取到有效信息")
            image_label.config(image=None)

root = tk.Tk()

# 创建上传按钮
upload_button = tk.Button(root, text="请上传照片", command=on_upload)
upload_button.pack()

# 创建用于显示照片的标签
image_label = tk.Label(root)
image_label.pack(side=tk.LEFT)

# 创建文本框用于显示识别结果
text_box = tk.Text(root)
text_box.pack(side=tk.RIGHT)

root.mainloop()

以下是对这段代码的详细解释:

一、导入模块

import tkinter as tk
from tkinter import filedialog
import requests
import base64
import json
from PIL import Image, ImageTk
  • tkinter:用于创建图形用户界面(GUI)。通常将其导入为tk以便后续使用。
    • filedialog:来自tkinter模块,用于打开文件对话框,以便用户选择文件。
    • requests:用于发送 HTTP 请求,这里用于与百度 AI 的接口进行交互以获取访问令牌和进行身份证识别。
    • base64:用于对图像数据进行 Base64 编码,以便发送给百度 AI 的接口。
    • json:用于处理 JSON 格式的数据,例如解析百度 AI 返回的识别结果。
    • Image和ImageTk:来自PIL(Python Imaging Library 的分支 Pillow),用于处理图像。Image用于打开图像文件,ImageTk用于将PIL格式的图像转换为tkinter可以显示的格式。

    二、设置百度 AI 的 API Key 和 Secret Key

    api_key = '你的百度API Key '
    secret_key = '你的百度Secret Key'

    这里需要提前申请百度 AI 的 API Key 和 Secret Key,并将其替换为实际的值。这些密钥用于验证你的应用与百度 AI 服务的交互权限。

    三、定义函数

    1. get_access_token函数:
       def get_access_token():
           url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={api_key}&client_secret={secret_key}"
           response = requests.post(url)
           return response.json().get('access_token')
    
  • 这个函数的目的是获取百度 AI 的访问令牌。它通过向特定的 URL 发送 POST 请求,传递 API Key 和 Secret Key,以获取访问令牌。
    • 首先构建请求 URL,其中包含了固定的部分https://aip.baidubce.com/oauth/2.0/token以及查询参数grant_type(表示授权类型为客户端凭证模式)、client_id(API Key)和client_secret(Secret Key)。
    • 然后使用requests.post发送 POST 请求,获取响应对象。
    • 最后从响应的 JSON 数据中提取出access_token并返回。

    2.recognize_id_card函数:

       def recognize_id_card(image_path):
           access_token = get_access_token()
           url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
           with open(image_path, 'rb') as f:
               image_data = f.read()
           base64_data = base64.b64encode(image_data).decode()
           payload = {
               "image": base64_data,
               "id_card_side": "front",
               "access_token": access_token
           }
           headers = {'content-type': 'application/x-www-form-urlencoded'}
           response = requests.post(url, data=payload, headers=headers)
           return response.json()
    
  • 这个函数用于识别照片中的文字信息。它接受一个图像文件路径作为参数。
    • 首先调用get_access_token函数获取访问令牌。
    • 然后打开指定路径的图像文件,以二进制模式读取图像数据。
    • 将图像数据进行 Base64 编码,并将编码后的结果转换为字符串。
    • 构建请求参数payload,其中包含了 Base64 编码的图像数据、身份证的正面(id_card_side设置为front)以及访问令牌。
    • 设置请求头headers,指定内容类型为application/x-www-form-urlencoded。
    • 使用requests.post向百度 AI 的 OCR 接口发送 POST 请求,并返回 JSON 格式的响应数据。
    1. on_upload函数:
       def on_upload():
           file_path = filedialog.askopenfilename()
           if file_path:
               result = recognize_id_card(file_path)
               if 'words_result' in result:
                   text_box.delete('1.0', tk.END)  # 清空文本框
                   for item in result['words_result']:
                       text_box.insert(tk.END, item['words'] + '\n')
                   # 显示上传的照片
                   image = Image.open(file_path)
                   photo = ImageTk.PhotoImage(image)
                   image_label.config(image=photo)
                   image_label.image = photo
               else:
                   text_box.delete('1.0', tk.END)  # 清空文本框
                   text_box.insert(tk.END, "识别失败,未获取到有效信息")
                   image_label.config(image=None)
    
  • 这个函数在用户点击上传按钮时被调用。
    • 首先使用filedialog.askopenfilename打开文件对话框,让用户选择一个图像文件,并将选择的文件路径存储在file_path变量中。
    • 如果用户选择了文件(file_path不为空),调用recognize_id_card函数对身份证照片进行识别,得到识别结果result。
    • 如果识别结果中包含words_result(表示成功识别到文字信息),则先清空文本框,然后遍历words_result中的每个项目,将识别到的文字插入到文本框中,并换行显示。接着打开图像文件,使用Image.open和ImageTk.PhotoImage将图像转换为tkinter可以显示的格式,并设置到image_label标签中以显示图像。
    • 如果识别结果中没有words_result,则清空文本框并在文本框中显示 “识别失败,未获取到有效信息”,同时将image_label的图像设置为None,不显示图像。

    四、创建图形用户界面

    1. 创建主窗口:
       root = tk.Tk()


    • 创建一个Tk对象,代表应用的主窗口。
    1. 创建上传按钮:
       upload_button = tk.Button(root, text="请上传照片", command=on_upload)
       upload_button.pack()


    • 创建一个按钮,文本为 “请上传身份证照片”,当按钮被点击时,会调用on_upload函数。
    • 使用pack方法将按钮放置在主窗口中。
    1. 创建用于显示照片的标签:
       image_label = tk.Label(root)
       image_label.pack(side=tk.LEFT)
    


    • 创建一个标签,用于显示上传的照片。
    • 使用pack方法将标签放置在主窗口中,并设置在左侧。
    1. 创建文本框用于显示识别结果:
       text_box = tk.Text(root)
       text_box.pack(side=tk.RIGHT)


    • 创建一个文本框,用于显示身份证识别结果。
    • 使用pack方法将文本框放置在主窗口中,并设置在右侧。
    1. 进入主循环:
       root.mainloop()


    • 启动tkinter的主事件循环,使应用程序开始接收用户输入并响应事件。
    最近发表
    标签列表