1.阿里云开通人脸识别
2.以人脸属性识别为例
3.为了安全起见,我们需要搭建一个服务器,以nodejs为例
nodejs代码
var request = require(\'request\');
var url = require(\'url\');
var crypto = require(\'crypto\');
var date = new Date().toUTCString();
var express = require(\'express\');
// 这里填写AK和请求
var ak_id = \'你的ak\';
var ak_secret = \'你的ak_secret\';
var app = express();
//人脸属性识别
function getFaceAttribute(obj,getMsg){
var options = {
//这里调用的是人脸属性识别接口
url : \'https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute\',
method: \'POST\',
body:obj,//这里传递参数
headers: {
\'accept\': \'application/json\',
\'content-type\': \'application/json\',
\'date\': date,
\'Authorization\': \'\'
}
};
md5 = function(buffer) {
var hash;
hash = crypto.createHash(\'md5\');
hash.update(buffer);
return hash.digest(\'base64\');
};
sha1 = function(stringToSign, secret) {
var signature;
return signature = crypto.createHmac(\'sha1\', secret).update(stringToSign).digest().toString(\'base64\');
};
// step1: 组stringToSign [StringToSign = #{method}\\n#{accept}\\n#{data}\\n#{contentType}\\n#{date}\\n#{action}]
var body = options.body || \'\';
var bodymd5;
if(body === void 0 || body === \'\'){
bodymd5 = body;
} else {
bodymd5 = md5(new Buffer(body));
}
console.log(bodymd5)
var stringToSign = options.method + "\n" + options.headers.accept + "\n" + bodymd5 + "\n" + options.headers[\'content-type\'] + "\n" + options.headers.date + "\n" + url.parse(options.url).path;
console.log("step1-Sign string:", stringToSign);
// step2: 加密 [Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign) ) )]
var signature = sha1(stringToSign, ak_secret);
// console.log("step2-signature:", signature);
// step3: 组authorization header [Authorization = Dataplus AccessKeyId + ":" + Signature]
var authHeader = "Dataplus " + ak_id + ":" + signature;
console.log("step3-authorization Header:", authHeader);
options.headers.Authorization = authHeader;
console.log(\'authHeader\', authHeader);
// step4: send request
/*if (error) {
console.log("error", error)
}
console.log("step4-response body:", response.statusCode, body)*/
function callback(error, response, body) {
getMsg(error,response,body);
}
request(options, callback);
}
//设置跨域访问
app.all(\'*\', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",\' 3.2.1\')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.get(\'/face/getFaceAttribute\', function(req, res) {
getFaceAttribute(\'{image_url:"\'+req.query.image_url+\'"}\',function(error,response,body){
if (error) {
console.log("error", error)
}
console.log("step4-response body:", response.statusCode, body)
res.end(JSON.stringify(body));
});
});
app.listen(8888);
console.log("port at 8888");
如果在本地运行此nodejs的话,地址是127.0.0.1:8888/face/getFaceAttribute
请求方式是get
这个时候前端页面只需要调用这个ajax地址就好了
$.ajax({
type:"get",
url:"http://127.0.0.1:8888/face/getFaceAttribute",//服务器我是部署在本地
async:true,
data:{\'image_url\':\'www.yuanyizheng.com/fuzi.jpg\'},
success:function(data){
console.log(\'success\');
console.log(data);
},
error:function(data){
console.log(\'error\')
console.log(data);
}
});
详情请看
https://help.aliyun.com/knowledge_detail/53520.html