称之为单机版,主要是相对于调用摄像头实时识别而言。本篇主要py2下利用face++和百度接口获取本地图片中的人脸属性,并按照一定格式保存数据。
face++版
face++是刚注册的,只能用一个试用的key,并且有QPS限制(这个嘛,哪个免费接口没有限制。不过个人觉得这个限制比百度的严重些),还有就是一帧画面只能识别最大的5张脸。能获取较为丰富的表情数据这点开阔以。。。
# -*- coding: utf-8 -*-
import urllib2
import json
import time
##################################################
#协议url
http_url='https://api-cn.faceplusplus.com/facepp/v3/detect'
#公钥
key = "你自己的key"
#密钥
secret = "你自己的secret"
#返回值,你所需要的属性
return_attributes = 'gender,age,emotion,glass,headpose,beauty'
#图片地址
filepath = r"D:\workspaces\timg8.jpg"
参数协议分隔标示
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)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_attributes')
data.append(return_attributes)
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--\r\n' % boundary)
###########################################################
#发送POST请求
http_body='\r\n'.join(data)
req=urllib2.Request(http_url)
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
req.add_data(http_body)
##########################################################
try:
#req.add_header('Referer','http://remotserver.com/')
#post data to server
#获得结果
resp = urllib2.urlopen(req, timeout=5)
#打印结果
qrcont=resp.read()
print qrcont
#下面2句返回调用时出现的异常
except urllib2.HTTPError as e:
print e.read()
json_resp = json.loads(qrcont)
num = len(json_resp['faces']) #人脸个数
face_s = json_resp['faces']
face_datas = [[]]*num
for i in range(0,num):
tempface = face_s[i]
data2=tempface['attributes']
gender = data2['gender'].values()[0]
age = data2['age'].values()[0]
glass = data2['glass'].values()[0]
headpose = 1 if data2['headpose']['pitch_angle'] > 0 else 0
emotion = data2['emotion']
emotion =max(emotion, key=emotion.get)
face_datas[i] = [gender,age,glass,headpose,emotion]
print face_datas
#face = {'gender':gender,'age':age,'glass':glass,'headpose':headpose,'emotion':emotion}
baidu版
百度的人脸识别是免费的,调用量没有限制,只有QPS限制为2.
# coding : UTF-8
from aip import AipFace
# 定义常量
APP_ID = '你自己的ID'
API_KEY = '你自己的key'
SECRET_KEY = '你自己的secret'
# 初始化AipFace对象
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)
# 读取图片
filePath = r"D:\workspaces\tmp_cap.jpg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 定义参数变量
options = {
'max_face_num': 10,
'face_fields': "age,gender,glasses,expression,faceshape",
}
# 调用人脸属性检测接口
#返回result字典,key为[u'log_id', u'result_num', u'result']
result = aipFace.detect(get_file_content(filePath),options)
face_num = result['result_num'] #人脸数目