多线程的三种实现方式及静态代理模
648 2023-04-03 04:09:55
安装好baidu-aip模块,获取了百度AI接口密钥后,即可调用百度接口进行人脸属性识别了。首先以杨紫的图片为例进行年龄、性别、颜值的识别。
具体python代码如下:
?12345678910111213141516171819202122232425262728import
os
import
base64
from
aip
import
AipFace
os.chdir(r
'F:\公众号\28.人脸识别'
)
#设置图片存放的路径
pictureName
=
'1_yz.webp'
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对应的值为颜值分。为进一步规范输出成标准格式,通过如下代码进行规范化:
?123import
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'
]] })
得到结果如下:
可以发现杨紫的颜值得分还是很高的图片,后续文章也会对其它一些明星进行测试。
根据性别和颜值分,对识别结果进行评价,具体代码如下:
?12345678910111213141516171819202122232425if
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
(
"小哥哥洗洗睡吧"
)
得到结果:
假设文件夹中有数张人脸图片,想批量对这批图片进行颜值打分,可执行如下代码:
?12345678910111213141516171819202122232425262728293031323334353637383940import
os
import
re
import
time
import
base64
from
aip
import
AipFace
os.chdir(r
'F:\公众号\28.人脸识别'
)
#设置图片存放的路径
pictureName
=
'6_brzp.webp'
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库和语音播放库包装起来,效果会更好。具体代码如下:
?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import
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()
最后我们来看下明星的颜值评分吧。
1 刘亦菲
颜值评价结果:
'小姐姐芳龄:21,颜值尚可,最终颜值得分为:64.19'
对于这个结果,我也比较吃惊,所以颜值得分大家当成娱乐就好图片。
2 贾玲
颜值评价结果:
'小姐姐芳龄:37,先天不够,后天来凑,最终颜值得分为:30.67'
颜值得分仅供参考,请勿当真。我的有些照片测出来也是三十多分,可能跟背景、光线、表情都有一定的关系,大家当成娱乐就好。
3 肖战
颜值评价结果:
'小哥哥贵庚:23,颜值尚可,最终颜值得分为:63.9'
颜值得分仅供参考,请勿当真图片,认为我的代码有问题的可以自行下载图片和代码进行尝试。
4 宋小宝
颜值评价结果:
'小哥哥贵庚:40,先天不够,后天来凑,最终颜值得分为:45.18'
颜值得分仅供参考,请勿当真。从上面的颜值得分可以发现,普遍颜值得分较低,八十多分的已经算高分了,也算是为杨紫的颜值正名了吧。至此,调用百度接口颜值识别已讲解完毕,感兴趣的朋友自己实现一遍吧。
以上就是Python调用百度AI实现颜值评分功能的详细内容,更多关于Python 颜值评分的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章: