這裏,記錄一下百度人臉識別在線API的調用,語言是python2.7,供大家一起學習參考
本教程目錄如下
1.申請百度人臉識別應用
首先需要在百度智能雲平臺登錄
https://console.bce.baidu.com/ai/?_=1558444274128&fromai=1#/ai/face/overview/index
登陸後創建應用,創建的應用是人臉識別的,默認已幫你勾選上了相關功能。
創建應用後,點擊管理應用,你就可以獲取API Key和Secret Key
2.獲取token
現在就可以編寫代碼調用在線的人臉檢測API了
首先是獲取token,用於校驗,代碼如下,注意換成自己申請的API Key和Secret Key
def getToken():
global token
# client_id 爲官網獲取的AK, client_secret 爲官網獲取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的API Key&client_secret=你的Secret Key'
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
if (content):
token=json.loads(content)['access_token']
3.圖片的base64編碼
上傳的用於檢測的圖片需經過Base64編碼
。需要注意的是,圖片的base64編碼是不包含圖片頭的,代碼如下:
def imgToBase64(imgPath):
with open(imgPath, "rb") as f: # 轉爲二進制格式
base64_data = base64.b64encode(f.read()) # 使用base64進行加密
return base64_data
4.人臉識別
最後就是調用接口進行人臉識別了
def faceDetect(imgBase64):
'''
人臉檢測與屬性分析
'''
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
request_url = request_url + "?access_token=" + token
request = urllib2.Request(request_url)
request.add_header('Content-Type', 'application/json')
data = {"image": imgBase64, "image_type": "BASE64","face_field":"age,beauty,expression,face_shape,gender"}
response = urllib2.urlopen(request, urllib.urlencode(data))
content = response.read()
if content:
return content
這個函數中輸入的是圖片的base64編碼,請求的參數中比較重要的是那個face_field,默認只返回人臉框的位置、概率和旋轉角度,age(年齡預測),beauty(顏值打分),expression(表情)等更多屬性,需要在這個參數中添加,具體的請參考官方說明文檔:http://ai.baidu.com/docs#/Face-Detect-V3/top
5.結果繪製與可視化
人臉識別最後返回的是json數據,但我們往往需要畫個框框,把人臉框出來,同時把一些預測的屬性也標註上,這個代碼我已經附在最後面了。
最終實現的效果如下:
6.實現的完整源代碼
下面我附上實現這些功能的完整代碼:
#coding:utf-8
import urllib,urllib2, sys
import ssl
import json
import base64
import cv2
global token
def getToken():
global token
# client_id 爲官網獲取的AK, client_secret 爲官網獲取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的API Key&client_secret=你的Secret Key'
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
if (content):
token=json.loads(content)['access_token']
def faceDetect(imgBase64):
'''
人臉檢測與屬性分析
'''
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
request_url = request_url + "?access_token=" + token
request = urllib2.Request(request_url)
request.add_header('Content-Type', 'application/json')
data = {"image": imgBase64, "image_type": "BASE64","face_field":"age,beauty,expression,face_shape,gender"}
response = urllib2.urlopen(request, urllib.urlencode(data))
content = response.read()
if content:
return content
def imgToBase64(imgPath):
with open(imgPath, "rb") as f: # 轉爲二進制格式
base64_data = base64.b64encode(f.read()) # 使用base64進行加密
return base64_data
if __name__=="__main__":
getToken()
imgPath=r"C:\Users\lee\Pictures\lena.jpg"
result=json.loads(faceDetect(imgToBase64(imgPath)))['result']
face_list=result['face_list'][0]
location=face_list['location']
age=face_list['age']
beauty=face_list['beauty']
expression=face_list['expression']['type']
gender=face_list['gender']['type']
img = cv2.imread(imgPath, cv2.IMREAD_COLOR)
leftTopX=int(location['left'])
leftTopY=int(location['top'])
rightBottomX=int(leftTopX+int(location['width']))
rightBottomY = int(leftTopY + int(location['height']))
cv2.rectangle(img, (leftTopX, leftTopY), (rightBottomX, rightBottomY), (0, 255, 0), 2)
font = cv2.FONT_HERSHEY_SIMPLEX
# 第一個座標表示起始位置
cv2.putText(img,"age:"+str(age),(0, 20),font, 0.5, (200, 255, 255), 1)
cv2.putText(img, "gender:" + gender.encode("utf-8"), (0, 40), font, 0.5, (200, 255, 255), 1)
cv2.putText(img, "beauty:" + str(beauty), (0, 60), font, 0.5, (200, 255, 255), 1)
cv2.putText(img, "expression:" + str(expression), (0, 80), font, 0.5, (200, 255, 255), 1)
cv2.imshow('image', img)
cv2.waitKey(0)
print("end")