百度AI——人脸识别案例
生活随笔
收集整理的這篇文章主要介紹了
百度AI——人脸识别案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
人臉分析
import requests, base64def get_access_token():url = 'https://aip.baidubce.com/oauth/2.0/token'data = {'grant_type': 'client_credentials', # 固定值'client_id': '', # 在開放平臺注冊后所建應用的API Key'client_secret': '' # 所建應用的Secret Key}res = requests.post(url, data=data)res = res.json()access_token = res['access_token']return access_tokendef get_json(img):access_token=get_access_token()request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"file = open(img, 'rb') # 二進制讀取圖片base64_data = base64.b64encode(file.read()) # 將圖片進行base64編碼base64_code = base64_data.decode()params = {'image': base64_code,'image_type': 'BASE64','face_field': 'age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,quality,eye_status,emotion,face_type,mask,spoofing'}request_url = request_url + "?access_token=" + access_tokenheaders = {'Content-Type': 'application/json'}response = requests.post(request_url, data=params, headers=headers)if response:return response.json()if __name__ == '__main__':img = './img/mjl.jpg'json_data=get_json(img)返回結果說明
| face_num | 是 | int | 檢測到的圖片中的人臉數量 |
| face_list | 是 | array | 人臉信息列表,具體包含的參數參考下面的列表。 |
| +face_token | 是 | string | 人臉圖片的唯一標識 (人臉檢測face_token有效期為60min) |
| +location | 是 | array | 人臉在圖片中的位置 |
| ++left | 是 | double | 人臉區域離左邊界的距離 |
| ++top | 是 | double | 人臉區域離上邊界的距離 |
| ++width | 是 | double | 人臉區域的寬度 |
| ++height | 是 | double | 人臉區域的高度 |
| ++rotation | 是 | int64 | 人臉框相對于豎直方向的順時針旋轉角,[-180,180] |
| +face_probability | 是 | double | 人臉置信度,范圍【0~1】,代表這是一張人臉的概率,0最小、1最大。其中返回0或1時,數據類型為Integer |
| +angle | 是 | array | 人臉旋轉角度參數 |
| ++yaw | 是 | double | 三維旋轉之左右旋轉角[-90(左), 90(右)] |
| ++pitch | 是 | double | 三維旋轉之俯仰角度[-90(上), 90(下)] |
| ++roll | 是 | double | 平面內旋轉角[-180(逆時針), 180(順時針)] |
| +age | 否 | double | 年齡 ,當face_field包含age時返回 |
| +beauty | 否 | int64 | 美丑打分,范圍0-100,越大表示越美。當face_fields包含beauty時返回 |
| +expression | 否 | array | 表情,當?face_field包含expression時返回 |
| ++type | 否 | string | none:不笑;smile:微笑;laugh:大笑 |
| ++probability | 否 | double | 表情置信度,范圍【0~1】,0最小、1最大。 |
| +face_shape | 否 | array | 臉型,當face_field包含face_shape時返回 |
| ++type | 否 | double | square: 正方形?triangle:三角形?oval: 橢圓?heart: 心形?round: 圓形 |
| ++probability | 否 | double | 置信度,范圍【0~1】,代表這是人臉形狀判斷正確的概率,0最小、1最大。 |
| +gender | 否 | array | 性別,face_field包含gender時返回 |
| ++type | 否 | string | male:男性?female:女性 |
| ++probability | 否 | double | 性別置信度,范圍【0~1】,0代表概率最小、1代表最大。 |
| +glasses | 否 | array | 是否帶眼鏡,face_field包含glasses時返回 |
| ++type | 否 | string | none:無眼鏡,common:普通眼鏡,sun:墨鏡 |
| ++probability | 否 | double | 眼鏡置信度,范圍【0~1】,0代表概率最小、1代表最大。 |
| +eye_status | 否 | array | 雙眼狀態(睜開/閉合)?face_field包含eye_status時返回 |
| ++left_eye | 否 | double | 左眼狀態 [0,1]取值,越接近0閉合的可能性越大 |
| ++right_eye | 否 | double | 右眼狀態 [0,1]取值,越接近0閉合的可能性越大 |
| +emotion | 否 | array | 情緒?face_field包含emotion時返回 |
| ++type | 否 | string | angry:憤怒?disgust:厭惡?fear:恐懼?happy:高興?sad:傷心?surprise:驚訝?neutral:無表情?pouty: 撅嘴?grimace:鬼臉 |
| ++probability | 否 | double | 情緒置信度,范圍0~1 |
| +face_type | 否 | array | 真實人臉/卡通人臉?face_field包含face_type時返回 |
| ++type | 否 | string | human: 真實人臉?cartoon: 卡通人臉 |
| ++probability | 否 | double | 人臉類型判斷正確的置信度,范圍【0~1】,0代表概率最小、1代表最大。 |
| +mask | 否 | array | 口罩識別?face_field包含mask時返回 |
| ++type | 否 | int | 沒戴口罩/戴口罩 取值0或1?0代表沒戴口罩 1 代表戴口罩 |
| ++probability | 否 | double | 置信度,范圍0~1 |
| +landmark | 否 | array | 4個關鍵點位置,左眼中心、右眼中心、鼻尖、嘴中心。face_field包含landmark時返回 |
| +landmark72 | 否 | array | 72個特征點位置?face_field包含landmark72時返回 |
| +landmark150 | 否 | array | 150個特征點位置?face_field包含landmark150時返回 |
| +quality | 否 | array | 人臉質量信息。face_field包含quality時返回 |
| ++occlusion | 否 | array | 人臉各部分遮擋的概率,范圍[0~1],0表示完整,1表示不完整 |
| +++left_eye | 否 | double | 左眼遮擋比例,[0-1] ,1表示完全遮擋 |
| +++right_eye | 否 | double | 右眼遮擋比例,[0-1] , 1表示完全遮擋 |
| +++nose | 否 | double | 鼻子遮擋比例,[0-1] , 1表示完全遮擋 |
| +++mouth | 否 | double | 嘴巴遮擋比例,[0-1] , 1表示完全遮擋 |
| +++left_cheek | 否 | double | 左臉頰遮擋比例,[0-1] , 1表示完全遮擋 |
| +++right_cheek | 否 | double | 右臉頰遮擋比例,[0-1] , 1表示完全遮擋 |
| +++chin | 否 | double | 下巴遮擋比例,,[0-1] , 1表示完全遮擋 |
| ++blur | 否 | double | 人臉模糊程度,范圍[0~1],0表示清晰,1表示模糊 |
| ++illumination | 否 | double | 取值范圍在[0~255], 表示臉部區域的光照程度 越大表示光照越好 |
| ++completeness | 否 | int64 | 人臉完整度,0或1, 0為人臉溢出圖像邊界,1為人臉都在圖像邊界內 |
| +spoofing | 否 | double | 判斷圖片是否為合成圖 |
人臉對比
import requests, base64def get_access_token():url = 'https://aip.baidubce.com/oauth/2.0/token'data = {'grant_type': 'client_credentials', # 固定值'client_id': '', # 在開放平臺注冊后所建應用的API Key'client_secret': '' # 所建應用的Secret Key}res = requests.post(url, data=data)res = res.json()access_token = res['access_token']return access_token# 根據圖片名讀取圖片,并轉換成base64 def read_img(img):with open(img, 'rb') as f:base64_data = base64.b64encode(f.read())base64_code = base64_data.decode()return base64_codedef get_json(img1,img2):request_url = "https://aip.baidubce.com/rest/2.0/face/v3/match"params = [{"image": img1,"image_type": "BASE64","face_type": "LIVE","quality_control": "LOW","liveness_control": "HIGH"},{"image": img2,"image_type": "BASE64","face_type": "LIVE","quality_control": "LOW","liveness_control": "HIGH"}]access_token = get_access_token()request_url = request_url + "?access_token=" + access_tokenheaders = {'content-type': 'application/json'}response = requests.post(request_url, json=params, headers=headers)if response:return response.json()if __name__ == '__main__':img1 = read_img('img/mm2.jpeg')img2 = read_img('img/mjl.jpg')json_data=get_json(img1,img2)print(json_data)if json_data['error_msg'] == 'SUCCESS':score = json_data['result']['score']if score > 80:print("照片相似度為:" + str(score) + "基本確定是本人")else:print("照片相似度為:" + str(score) + "基本確定不是本人")print(score)else:print('錯誤信息:', json_data['error_msg'])返回結果說明
| score | 是 | float | 人臉相似度得分,推薦閾值80分 |
| face_list | 是 | array | 人臉信息列表 |
| +face_token | 是 | string | 人臉的唯一標志 |
人臉融合
import requests import base64 import json# 獲取token def get_access_token():url = 'https://aip.baidubce.com/oauth/2.0/token'data = {'grant_type': 'client_credentials', # 固定值'client_id': '', # 在開放平臺注冊后所建應用的API Key'client_secret': '' # 所建應用的Secret Key}res = requests.post(url, data=data)res = res.json()access_token = res['access_token']return access_token# 根據圖片名讀取圖片,并轉換成base64 def read_photo(name):with open(name, 'rb') as f:base64_data = base64.b64encode(f.read())base64_code = base64_data.decode()return base64_code# 調用百度的接口,實現融合圖片 def face_fusion(template, target):access_token = get_access_token()url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'request_url = url + '?access_token=' + access_tokenparams = {"image_template": {"image": template,"image_type": "BASE64","quality_control": "NONE"},"image_target": {"image": target,"image_type": "BASE64","quality_control": "NONE"},"merge_degree": "NORMAL"}params = json.dumps(params)headers = {'content-type': 'application/json'}result = requests.post(request_url, data=params, headers=headers).json()if result['error_code'] == 0:res = result["result"]["merge_image"]down_photo(res)else:print(str(result['error_code'])+result['error_msg'])# 下載融合后圖片 def down_photo(data):imagedata = base64.b64decode(data)file = open('./result.jpg', "wb")file.write(imagedata)# 主程序 if __name__ == '__main__':template = read_photo('img/jjy.jpg')target = read_photo('img/mzc.jpeg')face_fusion(template, target)返回結果說明
| merge_image | string | 融合圖的BASE64值 |
人像動漫畫
import requests, base64# 百度AI開放平臺鑒權函數 def get_access_token():url = 'https://aip.baidubce.com/oauth/2.0/token'data = {'grant_type': 'client_credentials', # 固定值'client_id': '', # 在開放平臺注冊后所建應用的API Key'client_secret': '' # 所建應用的Secret Key}res = requests.post(url, data=data)res = res.json()access_token = res['access_token']return access_tokendef image_process(img_before, img_after, how_to_deal):# 函數的三個參數,一個是轉化前的文件名,一個是轉化后的文件名,均在同一目錄下,第三個是圖像處理能力選擇request_url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/' + how_to_dealif how_to_deal == 'style_trans': # 判斷如果是 圖像風格化,需要額外添加一個風格配置others = 'cartoon' # 風格化參數,具體可設置范圍參見下面注釋'''cartoon:卡通畫風格pencil:鉛筆風格color_pencil:彩色鉛筆畫風格warm:彩色糖塊油畫風格wave:神奈川沖浪里油畫風格lavender:薰衣草油畫風格mononoke:奇異油畫風格scream:吶喊油畫風格gothic:哥特油畫風格'''else:others = ''file = open(img_before, 'rb') # 二進制讀取圖片origin_img = base64.b64encode(file.read()) # 將圖片進行base64編碼headers = {'Content-Type': 'application/x-www-form-urlencoded'}data = {'access_token': get_access_token(),'image': origin_img,"type":'anime_mask',"mask_id":"2"}res = requests.post(request_url, data=data, headers=headers)res = res.json()print(res)if res:f = open(img_after, 'wb')after_img = res['image']after_img = base64.b64decode(after_img)f.write(after_img)f.close()if __name__ == '__main__':img_before = 'img/mm1.jpg' # 當前目錄下的圖片img_after = img_before.split('.') # 將原文件名分成列表img_after = img_after[0] + '_2.' + img_after[1] # 新生成的文件名為原文件名上加 _1image_process(img_before, img_after, 'selfie_anime')# 第三個參數: selfie_anime 為人像動漫化,colourize 圖像上色,style_trans 為圖像風格化print('done!')返回結果說明
| log_id | 是 | uint64 | 唯一的log id,用于問題定位 |
| image | 否 | string | 處理后圖片的Base64編碼 |
總結
以上是生活随笔為你收集整理的百度AI——人脸识别案例的全部內容,希望文章能夠幫你解決所遇到的問題。