認證授權(quán):
在開始調(diào)用任何API之前需要先進行認證授權(quán)糖荒,具體的說明請參考:
http://ai.baidu.com/docs#/Auth/top
獲取Access Token
向授權(quán)服務(wù)地址https://aip.baidubce.com/oauth/2.0/token發(fā)送請求(推薦使用POST)埠帕,并在URL中帶上以下參數(shù):
grant_type:?必須參數(shù)奴迅,固定為client_credentials霎冯;
client_id:?必須參數(shù)邻辉,應(yīng)用的API Key猿棉;
client_secret:?必須參數(shù)节吮,應(yīng)用的Secret Key;
例如:
具體代碼如下:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib
import json
#client_id 為官網(wǎng)獲取的AK瘪匿, client_secret 為官網(wǎng)獲取的SK
client_id =【百度云應(yīng)用的AK】
client_secret =【百度云應(yīng)用的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版本接口權(quán)限棋弥,再來閱讀本文檔核偿。
能力介紹
接口能力
人臉檢測:檢測圖片中的人臉并標記出位置信息;
人臉關(guān)鍵點:展示人臉的核心關(guān)鍵點信息,及150個關(guān)鍵點信息顽染。
人臉屬性值:展示人臉屬性信息漾岳,如年齡轰绵、性別等。
人臉質(zhì)量信息:返回人臉各部分的遮擋尼荆、光照左腔、模糊、完整度耀找、置信度等信息翔悠。
業(yè)務(wù)應(yīng)用
典型應(yīng)用場景:如人臉屬性分析业崖,基于人臉關(guān)鍵點的加工分析野芒,人臉營銷活動等。
調(diào)用方式
向API服務(wù)地址使用POST發(fā)送請求双炕,必須在URL中帶上參數(shù)access_token狞悲,可通過后臺的API Key和Secret Key生成,具體方式請參考“Access Token獲取”妇斤。
請求說明
請求體格式化:Content-Type為application/json摇锋,通過json格式化請求體。
Base64編碼:請求的圖片需經(jīng)過Base64編碼站超,圖片的base64編碼指將圖片數(shù)據(jù)編碼成一串字符串荸恕,使用該字符串代替圖像地址。您可以首先得到圖片的二進制死相,然后用Base64格式編碼即可融求。需要注意的是,圖片的base64編碼是不包含圖片頭的算撮,如data:image/jpg;base64,
圖片格式:現(xiàn)支持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 ''
這個函數(shù)將根據(jù)網(wǎng)上圖片URL地址獲取圖片并進行檢測。讓我們用網(wǎng)上找的一張圖片進行測試:
執(zhí)行代碼:
image_url='http://p0.qhimgs4.com/t0159dd32381d0c25b7.jpg'
face_detect_url(image_url)
返回結(jié)果:
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}]}]}}'
現(xiàn)在大家只能看到檢測的數(shù)字审洞,為了方便大家查看圖片莱睁,我把獲取圖片及畫圖的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)
執(zhí)行face_detect_line(image_url,'origin.jpg','lined.jpg')芒澜,將在代碼目錄生成缩赛,兩張圖片一張是原圖,一張是根據(jù)landmark72劃線后的圖片撰糠。
如下圖所示(圖片來自網(wǎng)上酥馍,僅為示例使用):
大家還可以用網(wǎng)上找的其他人臉圖片的URL來試一下。
建議:
感覺現(xiàn)在的人臉檢測功能十分的強大和準確阅酪,有一個小建議旨袒,如果是張嘴的圖汁针,能否把人的牙齒和舌頭等其他部分區(qū)分的點也都標出來。如果能實現(xiàn)的話就太好了砚尽。