您好,登錄后才能下訂單哦!
本篇內容主要講解“Python怎么調用百度AI實現顏值評分功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Python怎么調用百度AI實現顏值評分功能”吧!
安裝好baidu-aip模塊,獲取了百度AI接口密鑰后,即可調用百度接口進行人臉屬性識別了。首先以楊紫的圖片為例進行年齡、性別、顏值的識別。
具體python代碼如下:?
import os import base64 from aip import AipFace os.chdir(r'F:\公眾號\28.人臉識別') #設置圖片存放的路徑 pictureName = '1_yz.jpg' def get_picture_content(pictureName): with open(pictureName, 'rb') as fp: content = base64.b64encode(fp.read()) return content.decode('utf-8') #定義讀取圖片的函數 APP_ID = 'XXX' API_KEY = 'XXXXXXXX' SECRET_KEY = 'XXXXXXXXXXXX' #百度賬號和密鑰 options = {} imageType = 'BASE64' options["face_field"] = "age, gender, beauty" aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY) result = aipFace.detect(get_picture_content(pictureName), imageType, options) print(result)
注: 其中os.chdir中的內容應該替換成你存儲圖片的地址,APP_ID、API_KEY、SECRET_KEY應該替換成你獲取的百度密鑰。
得到結果如下:
其中,age對應的值為年齡,gender對應的值為性別,beauty對應的值為顏值分。為進一步規范輸出成標準格式,通過如下代碼進行規范化:
import pandas as pd face_character = pd.DataFrame({"age":[result['result']['face_list'][0]['age']], "gender":[result['result']['face_list'][0]['gender']['type']], "beauty":[result['result']['face_list'][0]['beauty']] })
得到結果如下:
可以發現楊紫的顏值得分還是很高的圖片,后續文章也會對其它一些明星進行測試。
根據性別和顏值分,對識別結果進行評價,具體代碼如下:
if face_character['beauty'][0]>80: if face_character['gender'][0] == 'female': print("小姐姐顏值爆表") else: print("小哥哥顏值爆表") elif face_character['beauty'][0]>70: if face_character['gender'][0] == 'female': print("小姐姐天生美顏") else: print("小哥哥天生美顏") elif face_character['beauty'][0]>60: if face_character['gender'][0] == 'female': print("小姐姐顏值尚可") else: print("小哥哥顏值尚可") elif face_character['beauty'][0]>40: if face_character['gender'][0] == 'female': print("小姐姐先天不夠,后天來湊") else: print("小哥哥先天不夠,后天來湊") else: if face_character['gender'][0] == 'female': print("小姐姐洗洗睡吧") else: print("小哥哥洗洗睡吧")
得到結果:
假設文件夾中有數張人臉圖片,想批量對這批圖片進行顏值打分,可執行如下代碼:
import os import re import time import base64 from aip import AipFace os.chdir(r'F:\公眾號\28.人臉識別') #設置圖片存放的路徑 pictureName = '6_brzp.jpg' def get_picture_content(pictureName): with open(pictureName, 'rb') as fp: content = base64.b64encode(fp.read()) return content.decode() #定義讀取圖片的函數 APP_ID = 'XXX' API_KEY = 'XXXXXXXX' SECRET_KEY = 'XXXXXXXXXXXX' #百度賬號和密鑰 options = {} imageType = 'BASE64' options["face_field"] = "beauty" options["max_face_num"] = 2 aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY) root_path = r'F:\公眾號\28.人臉識別' list = os.listdir(root_path) # 列出文件夾下所有的目錄與文件 for i in range(0, len(list)): time.sleep(1) path = os.path.join(root_path, list[i]) if os.path.isfile(path): result = aipFace.detect(get_picture_content(path), imageType, options) print(list[i] + ' 評分為:' + str(result['result']['face_list'][0]['beauty'])) #顯示出所有圖片的顏值得分
得到結果:
可以發現楊紫的那張照片顏值分很高,有八十幾分。為了更好的用戶體驗,我們設置一個窗口,顯示評價照片并語音播報顏值得分。?
為了更清晰地評價人的顏值,把圖片和結果用tkinter庫和語音播放庫包裝起來,效果會更好。具體代碼如下:
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 = "小姐姐芳齡:"+ 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()
到此,相信大家對“Python怎么調用百度AI實現顏值評分功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。