Java调用百度人脸对比接口进行人脸(图片)对比

前三步和人脸检测代码一样

在我上一篇博客地址链接:https://blog.csdn.net/weixin_45736927/article/details/104696428

第四步 Token和工具类准备完毕,写人脸对比代码

2张对比图片
在这里插入图片描述在这里插入图片描述

FaceMatch类
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FaceMatch{
	/**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String match(String imgPath1, String imgPath2) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
        try {
            // 【本地文件1地址】
            byte[] bytes1 = FileUtil.readFileByBytes(imgPath1);
            // 【本地文件2地址】
            byte[] bytes2 = FileUtil.readFileByBytes(imgPath2);
            String image1 = Base64Util.encode(bytes1);
            String image2 = Base64Util.encode(bytes2);
 
            List<Map<String, Object>> images = new ArrayList<>();
 
            Map<String, Object> map1 = new HashMap<>();
            //请求参数详情,看百度人脸对比开发文档
            //https://ai.baidu.com/ai-doc/FACE/Lk37c1tpf
            map1.put("image", image1);
            map1.put("image_type", "BASE64");
            map1.put("face_type", "LIVE");			
            map1.put("quality_control", "LOW");
            map1.put("liveness_control", "NORMAL");
 
            Map<String, Object> map2 = new HashMap<>();
            map2.put("image", image2);
            map2.put("image_type", "BASE64");
            map2.put("face_type", "LIVE");
            map2.put("quality_control", "LOW");
            map2.put("liveness_control", "NORMAL");
 
            images.add(map1);
            images.add(map2);
 
            String param = GsonUtils.toJson(images);
 
            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
            // 【调用鉴权接口获取的token】
            String accessToken = FaceUtils.getAuth();
 
            String result = HttpUtil.post(url, accessToken, "application/json", param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
Controller层(自己的api接口)
@Log("人脸对比")
	@AuthIgnore
	@PostMapping("getFaceDB")
	public R getFaceDB() throws Exception{
		//String filePath = new File("").getAbsolutePath();
		//System.out.println(filePath);
		String imgPath1="D:\\img\\111.jpg";		//本地路径
		String imgPath2="D:\\img\\222.jpg";		////本地路径
		String result = FaceMatch.match(imgPath1, imgPath2);
		System.out.println(result);
		String judge = "不是同一人";
		 try {
	         double score = new JSONObject(result).getJSONObject("result").getDouble("score");
			 System.out.println("相似得分为:" + score);
//	            String judge = "不是同一人";
	            // 阈值为80,高于80分判断为同一人
	            if(score >= 80){
	                judge = "同一人";
	            }
	 
	            System.out.println("判断为:" + judge);
	        } catch (JSONException e) {
	            e.printStackTrace();
	        }
		return R.ok(judge);
	}

第五步.调用接口

在这里插入图片描述
在这里插入图片描述

发布了30 篇原创文章 · 获赞 36 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_45736927/article/details/104713188