阿里云 人脸识别 测试

阿里云 人脸识别 测试

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"
}

3.参考

阿里云 人脸属性识别API调用说明

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值