python呼叫百度AI對顏值評分

專注的阿熊發表於2021-11-29

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章