AI人脸比对技术
百度智能云注册
https://login.bce.baidu.com/?account=&redirect=https%3A%2F%2Fconsole.bce.baidu.com%2Fiam%2F#/iam/baseinfo
选择人工智能-->人脸识别认证

选择-->立即使用

选择免费尝鲜(领取之后五分钟就可以在我的应用里查看了,里面勾选所有的选项)


在我的应用里查看公共云(记住AppID、APIKey、SelectKey,程序中需要)

在IDEA中运行程序
先导入需要的依赖
<dependencies>
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.4.0</version>
</dependency>
</dependencies>
再运行下面这个程序
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
public class Sample {
public int faceCompare(AipFace client,String imagePath1,String imagePath2) {
// 将图片文件转换为base64编码的字符串
String image1 = encodeImageToBase64(imagePath1);
String image2 = encodeImageToBase64(imagePath2);
// image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应。
MatchRequest req1 = new MatchRequest(image1, "BASE64");
MatchRequest req2 = new MatchRequest(image2, "BASE64");
ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
requests.add(req1);
requests.add(req2);
JSONObject res = client.match(requests);
System.out.println(res.toString(2));
return res.getInt("score");
}
private String encodeImageToBase64(String imagePath) {
try (FileInputStream imageInFile = new FileInputStream(imagePath)) {
// 读取图片字节数据
byte[] bytes = new byte[(int) new File(imagePath).length()];
imageInFile.read(bytes);
// 转换为base64编码的字符串
return Base64.getEncoder().encodeToString(bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
Sample sample = new Sample();
AipFace client = new AipFace("Enter Your APP_ID", "Enter Your API_KEY", "Enter Your SECRET_KEY");//输入之前保存的三个参数
String imagePath1 = "path/to/image1";//输入你要比对的照片
String imagePath2 = "path/to/image2";//输入你要比对的第二张照片
int score = sample.faceCompare(client, imagePath1, imagePath2);
System.out.println("Score: " + score);// 输出比对的相似度结果
}
}