高性能网站建设指南 pdf,东莞网站开发公司,iis7 伪静态 wordpress,如何制作网络先看结果 必须遥遥领先 所需准备
这里我调用了#xff1a;
人脸比对 API 文档 | 讯飞开放平台文档中心https://www.xfyun.cn/doc/face/xffaceComparisonRecg/API.html#%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
代码里所涉及的APPID、APISecret、APIKey 皆从讯飞的控制台获取
人脸比对 API 文档 | 讯飞开放平台文档中心https://www.xfyun.cn/doc/face/xffaceComparisonRecg/API.html#%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
代码里所涉及的APPID、APISecret、APIKey 皆从讯飞的控制台获取自己注册去讯飞开放平台-以语音交互为核心的人工智能开放平台申请即可。 话不多说上代码
代码实现
##APPID、APISecret、APIKey一定要填写##import tkinter as tk
from tkinter import filedialog
from tkinter import Label
from tkinter import ttk
from PIL import Image, ImageTk
import json
import requests
import base64
import hmac
import hashlib
from datetime import datetime
from time import mktime
from wsgiref.handlers import format_date_time
from urllib.parse import urlencodeclass AssembleHeaderException(Exception):def __init__(self, msg):self.message msgclass Url:def __init__(this, host, path, schema):this.host hostthis.path paththis.schema schemadef sha256base64(data):sha256 hashlib.sha256()sha256.update(data)digest base64.b64encode(sha256.digest()).decode(encodingutf-8)return digestdef parse_url(request_url):stidx request_url.index(://)host request_url[stidx 3:]schema request_url[:stidx 3]edidx host.index(/)if edidx 0:raise AssembleHeaderException(invalid request url: request_url)path host[edidx:]host host[:edidx]u Url(host, path, schema)return udef assemble_ws_auth_url(request_url, methodGET, api_key, api_secret):u parse_url(request_url)host u.hostpath u.pathnow datetime.now()date format_date_time(mktime(now.timetuple()))signature_origin host: {}\ndate: {}\n{} {} HTTP/1.1.format(host, date, method, path)signature_sha hmac.new(api_secret.encode(utf-8), signature_origin.encode(utf-8),digestmodhashlib.sha256).digest()signature_sha base64.b64encode(signature_sha).decode(encodingutf-8)authorization_origin api_key\%s\, algorithm\%s\, headers\%s\, signature\%s\ % (api_key, hmac-sha256, host date request-line, signature_sha)authorization base64.b64encode(authorization_origin.encode(utf-8)).decode(encodingutf-8)values {host: host,date: date,authorization: authorization}return request_url ? urlencode(values)def gen_body(appid, img1_path, img2_path, server_id):with open(img1_path, rb) as f:img1_data f.read()with open(img2_path, rb) as f:img2_data f.read()body {header: {app_id: appid,status: 3},parameter: {server_id: {service_kind: face_compare,face_compare_result: {encoding: utf8,compress: raw,format: json}}},payload: {input1: {encoding: jpg,status: 3,image: str(base64.b64encode(img1_data), utf-8)},input2: {encoding: jpg,status: 3,image: str(base64.b64encode(img2_data), utf-8)}}}return json.dumps(body)def run(appid, apikey, apisecret, img1_path, img2_path, server_ids67c9c78c):url http://api.xf-yun.com/v1/private/{}.format(server_id)request_url assemble_ws_auth_url(url, POST, apikey, apisecret)headers {content-type: application/json, host: api.xf-yun.com, app_id: appid}response requests.post(request_url, datagen_body(appid, img1_path, img2_path, server_id), headersheaders)resp_data json.loads(response.content.decode(utf-8))result base64.b64decode(resp_data[payload][face_compare_result][text]).decode()return resultdef browse_file(entry_widget, image_label, img_num):file_path filedialog.askopenfilename()if file_path:entry_widget.delete(0, tk.END)entry_widget.insert(0, file_path)load_and_display_image(file_path, image_label, img_num)def load_and_display_image(file_path, image_label, image_num):try:image Image.open(file_path)image image.resize((200, 200))photo ImageTk.PhotoImage(image)image_label.config(imagephoto)image_label.image photoif image_num 1:global img1_dataimg1_data imageelif image_num 2:global img2_dataimg2_data imageexcept Exception as e:result_label.config(text加载图片出错请检查文件格式)def compare_faces():if img1_data not in globals() or img2_data not in globals():result_label.config(text请选择两张图片进行比对)returntry:img1_path img1.jpgimg2_path img2.jpgimg1_data.save(img1_path)img2_data.save(img2_path)result run(appid,#自行申请填写apisecret,#自行申请填写apikey,#自行申请填写img1_pathimg1_path,img2_pathimg2_path)score float(json.loads(result)[score])if score 0.67:result_label.config(textf这两张图片是同一个人相似度{score:.2f})else:result_label.config(textf这两张图片不是同一个人相似度{score:.2f})except Exception as e:result_label.config(text比对出错请检查图片和配置)root tk.Tk()
root.title(人脸比对)# 设置样式主题
style ttk.Style()
style.configure(TButton, font(Helvetica, 12))
style.configure(TLabel, font(Helvetica, 14))frame ttk.LabelFrame(root, text选择图片)
frame.grid(row0, column0, columnspan2, padx10, pady10, stickyew)
entry1 tk.Entry(frame, width50)
entry2 tk.Entry(frame, width50)
entry1.grid(row0, column0, padx10, pady10)
entry2.grid(row1, column0, padx10, pady10)separator ttk.Separator(root, orienthorizontal)
separator.grid(row1, column0, columnspan2, stickyew)button1 ttk.Button(root, text选择图片1, commandlambda: browse_file(entry1, img_label1, 1))
button2 ttk.Button(root, text选择图片2, commandlambda: browse_file(entry2, img_label2, 2))
button1.grid(row2, column0, padx10, pady10)
button2.grid(row2, column1, padx10, pady10)compare_button ttk.Button(root, text比对图片, commandcompare_faces)
compare_button.grid(row3, column0, columnspan2, pady20)result_label Label(root, text, font(Helvetica, 14))
result_label.grid(row4, column0, columnspan2, padx10, pady10)img_label1 tk.Label(root)
img_label1.grid(row5, column0, padx10, pady10)
img_label2 tk.Label(root)
img_label2.grid(row5, column1, padx10, pady10)root.mainloop()为了美观并优化弹窗的布局使用了ttk.LabelFrame来创建一个带有标题的框架用于容纳选择图片的部件。还在界面中添加了一个水平分隔符ttk.Separator以提高界面的可读性。
官方代码里图片路径需要自行填写改进后则使得可以从本地选择需要对比图片。