网站首页 > 技术文章 正文
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
- 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 服务的交互权限。
三、定义函数
- 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')
- 首先构建请求 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 格式的响应数据。
- 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,不显示图像。
四、创建图形用户界面
- 创建主窗口:
root = tk.Tk()
- 创建一个Tk对象,代表应用的主窗口。
- 创建上传按钮:
upload_button = tk.Button(root, text="请上传照片", command=on_upload)
upload_button.pack()
- 创建一个按钮,文本为 “请上传身份证照片”,当按钮被点击时,会调用on_upload函数。
- 使用pack方法将按钮放置在主窗口中。
- 创建用于显示照片的标签:
image_label = tk.Label(root)
image_label.pack(side=tk.LEFT)
- 创建一个标签,用于显示上传的照片。
- 使用pack方法将标签放置在主窗口中,并设置在左侧。
- 创建文本框用于显示识别结果:
text_box = tk.Text(root)
text_box.pack(side=tk.RIGHT)
- 创建一个文本框,用于显示身份证识别结果。
- 使用pack方法将文本框放置在主窗口中,并设置在右侧。
- 进入主循环:
root.mainloop()
- 启动tkinter的主事件循环,使应用程序开始接收用户输入并响应事件。
猜你喜欢
- 2024-12-03 137.Python——PySide6:QInputDialog输入对话框的创建与使用
- 2024-12-03 云中忆低代码之【开关】组件
- 2024-12-03 微信8.0.28正式更新!发现6个实用功能,朋友圈新增访问权限
- 2024-12-03 Word文档如何不显示回车符?让你的文档更美观
- 2024-12-03 「Excel技巧」恼人的数据透视表之怎么去掉(空白)
- 2024-12-03 随手写个接口测试工具,让领导瞧瞧我的能耐
- 2024-12-03 topjui easyui:combobox(下拉列表应用)
- 2024-12-03 学会这两招,PS换行不求人!轻松实现文本排版,小白也能秒变高手
- 2024-12-03 如何在Excel中使用斜线,并在表格的标题中使用单斜线或双斜线
- 2024-12-03 哇塞!这款PPT插件简直太OK了!PPT新手狂喜!网友:就服气它
- 1509℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 532℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 495℃MySQL service启动脚本浅析(r12笔记第59天)
- 474℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 472℃启用MySQL查询缓存(mysql8.0查询缓存)
- 452℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 431℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 428℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)