# 来写一个窗口
import PIL
import tkinter as ek
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk
import base64
from aip import AipFace
# 配置百度aip的参数
# 配置百度aip参数
APP_ID = '15768642'
API_KEY = 'xhiiGmGPRCRj10XIqVlVeCky'
SECRET_KEY = 'ZDMMAO7StwTKzW8BspVQxvoGtdgSW4yI'
a_face = AipFace(APP_ID, API_KEY, SECRET_KEY)
image_type = 'BASE64' # 图像的文件类型
options = {'face_field':'age,gender,beauty'}
def get_file_content(file_path):
with open(file_path,"rb") as f:
content = base64.b64encode(f.read())
return content.decode("utf-8")
def face_score(file_path):
result = a_face.detect(get_file_content(file_path),image_type,options)
print(result)
age = result['result']["face_list"][0]["age"]
beauty = result['result']["face_list"][0]["beauty"]
gender = result['result']["face_list"][0]["gender"]['type']
return age,beauty,gender
class da_fen():
mywindow = ek.Tk()
mywindow.geometry("888x666")
mywindow.title("你钉起来真好听")
# 修改背景色
canvas = ek.Canvas(mywindow, width=888,height=666,bg="#9400D3")
canvas.pack()
def start(self):
# 按钮: 打开文件
ek.Button(self.mywindow,text="打开文件",command=self.show_picture).place(x=50,y=150)
# 按钮: 颜值识别
ek.Button(self.mywindow,text="颜值识别",command=self.open_files).place(x=50,y=250)
# 按钮: 退出软件
ek.Button(self.mywindow,text="退出软件",command=self.quit).place(x=50,y=350)
# 原图 标签:Label
ek.Label(self.mywindow,text="原图").place(x=420,y=50)
ek.Label(self.mywindow,text="性别").place(x=680,y=150)
ek.Label(self.mywindow,text="年龄").place(x=680,y=250)
ek.Label(self.mywindow,text="颜值").place(x=680,y=350)
# 显示图框
self.label_img = ek.Label(self.mywindow)
# 显示图框背景
self.cv_img = ek.Canvas(self.mywindow, bg='white',width=270,height=270)
# 显示图框边框
self.cv_img.create_rectangle(8,8,260,260,width=1,outline="red")
# 设置位置
self.cv_img.place(x=265,y=150)
self.label_img.place(x=265,y=150)
self.text1 = ek.Text(self.mywindow, font=15,width=10,height=2)
self.text1.place(x=680,y=175)
self.text2 = ek.Text(self.mywindow, font=15,width=10,height=2)
self.text2.place(x=680,y=275)
self.text3 = ek.Text(self.mywindow, font=15, width=10, height=2)
self.text3.place(x=680, y=375)
self.mywindow.mainloop()
def show_picture(self):
# 1.选择文件
self.path = askopenfilename(title="选择文件")
# 2.处理文件
img = Image.open(fr'{self.path}')
img = img.resize((270,270),PIL.Image.ANTIALIAS)
# 生成一张图像
img_png = ImageTk.PhotoImage(img)
# 3,。设置图像
self.label_img.config(image=img_png)
self.label_img.image=img_png
self.cv_img.create_image(5,5,anchor="nw",image=img_png) # 船锚
def open_files(self):
age,score,gender = face_score(self.path)
self.text1.delete(1.0,ek.END)
self.text1.tag_config("red",foreground="RED")
self.text1.insert(ek.END,gender,"red")
self.text2.delete(1.0, ek.END)
self.text2.tag_config("red", foreground="RED")
self.text2.insert(ek.END, age, "red")
self.text3.delete(1.0, ek.END)
self.text3.tag_config("red", foreground="RED")
self.text3.insert(ek.END, score, "red")
def quit(self):
self.mywindow.quit()
# 通过类新建一个对象
xi_tong = da_fen()
xi_tong.start()
qiyuan -颜值打分系统
猜你喜欢
转载自blog.csdn.net/houlaos/article/details/105057297
今日推荐
周排行