• 阿里云人脸识别接口调用。


    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 + "
    " + options.headers.accept + "
    " + bodymd5 + "
    " + options.headers['content-type'] + "
    " + options.headers.date + "
    " + 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

    
    
  • 相关阅读:
    了解Whitehorse
    更新排行榜说明
    一个从Microsoft Word发表Blog文章的工具
    首页"进入我的博客"可以正常使用了
    ADO.NET: Close()与Dispose() 的讨论
    向大家致歉
    折腾了我一个下午及吃晚饭时间的问题
    [转帖]ASP.NET服务器端异步Web方法
    关于增加“收藏”功能的设想
    Mono 0.30发布了
  • 原文地址:https://www.cnblogs.com/MainActivity/p/8507656.html
Copyright © 2020-2023  润新知