SSM人脸识别签到小程序
前言
随着人工智能的不断发展,人脸识别这门技术也越来越重要,很多人都开启了人脸识别算法学习,本文就介绍了基于百度人脸识别API使用的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、人脸识别开通服务
!!!小程序端demo属于github上所git
源码地址
本次使用人脸识别接口是基于百度的人脸识别服务,百度提供免费接口。
二、使用步骤
1、登录百度云
2、创建应用
3、获取密钥
4、获取相应的JAR包
相应jar包获取方式如下:
1.直接下载
2.Maven方式
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>${version}</version>
</dependency>
三、实现
1、工具类
client.java填入相应的密钥public class Client {
private static final String APP_ID = "123";
private static final String API_KEY = "123";
private static final String SECRET_KEY = "123";
private static final String IMAGE_TYPE = "BASE64";
private final AipFace aipFace;
private String storagePath;
public Client() {
aipFace = new AipFace(APP_ID, API_KEY, SECRET_KEY);
aipFace.setConnectionTimeoutInMillis(2000);
aipFace.setSocketTimeoutInMillis(60000);
}
public Double match(byte[] img1, byte[] img2) {
MatchRequest request1 = new MatchRequest(bytesToBase64(img1), IMAGE_TYPE);
MatchRequest request2 = new MatchRequest(bytesToBase64(img2), IMAGE_TYPE);
JSONObject jsonObject = aipFace.match(Arrays.asList(request1, request2));
//System.out.println("查看返回结果"+jsonObject.toString());
if (jsonObject.optString("error_msg", "").equalsIgnoreCase("SUCCESS")) {
return jsonObject.optJSONObject("result").optDouble("score", 0D);
}
return 0D;
}
private String bytesToBase64(byte[] data) {
if (data == null) {
data = new byte[]{};
}
return Base64.getEncoder().encodeToString(data);
}
public byte[] readImage(HttpServletRequest request, String parameter) {
try {
String filename = request.getParameter(parameter);
try (InputStream in = new FileInputStream(new File(this.storagePath, filename))
) {
return IOUtils.toByteArray(in);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
2、上传相应文件
@ResponseBody
@RequestMapping(value ="/picture")
public String uploadPicture(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile files,@RequestParam("id") String id) throws Exception {
System.out.println("图片上传请求成功"+files.getOriginalFilename());
String type = files.getOriginalFilename().substring(files.getOriginalFilename().lastIndexOf("."));
System.out.println("类型"+type);
String path = "E:/Javadata/wechatWalkRest/WebContent/static/images/face";
String a=files.getOriginalFilename();
System.out.println(a);
File targetFile = new File(path + "/",a);
if (!targetFile.exists()) {
targetFile.createNewFile();
}
files.transferTo(targetFile);
User user=userinfoServiceImpl.query("11");
System.out.println("路径为"+user.toString());
byte[] imageLeft=readImage(path,a);
byte[] imageRight =readImage1(path,user.getUrl());
System.out.println(client.match(imageLeft, imageRight));
System.out.println(targetFile.delete());
return ""+client.match(imageLeft, imageRight);
}
public byte[] readImage(String path,String name) {
try {
try (InputStream in = new FileInputStream(new File(path, name))
) {
return IOUtils.toByteArray(in);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public byte[] readImage1(String path,String name) {
try {
try (InputStream in = new FileInputStream(new File(path, name))
) {
return IOUtils.toByteArray(in);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
3、小程序端
四、演示
展示
所选图片
对比图片
两张相似度为百分之76.65333557
微信小程序反应