本篇内容主要讲解“Python怎么调用百度AI实现颜值评分功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python怎么调用百度AI实现颜值评分功能”吧!
一、调用百度接口进行人脸属性识别
安装好baidu-aip模块,获取了百度AI接口密钥后,即可调用百度接口进行人脸属性识别了。首先以杨紫的图片为例进行年龄、性别、颜值的识别。
具体python代码如下:
import osimport base64from aip import AipFaceos.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 pdface_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 osimport reimport timeimport base64from aip import AipFaceos.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"] = 2aipFace = 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 osimport reimport timeimport base64import pandas as pdimport tkinter as tkfrom aip import AipFacefrom tkinter import filedialogroot = 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"] = 2options["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, ImageTkfrom 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实现颜值评分功能”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!