阿里云 人脸识别 测试
1.使用阿里云平台提供的接口直接测试人脸识别功能
参考代码:python2.x
#!/usr/bin/python
# -*- coding:utf-8 -*-
from urlparse import urlparse
import datetime
import base64
import hmac
import hashlib
import json
import urllib2
def get_current_date():
date = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")
return date
def to_md5_base64(strBody):
hash = hashlib.md5()
hash.update(body)
return hash.digest().encode('base64').strip()
def to_sha1_base64(stringToSign, secret):
hmacsha1 = hmac.new(secret, stringToSign, hashlib.sha1)
return base64.b64encode(hmacsha1.digest())
ak_id = '添加阿里云AccessKey ID'
ak_secret = '添加阿里云Access Key Secret'
options = {
'url': 'https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute',
'method': 'POST',
'body': json.dumps({"type": 0,
"image_url": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1550729205230&di=4e533df8440316eaf5c237f6fdbcb40a&imgtype=0&src=http%3A%2F%2Fimg2.cache.netease.com%2Fphoto%2F0026%2F2017-04-22%2FCILFTVLK78LH0026.jpg"
}, separators=(',', ':')),
'headers': {
'accept': 'application/json',
'content-type': 'application/json',
'date': get_current_date(),
'authorization': ''
}
}
body = ''
if 'body' in options:
body = options['body']
print body
bodymd5 = ''
if not body == '':
bodymd5 = to_md5_base64(body)
print bodymd5
urlPath = urlparse(options['url'])
if urlPath.query != '':
urlPath = urlPath.path + "?" + urlPath.query
else:
urlPath = urlPath.path
stringToSign = options['method'] + '\n' + options['headers']['accept'] + '\n' + bodymd5 + '\n' + options['headers'][
'content-type'] + '\n' + options['headers']['date'] + '\n' + urlPath
signature = to_sha1_base64(stringToSign, ak_secret)
print stringToSign
authHeader = 'Dataplus ' + ak_id + ':' + signature
options['headers']['authorization'] = authHeader
print authHeader
request = None
method = options['method']
url = options['url']
print method
print url
if 'GET' == method or 'DELETE' == method:
request = urllib2.Request(url)
elif 'POST' == method or 'PUT' == method:
request = urllib2.Request(url, body)
request.get_method = lambda: method
for key, value in options['headers'].items():
request.add_header(key, value)
try:
conn = urllib2.urlopen(request)
response = conn.read()
print response
except urllib2.HTTPError, e:
print e.read()
raise SystemExit(e)
2.人脸识别返回结果
{
"face_num":1,
"face_rect":[
248,
96,
98,
133
],
"face_prob":[
1
],
"pose":[
-16.07172966003418,
-8.95702838897705,
1.9855666160583496
],
"landmark_num":105,
"landmark":[
247.61978149414062,
137.2036590576172,
268.0321350097656,
139.6984405517578,
135.26028442382812,
# ... ... 略去n行数据
177.718994140625,
263.8021240234375,
211.8135986328125,
309.4313049316406,
223.700439453125
],
"iris":[
266.2425842285156,
150.38868713378906,
4.3046369552612305,
309.6331481933594,
154.08685302734375,
4.3046369552612305
],
"gender":[
0
],
"age":[
23
],
"expression":[
1
],
"glass":[
0
],
"dense_fea_len":1024,
"dense_fea":[
-0.025663359090685844,
0.023277662694454193,
-0.0033915694802999496,
-0.029405198991298676,
0.03825557976961136,
# ... ... 略去n行数据
0.002641384955495596,
-0.029730748385190964,
-0.0018590112449601293,
-0.04892149567604065,
0.03814288601279259,
0.0002941368438769132
],
"errno":0,
"request_id":"11e53710-7b6e-48cd-86e4-963e0e48fda9"
}