來個好玩的東西,百度開源的人臉識別接口,通過上傳人像圖片可以返回顏值打分,年齡等信息;
項目實現Python調用,已調試通過;
在運行代碼之前,你需要先在Baidu開發者平台申請權限,步驟如下:
登錄百度雲「https://cloud.baidu.com/?from=console」,沒有Baidu賬號的注冊一個;
通過界面右上角進入控制台;
找到人臉識別,進入並創建應用,創建時默認全選即可;
import requests
import base64
from IPython.display import Image
access_token
,用於後期人臉檢測的接口請求,token
的有效期為30天,不必頻繁請求;def access_token(ak, sk):
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(ak, sk)
response = requests.get(host)
if response.status_code == 200:
token = response.json()['access_token']
return token
else:
assert '獲取Token失敗...'
上傳的圖表需要先進行Base64編碼,代碼如下:
def encode(img_path):
with open(img_path, 'rb') as f:
data = base64.b64encode(f.read())
return data
需要兩個參數,data是Base64編碼後的圖片文件,token即為前面提到的access_token
;
請求成功的話會返回一個json,其中包括顏值打分,年齡預測,人臉錨點等;
def appearance(data, token):
url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token={}'.format(token)
headers = {
'content-type': 'application/json'}
params = {
"image": data,
"image_type": "BASE64",
"face_field": "age,beauty,expression,face_shape,gender,glasses,"
"quality,eye_status,emotion,face_type,mask,spoofing"}
res = requests.post(url, headers=headers, data=params)
if res.status_code == 200:
return res.json()
將下方cell中的API Key和Secret Key替換為自己在百度開發者後台申請的,完成認證;
token = access_token('API Key', 'Secret Key')
看看這算法有沒有對自己的老大有偏心~
有一說一,顏值這塊,Robin在一眾企業家中真難逢對手。
page_path = '/home/kesci/work/appearance/robin.jpeg'
data = encode(page_path)
result = appearance(data, token)['result']['face_list'][0]
msg = '年齡:{}\n性別:{}\n顏值:{}\n臉型:{}\n是否戴眼鏡:{}\n是否戴口罩:{}\n情緒:{}\n'
print(
msg.format(
result['age'],
result['gender']['type'],
result['beauty'],
result['face_shape']['type'],
result['glasses']['type'],
result['mask']['type'],
result['emotion']['type'])
)
Image(page_path)
年齡:29
性別:male
顏值:66.49
臉型:oval
是否戴眼鏡:none
是否戴口罩:0
情緒:happy
三角形臉型??
page_path = '/home/kesci/work/appearance/jack.jpeg'
data = encode(page_path)
result = appearance(data, token)['result']['face_list'][0]
msg = '年齡:{}\n性別:{}\n顏值:{}\n臉型:{}\n是否戴眼鏡:{}\n是否戴口罩:{}\n情緒:{}\n'
print(
msg.format(
result['age'],
result['gender']['type'],
result['beauty'],
result['face_shape']['type'],
result['glasses']['type'],
result['mask']['type'],
result['emotion']['type'])
)
Image(page_path)
年齡:37
性別:male
顏值:38.23
臉型:triangle
是否戴眼鏡:none
是否戴口罩:0
情緒:happy
除了顏值外還能識別是否戴眼鏡~
page_path = '/home/kesci/work/appearance/pony.jpeg'
data = encode(page_path)
result = appearance(data, token)['result']['face_list'][0]
msg = '年齡:{}\n性別:{}\n顏值:{}\n臉型:{}\n是否戴眼鏡:{}\n是否戴口罩:{}\n情緒:{}\n'
print(
msg.format(
result['age'],
result['gender']['type'],
result['beauty'],
result['face_shape']['type'],
result['glasses']['type'],
result['mask']['type'],
result['emotion']['type'])
)
Image(page_path)
年齡:33
性別:male
顏值:49.65
臉型:square
是否戴眼鏡:common
是否戴口罩:0
情緒:neutral
除了普通眼鏡還能判斷是不是太陽鏡️
page_path = '/home/kesci/work/Robert.jpeg'
data = encode(page_path)
result = appearance(data, token)['result']['face_list'][0]
msg = '年齡:{}\n性別:{}\n顏值:{}\n臉型:{}\n是否戴眼鏡:{}\n是否戴口罩:{}\n情緒:{}\n'
print(
msg.format(
result['age'],
result['gender']['type'],
result['beauty'],
result['face_shape']['type'],
result['glasses']['type'],
result['mask']['type'],
result['emotion']['type'])
)
Image(page_path)
年齡:50
性別:male
顏值:53.94
臉型:square
是否戴眼鏡:sun
是否戴口罩:0
情緒:happy
拉高一下本項目的顏值平均分~
page_path = '/home/kesci/work/meizhi.jpeg'
data = encode(page_path)
result = appearance(data, token)['result']['face_list'][0]
msg = '年齡:{}\n性別:{}\n顏值:{}\n臉型:{}\n是否戴眼鏡:{}\n是否戴口罩:{}\n情緒:{}\n'
print(
msg.format(
result['age'],
result['gender']['type'],
result['beauty'],
result['face_shape']['type'],
result['glasses']['type'],
result['mask']['type'],
result['emotion']['type'])
)
Image(page_path)
年齡:24
性別:female
顏值:77.24
臉型:square
是否戴眼鏡:none
是否戴口罩:0
情緒:neutral
識別是否戴口罩
page_path = '/home/kesci/work/mask.jpeg'
data = encode(page_path)
result = appearance(data, token)['result']['face_list'][0]
msg = '年齡:{}\n性別:{}\n顏值:{}\n臉型:{}\n是否戴眼鏡:{}\n是否戴口罩:{}\n情緒:{}\n'
print(
msg.format(
result['age'],
result['gender']['type'],
result['beauty'],
result['face_shape']['type'],
result['glasses']['type'],
result['mask']['type'],
result['emotion']['type'])
)
Image(page_path)
年齡:23
性別:female
顏值:70.81
臉型:heart
是否戴眼鏡:none
是否戴口罩:1
情緒:happy