認證受權:python
在開始調用任何API以前須要先進行認證受權,具體的說明請參考:json
http://ai.baidu.com/docs#/Auth/topapp
獲取Access Tokenide
向受權服務地址https://aip.baidubce.com/oauth/2.0/token發送請求(推薦使用POST),並在URL中帶上如下參數:函數
grant_type:?必須參數,固定爲client_credentials;測試
client_id:?必須參數,應用的API Key;編碼
client_secret:?必須參數,應用的Secret Key;url
例如:spa
具體代碼以下:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib
import json
#client_id 爲官網獲取的AK, client_secret 爲官網獲取的SK
client_id =【百度雲應用的AK】
client_secret =【百度雲應用的SK】
#獲取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content)
token_key = token_info['access_token']
return token_key
人臉檢測與屬性分析
具體說明請查閱:http://ai.baidu.com/docs#/Face-Detect-V3/top
人臉識別接口分爲V2和V3兩個版本,本文檔爲V3版本接口的說明文檔,請確認您在百度雲後臺得到的是V3版本接口權限,再來閱讀本文檔。
能力介紹
接口能力
人臉檢測:檢測圖片中的人臉並標記出位置信息;
人臉關鍵點:展現人臉的核心關鍵點信息,及150個關鍵點信息。
人臉屬性值:展現人臉屬性信息,如年齡、性別等。
人臉質量信息:返回人臉各部分的遮擋、光照、模糊、完整度、置信度等信息。
業務應用
典型應用場景:如人臉屬性分析,基於人臉關鍵點的加工分析,人臉營銷活動等。
調用方式
向API服務地址使用POST發送請求,必須在URL中帶上參數access_token,可經過後臺的API Key和Secret Key生成,具體方式請參考「Access Token獲取」。
請求說明
請求體格式化:Content-Type爲application/json,經過json格式化請求體。
Base64編碼:請求的圖片需通過Base64編碼,圖片的base64編碼指將圖片數據編碼成一串字符串,使用該字符串代替圖像地址。您能夠首先獲得圖片的二進制,而後用Base64格式編碼便可。須要注意的是,圖片的base64編碼是不包含圖片頭的,如data:image/jpg;base64,
圖片格式:現支持PNG、JPG、JPEG、BMP,不支持GIF圖片
HTTP方法:POST
請求URL:https://aip.baidubce.com/rest/2.0/face/v3/detect
Python3代碼以下:
#獲取人臉檢測信息
def face_detect_url(url):
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
params = dict()
params['image'] = url
params['image_type'] = 'URL'
params['face_field'] = 'faceshape,facetype,landmark,landmark72'
params = json.dumps(params).encode('utf-8')
access_token=get_token()
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(request)
content = response.read()
if content:
print (content)
return content
else:
return ''
這個函數將根據網上圖片URL地址獲取圖片並進行檢測。讓咱們用網上找的一張圖片進行測試:
執行代碼:
image_url='http://p0.qhimgs4.com/t0159dd32381d0c25b7.jpg'
face_detect_url(image_url)
返回結果:
b'{"error_code":0,"error_msg":"SUCCESS","log_id":304592843452042151,"timestamp":1554345204,"cached":0,"result":{"face_num":1,"face_list":[{"face_token":"5951950e8c05735152e696163b9a59cc","location":{"left":153.82,"top":248.56,"width":318,"height":317,"rotation":3},"face_probability":0.97,"angle":{"yaw":16.99,"pitch":10.57,"roll":-2.37},"face_shape":{"type":"heart","probability":0.37},"face_type":{"type":"human","probability":0.96},"landmark":[{"x":204.94,"y":313.87},{"x":340.33,"y":321.52},{"x":244.5,"y":402.48},{"x":256,"y":469.66}],"landmark72":[{"x":150.9,"y":310.37},{"x":146.88,"y":354.74},{"x":146.91,"y":400.53},{"x":155.44,"y":445.99},{"x":174.77,"y":493.18},{"x":205.04,"y":541.51},{"x":246.8,"y":572.16},{"x":303.11,"y":568.65},{"x":361.73,"y":541.55},{"x":413.84,"y":495.42},{"x":440.27,"y":442.75},{"x":456.03,"y":388.79},{"x":467.57,"y":334.35},{"x":171.98,"y":314.72},{"x":185.4,"y":302.98},{"x":200.74,"y":300.89},{"x":217.11,"y":307.92},{"x":229.67,"y":325.15},{"x":213.74,"y":325.74},{"x":196.71,"y":325.45},{"x":182.26,"y":321.23},{"x":204.94,"y":313.87},{"x":157.69,"y":275.88},{"x":172.48,"y":256.88},{"x":193.09,"y":254.09},{"x":213.77,"y":259.09},{"x":231.35,"y":276.15},{"x":211.35,"y":272.76},{"x":191.75,"y":269.52},{"x":173.47,"y":269.62},{"x":307.03,"y":329.36},{"x":321.9,"y":313.15},{"x":339.63,"y":308.27},{"x":357.91,"y":313.5},{"x":374.37,"y":327.15},{"x":358.16,"y":332.11},{"x":339.75,"y":333.86},{"x":322.58,"y":332.47},{"x":340.33,"y":321.52},{"x":299.9,"y":278.14},{"x":326.07,"y":261.57},{"x":354.67,"y":260.59},{"x":383.58,"y":268.55},{"x":406.72,"y":293.57},{"x":380.55,"y":281.15},{"x":353.84,"y":276.67},{"x":326.51,"y":278},{"x":243.97,"y":326.97},{"x":234.67,"y":354.6},{"x":224.5,"y":382.86},{"x":212.93,"y":410},{"x":229.08,"y":417.3},{"x":272.61,"y":419.89},{"x":299.76,"y":416.45},{"x":288.56,"y":386.93},{"x":285.88,"y":358.4},{"x":282.93,"y":329.6},{"x":244.5,"y":402.48},{"x":200.9,"y":453.98},{"x":223.42,"y":448},{"x":251.74,"y":451.01},{"x":289.71,"y":452.96},{"x":329.26,"y":462.63},{"x":293.65,"y":494.31},{"x":249.35,"y":504.21},{"x":216.52,"y":487},{"x":223.98,"y":456.81},{"x":251.93,"y":461.81},{"x":289.22,"y":461.47},{"x":289.29,"y":480.88},{"x":251.7,"y":484.9},{"x":223.34,"y":475.3}]}]}}'
如今你們只能看到檢測的數字,爲了方便你們查看圖片,我把獲取圖片及畫圖的Python3代碼也寫了。以下:
def get_landmark72(content):
content=content.decode('utf-8')
data = json.loads(content)
#print (data)
return data['result']['face_list'][0]['landmark72']
def drow_face_line_72(url,imagefile,linedfile,landmark72):
from PIL import Image, ImageDraw
from io import BytesIO
import requests
response = requests.get(url)
image_origin = Image.open(BytesIO(response.content))
image_origin.save(imagefile)
restart_points=[0,13,22,30,39,47,58]
#image_origin = Image.open(imagefile)
draw =ImageDraw.Draw(image_origin)
i=0
for point in landmark72:
if i in restart_points:
start_x=point['x']
start_y=point['y']
else:
draw.line((start_x, start_y, point['x'], point['y']), 'blue')
start_x=point['x']
start_y=point['y']
i=i+1
#draw.line((0,0) +Image1.size, fill=128)
image_origin.show()
image_origin.save(linedfile, "JPEG")
def face_detect_line(url,imagefile,linedfile):
content=face_detect_url(url)
if len(content)>0:
landmark72=get_landmark72(content)
drow_face_line_72(url,'origin.jpg','lined.jpg',landmark72)
執行face_detect_line(image_url,'origin.jpg','lined.jpg'),將在代碼目錄生成,兩張圖片一張是原圖,一張是根據landmark72劃線後的圖片。
以下圖所示(圖片來自網上,僅爲示例使用):
你們還能夠用網上找的其餘人臉圖片的URL來試一下。
建議:
感受如今的人臉檢測功能十分的強大和準確,有一個小建議,若是是張嘴的圖,可否把人的牙齒和舌頭等其餘部分區分的點也都標出來。若是能實現的話就太好了。