""" 》》》运行前必做《《《 1. 运行【安装第三方库.py】文件 2. 注册百度云账号,获取API,配置后查看完整效果 账号注册指南: https://huewq7h021.feishu.cn/wiki/Ry3UwaoceiMRXgklbWtc9mEUn9f?from=from_copylink """ ''' 【一、导入库】 ''' # 预置内容,请勿改动 import io import base64 import cv2 # 请在下方编写代码 import tkinter as tk from PIL import Image, ImageTk from tkinter import filedialog, messagebox from aip import AipFace ''' 【二、初始化】(变量初始化、窗口初始化、API配置信息) ''' # 预置内容,请勿改动 cap = None imgtk = None update_id = None # 请在下方编写代码 # 初始化Tkinter root = tk.Tk() root.geometry('700x700') root.title('颜值测评') root.resizable(0, 0) # 百度AI的API配置信息 APP_ID = '你的 AppID' API_KEY = '你的 API Key' SECRET_KEY = '你的 Secret Key' client = AipFace(APP_ID, API_KEY, SECRET_KEY) ''' 【三、函数】 ''' # 预置内容,请勿改动 图像 --> 字节数据 --> Base64 编码格式 def img_to_bytes(photoimg): # 将 PhotoImage 转换为 PIL 图像 pil_img = ImageTk.getimage(photoimg) # 如果图像具有 Alpha 信道(透明度),则转换为 RGB if pil_img.mode == 'RGBA': pil_img = pil_img.convert('RGB') # 保存到字节缓冲区 buffer = io.BytesIO() pil_img.save(buffer, format='JPEG') img_data = buffer.getvalue() return base64.b64encode(img_data).decode('utf-8') # 预置内容,请勿改动 打开摄像头 def open_camera(): global cap, update_id cap = cv2.VideoCapture(0) if not cap.isOpened(): print("无法打开摄像头") return def update_frame(): global cap, update_id ret, frame = cap.read() if frame is None: return frame = cv2.resize(frame, (250, 250)) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img = Image.fromarray(frame) imgtk = ImageTk.PhotoImage(img) lab_photo.imgtk = imgtk lab_photo.config(image=imgtk) update_id = lab_photo.after(10, update_frame) update_frame() # 预置内容,请勿改动 关闭摄像头 def close_camera(): global cap, update_id if cap is not None: cap.release() cap = None if update_id is not None: lab_photo.after_cancel(update_id) update_id = None lab_photo.imgtk = imgtk lab_photo.config(image=imgtk) # 请在下方编写代码 # 加载本地图片 def load(): file_path = filedialog.askopenfilename() # 打开一个文件选择对话框 if file_path: close_camera() img = Image.open(file_path) img = img.resize((250, 250)) imgtk = ImageTk.PhotoImage(img) lab_photo.imgtk = imgtk lab_photo.config(image=imgtk) # 颜值检测开始 def ok(): # 判断 lab_photo 对象是否具有属性 imgtk if not hasattr(lab_photo, 'imgtk'): messagebox.showerror("错误", "没有显示图片,无法进行颜值测试") return img_base64 = img_to_bytes(lab_photo.imgtk) # 将图片转为Base64 imageType = "BASE64" # 设置图片类型 options = {"face_field":"age,gender,face_shape,emotion,beauty"} result = client.detect(img_base64, imageType, options) if 'error_msg' in result and result['error_msg'] == 'pic not has face': messagebox.showerror("错误", "图像中未检测到人脸") return if 'result' in result and 'face_list' in result['result']: # 解析结果 age = result['result']['face_list'][0]['age'] gender = result['result']['face_list'][0]['gender']['type'] face_shape = result['result']['face_list'][0]['face_shape']['type'] emotion = result['result']['face_list'][0]['emotion']['type'] beauty = result['result']['face_list'][0]['beauty'] # 构造结果字符串 output_str=f"年龄:{age}\n性别:{gender}\n脸型:{face_shape}\n情绪:{emotion}\n颜值:{beauty}" # 使用对话框显示结果 messagebox.showinfo("颜值测评结果", output_str) else: messagebox.showerror("错误", "检测失败,请重试") ''' 【四、组件配置】 ''' # 设置背景图 bg_image = tk.PhotoImage(file = '测颜值.png') bg_label = tk.Label(root, image = bg_image) bg_label.place(x=0, y=0, relwidth=1, relheight=1) # 设置缩略图 pic0 = Image.open('缩略图.png') pic0 = pic0.resize((250, 250)) pic0_image = ImageTk.PhotoImage(pic0) lab_photo = tk.Label(root, image=pic0_image) lab_photo.place(x=223, y=223) # 本地上传按钮 btn_load = tk.Button(root, text="本地上传", command=load, width=15, height=2, font=("楷体", 12)) btn_load.place(x=115, y=625) # 拍照上传按钮 btn_cam = tk.Button(root, text="拍照上传", command=open_camera, width=15, height=2, font=("楷体", 12)) btn_cam.place(x=300, y=625) # 开始测评按钮 btn_enter = tk.Button(root, text="点击开测!", command=ok, width=15, height=2, font=("楷体", 12)) btn_enter.place(x=485, y=625) ''' 【五、启动事件循环】 ''' root.mainloop()