一、申请百度API人脸识别的使用权利
http://ai.baidu.com/tech/face(点击进入官方网站,然后找到产品服务-人脸识别)
二、注册百度账号,并且申请人脸识别的使用权(一般几分钟就可以了,不会太久),获取以下参数,为调用API做准备
AppID
|
API Key
|
Secret Key
|
---|
三、接下来,开始敲代码调用百度API了,具体技术需要可以查看相关文档(https://ai.baidu.com/docs#/Face-Detect/top)
详细代码如下
package com;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONObject;
import com.baidu.aip.face.AipFace;
public class BaiduFace {
//以下参数分别对应着意见申请好的AppID、API Key 以及 Secret Key
public static final String APP_ID = "输入AppID";
public static final String API_KEY = "输入API Key";
public static final String SECRET_KEY = "输入Secret Key";
public static void main(String[] args) {
AipFace client = new AipFace(APP_ID , API_KEY , SECRET_KEY);
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
String img_1 = "F:/img/1.jpg"; //需要进行人脸识别的图像位置
String img_2 = "F:/img/2.jpg"; //需要进行人脸识别的图像位置
ArrayList pathArray = new ArrayList();
pathArray.add(img_1);
pathArray.add(img_2);
JSONObject response = client.match(pathArray , new HashMap());
System.out.println(response.toString()); //输出的是JSON格式
}
}
输出:{"result":[{"score":64.786140441895,"index_i":"0","index_j":"1"}],"log_id":3877673471041318,"result_num":1}
其中,score代表相似度,相似度越高,说明两张图片的人脸越像是同一个人。