文章目錄
- 前言
- 一、具體程序
- 1.百度AI平臺申請密鑰
- 2.python代碼實作
- 3.結果
- 二、悄悄看看明星的顏值分
- 彭于晏
- 王力宏
- Lisa
前言
最近網路上爆火的藏族小哥哥丁真,大家都知道嗎?

十幾天前憑借一張純真、干凈、帥氣的臉霸屏各大短視頻平臺,連各大電視臺新聞媒體都爭相報道,這個藏族小伙瞬間火了!!
網友們對于丁真的長相評價不一,我個人感覺是很帥的,
今天我使用百度AI的人臉識別,來看看人工智能會給丁真的顏值打多少分?
一、具體程序
1.百度AI平臺申請密鑰
進入百度AI開放平臺https://ai.baidu.com/
進入控制臺并登錄賬號
進入后進入人臉識別,并創建應用

創建成功可以在我的應用看到密鑰

2.python代碼實作
首先要獲取Access Token,可以查看官方的檔案

然后將圖片轉化為base64編碼
def img_to_base64(slef, path):
#圖片轉化為base64
with open(path, 'rb') as f:
image = f.read()
image_base64 = str(base64.b64encode(image), encoding='utf-8')
return image_base64
查看檔案的主要請求引數

face_field引數,默認只回傳人臉框、概率和旋轉角度,如果需要回傳更多結果,可以在此引數中添加(beauty、age等),

python完整代碼:
# encoding:utf-8
import base64
import json
import requests
class BaiduAI:
def __init__(self, img):
self.AK = ""#你的應用API Key
self.SK = ""#你的應用Secret Key
self.img_src = img
self.headers = {
"Content-Type": "application/json; charset=UTF-8"
}
def get_AccessToken(self):
#獲取Access Token
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + self.AK + '&client_secret=' + self.SK
response = requests.get(host, headers=self.headers)
json_result = json.loads(response.text)
if response:
return json_result['access_token']
else:
print(json_result)
return 0
def img_to_base64(slef, path):
#圖片轉化為base64
with open(path, 'rb') as f:
image = f.read()
image_base64 = str(base64.b64encode(image), encoding='utf-8')
return image_base64
def face_identification(self):
# 人臉檢測與屬性分析
img = self.img_to_base64(self.img_src)
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
post_data = {
"image": img,
"image_type": "BASE64",
"face_field": "gender,age,beauty,gender,race,emotion,face_shape,landmark",#包括age,beauty,expression,face_shape,gender,glasses,landmark,emotion,face_type,mask,spoofing資訊
"face_type": "LIVE"#人臉的型別,LIVE表示生活照,IDCARD表示身份證芯片照,WATERMARK表示帶水印證件照,CERT表示證件照片,默認LIVE,
}
access_token = self.get_AccessToken()
request_url = request_url + "?access_token=" + access_token
response = requests.post(url=request_url, data=post_data, headers=self.headers)
json_result = json.loads(response.text)
print(json_result)
if json_result['error_code'] == 0:
print("人臉表情:", json_result['result']['face_list'][0]['emotion']['type'])
print("人物年齡:", json_result['result']['face_list'][0]['age'])
print("人物顏值評分:", json_result['result']['face_list'][0]['beauty'])
print("人物性別:", json_result['result']['face_list'][0]['gender']['type'])
print("人物種族:", json_result['result']['face_list'][0]['race']['type'])
#print("人物特征點位置:", json_result['result']['face_list'][0]['landmark72'])
else:
print(json_result['error_code'])
print(json_result['error_msg'])
if __name__ == '__main__':
imglist=['dingzhen1.jpg','dingzhen2.jpg']
for i in range(0,len(imglist)):
print('第{}張圖片:'.format(i+1))
demo = BaiduAI(imglist[i])
if(demo.get_AccessToken()):
demo.face_identification()
3.結果
下面是我找的兩張圖片:



從結果來看,丁真的顏值還是十分高的,
第二張圖片可能比較模糊,顏值分不太高哈哈哈
二、悄悄看看明星的顏值分
彭于晏


不愧是男神啊!!!!!
王力宏


找高清圖片真的太難了!!!
最后再找一張女神Lisa的照片看看顏值分多少吧,
Lisa


太可了!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/229187.html
標籤:python
