java调用百度云人脸识别API
System.out.println("百度返回的user对象:"+user.toString());System.out.println("这是res========="+res);// 给上传的图片命名,格式:用户名_当前时间的毫秒数.jpg。//识别到的用户信息。
·
package com.sike.controller; import com.baidu.aip.face.AipFace; import com.sike.entity.User; import com.sike.service.UserService; import jakarta.servlet.http.HttpServletRequest; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Base64; import java.util.HashMap; @Controller public class FaceController { //从百度申请到的三种参数,用于连接其人脸识别接口api //读者要替换成自己从百度申请到的这三种数据,这个很快会过期 private static final String APP_ID = "116060324"; private static final String API_KEY = "KrLMgijnIAqvLjpWo6uRFdj3"; private static final String SECRET_KEY = "GWg4DtQNZ9IsXDjHjBczdRIi5l3h1c9S"; @Autowired private UserService userService; //将收集到的用户名和相片发送给百度进行注册,其中相片是Base64格式 //同时相片上传到服务器的photo文件夹存储,用户数据也保存一份到数据库 @RequestMapping(value = "register",method = RequestMethod.POST) @ResponseBody public String register(String userName,String faceBase) throws IOException { if(!StringUtils.isEmpty(userName) && !StringUtils.isEmpty(faceBase)) { //文件上传的地址 String upPath = ResourceUtils.getURL("classpath:").getPath()+"static\\photo"; // 给上传的图片命名,格式:用户名_当前时间的毫秒数.jpg String fileName = userName+"_"+System.currentTimeMillis() + ".jpg"; String path=upPath+"\\"+fileName;//上传文件的最终路径 File file = new File(path); //初始化百度云的AipFace AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY); //向数据库里插入一条用户数据 User user = new User(); user.setUsername(userName); user.setPhoto(path); User exitUser = userService.findUserByName(userName); if(exitUser != null) { //表示用户名重复了 return "2"; } userService.addUser(user);//添加到数据库 //图片上传到服务器中 //将客户端传递过来的相片转换为字节 byte[] bytes= Base64.getDecoder().decode(faceBase); //创建输出流 OutputStream out=new FileOutputStream(path); out.write(bytes);//上传图片 //创建w调用接口所需参数 // 传入可选参数调用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("quality_control", "LOW"); options.put("liveness_control", "LOW"); // String image = Base64Util.encode(bytes); String imageType = "BASE64";//相片格式 String groupId = "1001"; //用户组id //调用接口进行人脸注册,向百度云人脸库插入一张人脸 JSONObject res = client.addUser(faceBase,imageType,groupId,userName,options); System.out.println(res.toString(2));//控制台观察注册反馈结果 } return "1"; } //用户登录 @RequestMapping(value = "login",method = RequestMethod.POST) @ResponseBody public String login(String faceBase, HttpServletRequest request) { String faceData = faceBase; //进行人像数据对比 AipFace client = new AipFace(APP_ID,API_KEY,SECRET_KEY); JSONObject user = verifyUser(faceData,client);//进行人像对比获取结果 System.out.println(user.toString()); Double score = (Double) user.get("score");//获得准确率百分数 if(score>95) { //准确率超过 95%认为成功 return "{\"num\":\"1\",\"username\":\""+user.getString("user_id")+"\"}"; }else { return "{\"num\":\"2\"}"; } } //人脸对比 public JSONObject verifyUser(String imgBash64, AipFace client) { // 传入可选参数调用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("quality_control", "LOW"); options.put("liveness_control", "LOW"); JSONObject res = client.search(imgBash64, "BASE64", "1001", options); System.out.println("这是res========="+res); // System.out.println(res.toString(2));//观察比较结果 // System.out.println(res.getJSONObject("result")); // System.out.println(res.getJSONObject("result").getJSONArray("user_list")); JSONObject user = (JSONObject) res.getJSONObject("result").getJSONArray("user_list").get(0); System.out.println("百度返回的user对象:"+user.toString());//观察返回的识别到的用户数据 // System.out.println("username:"+user.getString("user_id")); return user; //识别到的用户信息 } }
更多推荐
已为社区贡献2条内容
所有评论(0)