ITPub博客

首页 > IT职业 > IT生活 > python调用百度AI对颜值评分

python调用百度AI对颜值评分

IT生活 作者:专注的阿熊 时间:2021-11-29 17:01:36 0 删除 编辑

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']['face_list'][0]['gender']['type']

options["face_field"] = "age"

time.sleep(1)

result = aipFace.detect(get_picture_content(picturePath), imageType, options)

age = result['result']['face_list'][0]['age']

options["face_field"] = "beauty"

time.sleep(2)

result = aipFace.detect(get_picture_content(picturePath), imageType, options)

beauty = result['result']['face_list'][0]['beauty']

# 获取年龄、性别、颜值信息

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

if face_character['beauty'][0]>80:

     if face_character['gender'][0] == 'female':

         str_list = " 小姐姐芳龄:外汇跟单gendan5.com "+ str(age) + " ,颜值爆表 "+ ' ,最终颜值得分为: '+str(beauty)

     else:

         str_list = " 小哥哥贵庚: "+ str(age) + " ,颜值爆表 "+ ' ,最终颜值得分为: '+str(beauty)

elif face_character['beauty'][0]>70:

     if face_character['gender'][0] == 'female':

         str_list = " 小姐姐芳龄: "+ str(age) + " ,天生美颜 "+ ' ,最终颜值得分为: '+str(beauty)

     else:

         str_list = " 小哥哥贵庚: "+ str(age) + " ,天生美颜 "+ ' ,最终颜值得分为: '+str(beauty)

elif face_character['beauty'][0]>50:

     if face_character['gender'][0] == 'female':

         str_list = " 小姐姐芳龄: "+ str(age) + " ,颜值尚可 "+ ' ,最终颜值得分为: '+str(beauty)

     else:

         str_list = " 小哥哥贵庚: "+ str(age) + " ,颜值尚可 "+ ' ,最终颜值得分为: '+str(beauty)

elif face_character['beauty'][0]>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()


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69946337/viewspace-2844694/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    268
  • 访问量
    145930