Python調用face++API完成本地圖片的人臉檢測
簡單調用face++API對本地圖片進行人臉檢測,輸出基本信息到csv文件。
註冊face++賬號
face++網址 https://www.faceplusplus.com.cn/
註冊完成之後,查看自己的 API Key 和 API Secret
改寫官網代碼示例完成人臉信息採集
原代碼示例網址 https://console.faceplusplus.com.cn/documents/6329752
選用原代碼示例中的python代碼對其進行符合人臉檢測信息採集需求的修改,代碼如下,具體代碼解釋見下節:
# -*- coding: utf-8 -*-
import urllib2
import time
import os
from pandas.core.frame import DataFrame
result = []
http_url='https://api-cn.faceplusplus.com/facepp/v3/detect'
key = "sJVodwPAjfbo18Vc2OCZjIUuM1u2BhWe" #這裏是你申請的API Key
secret = "29dB1GxL9oXbv7P117CLa4ZyUdHzj1ad" #這裏是你申請的API Secret
filedir = r"C:\Users\Desktop\face\data\images" #這裏是你要檢測圖片文件夾的路徑
writepath = "C:\\Users\\Desktop\\face\\data\\note.csv" #這裏替換爲你寫入csv文件的路徑
filelist = os.listdir(filedir)
for filename in filelist:
filepath = os.path.join(filedir,filename)
boundary = '----------%s' % hex(int(time.time() * 1000))
data = []
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key')
data.append(key)
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret')
data.append(secret)
data.append('--%s' % boundary)
fr=open(filepath,'rb')
data.append('Content-Disposition: form-data; name="%s"; filename=" "' % 'image_file')
data.append('Content-Type: %s\r\n' % 'application/octet-stream')
data.append(fr.read())
fr.close()
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_landmark')
data.append('1')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_attributes')
data.append("gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus")
data.append('--%s--\r\n' % boundary)
http_body='\r\n'.join(data)
#buld http request
req=urllib2.Request(http_url)
#header
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
req.add_data(http_body)
#這裏提取圖片的性別、年齡、人臉框位置,並輸出該圖片的絕對路徑。
#若提取其他屬性,請修改以下代碼,將result中加入所需屬性並輸出
try:
resp = urllib2.urlopen(req, timeout=5)
qrcont = resp.read()
dict = eval(qrcont)
faces = dict['faces']
for i in range(len(faces)):
attribute = faces[i]['attributes']
gender = attribute['gender']['value']
age = attribute['age']['value']
face_rectangle = faces[i]['face_rectangle']
width = face_rectangle['width']
top = face_rectangle['top']
left = face_rectangle['left']
height = face_rectangle['height']
result.append([filepath,gender,age,width,top,left,height])
except urllib2.HTTPError as e:
print e.read()
DataFrame(result).to_csv(writepath,header=['filepath','gender','age','width','top','left','heigth'])
返回變量
在本示例中,只提取了檢測到人臉的性別、年齡、人臉框位置,而face++人臉檢測API提供了十分全面的返回值,具體返回值課件參考官網文檔 https://console.faceplusplus.com.cn/documents/4888373
這裏,解釋上述代碼提取各變量的含義。
字段 | 類型 | 說明 |
---|---|---|
gender | String | 性別分析結果,Male爲男性,Female爲女性 |
age | Int | 年齡分析結果。返回值爲一個非負整數 |
face_rectangle | Object | 人臉矩形框的位置,包括以下屬性 |
top | Int | 矩形框左上角像素點的縱座標 |
left | Int | 矩形框左上角像素點的橫座標 |
width | Int | 矩形框的寬度 |
height | Int | 矩形框的高度 |
代碼結果
若路徑filedir下有如下一張圖片:
使用python2.7運行代碼,可以得到一個絕對路徑爲writepath的csv文件如下
face++人臉檢測API檢測出5張人臉,並將人臉信息保存在了csv文件中。
注意事項
- 改進的示例代碼使用python2編譯
- filedir和writepath兩個路徑不要帶有中文字符
- 需要事先安裝urllib2