python调用百度AI对颜值评分

import os
import re
import time
import base64
import pandas as pd
import tkinter as tk
from aip import AipFace
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
Folderpath = filedialog.askdirectory(title = '请选择图片存放的文件夹')
picturePath = filedialog.askopenfilename(title = '请选择要进行颜值评价的图片')
root.destroy()
os.chdir(Folderpath)

设置图片存放的路径

def get_picture_content(picturePath):

with open(picturePath, 'rb') as fp:
    content = base64.b64encode(fp.read())
    return content.decode()

定义读取图片的函数

APP_ID = 'XXX'
API_KEY = 'XXXXXXXX'
SECRET_KEY = 'XXXXXXXXXXXX'

百度账号和密钥

options = {}
options["max_face_num"] = 2
options["face_field"] = "gender"
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)
imageType = 'BASE64'
result = aipFace.detect(get_picture_content(picturePath), imageType, options)
gender = result'result'0['type']
options["face_field"] = "age"
time.sleep(1)
result = aipFace.detect(get_picture_content(picturePath), imageType, options)
age = result'result'0
options["face_field"] = "beauty"
time.sleep(2)
result = aipFace.detect(get_picture_content(picturePath), imageType, options)
beauty = result'result'0

获取年龄、农产品期货性别、颜值信息

face_character = pd.DataFrame({"age":age, "gender":gender, "beauty":beauty},index = ['value'])
if face_character'beauty'>80:

if face_character['gender'][0] == 'female':
    str_list = "小姐姐芳龄:"+ str(age) + ",颜值爆表"+ ',最终颜值得分为:'+str(beauty)
else:
    str_list = "小哥哥贵庚:"+ str(age) + ",颜值爆表"+ ',最终颜值得分为:'+str(beauty)

elif face_character'beauty'>70:

if face_character['gender'][0] == 'female':
    str_list = "小姐姐芳龄:"+ str(age) + ",天生美颜"+ ',最终颜值得分为:'+str(beauty)
else:
    str_list = "小哥哥贵庚:"+ str(age) + ",天生美颜"+ ',最终颜值得分为:'+str(beauty)

elif face_character'beauty'>50:

if face_character['gender'][0] == 'female':
    str_list = "小姐姐芳龄:"+ str(age) + ",颜值尚可"+ ',最终颜值得分为:'+str(beauty)
else:
    str_list = "小哥哥贵庚:"+ str(age) + ",颜值尚可"+ ',最终颜值得分为:'+str(beauty)

elif face_character'beauty'>30:

if face_character['gender'][0] == 'female':
    str_list = "小姐姐芳龄:"+ str(age) + ",先天不够,后天来凑"+ ',最终颜值得分为:'+str(beauty)
else:
    str_list = "小哥哥贵庚:"+ str(age) + ",先天不够,后天来凑" + ',最终颜值得分为:'+str(beauty)

else:

if face_character['gender'][0] == 'female':
    str_list = "小姐姐芳龄:"+ str(age) + ",早点洗洗睡吧"+ ',最终颜值得分为:'+str(beauty)
else:
    str_list = "小哥哥贵庚:"+ str(age) + ",早点洗洗睡吧"+ ',最终颜值得分为:'+str(beauty)

颜值定义

from tkinter import *
from PIL import Image, ImageTk
from win32com.client import Dispatch

导入包

speaker = Dispatch("SAPI.SpVoice")
def roll_call():

speaker.Speak(str_list)

控制播放语音

os.chdir(Folderpath)

设置文件路径

root = Tk()
root.title("颜值测试小程序")
root.iconbitmap("pikaqiu2.ico")

设置窗口

image = Image.open(picturePath)

加载图片

root.geometry("400x300")

根据图片大小设置窗口大小

img_pic = ImageTk.PhotoImage(image)
label = Label(root, image=img_pic)
label.pack()
b2 = tk.Button(root, bg='lightyellow', text='颜值评价', font=("KaiTi", 8), width=8, height=2, command=roll_call)
b2.place(x=0, y=0)
root.mainloop()

你可能感兴趣的:(python)