SpringBoot集成百度人脸识别

人脸识别(Face Recognition)基于图像或视频中的人脸检测、分析和比对技术,提供对您已获授权前提下的私有数据的人脸检测与属性分析、人脸对比、人脸搜索、活体检测等能力。灵活应用于金融、泛安防、零售等行业场景,满足身份核验、人脸考勤、闸机通行等业务需求

1、概述

地址:https://ai.baidu.com/tech/face

image-20201030184449505

2、账号申请

账号登录注册

百度云AI支持百度账号登录,按需注册即可

image-20201030184841463

创建应用

按需创建应用

image-20230620094348630

image-20230620094443292

3、抽取模板工具

AipFaceProperties

@Data
@ConfigurationProperties("facedemo")
public class AipFaceProperties {
    
    
    private String appId;
    private String apiKey;
    private String secretKey;

    @Bean
    public AipFace aipFace() {
    
    
        AipFace client = new AipFace(appId, apiKey, secretKey);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }
}

AipFaceTemplate

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;

public class AipFaceTemplate {
    
    

    @Autowired
    private AipFace client;

    /**
     * 检测图片中是否包含人脸
     *  true:包含
     *  false:不包含
     */
    public boolean detect(String imageUrl) {
    
    
        // 调用接口
        String imageType = "URL";

        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        // 人脸检测
        JSONObject res = client.detect(imageUrl, imageType, options);
        System.out.println(res.toString(2));

        Integer error_code = (Integer) res.get("error_code");

        return error_code == 0;
    }
}

application.yml

编写百度AI的配置信息

扫描二维码关注公众号,回复: 15523470 查看本文章
facedemo:
    appId: 24021388
    apiKey: ZnMTwoETXnu4OPIGwGAO2H4G
    secretKey: D4jXShyinv5q26bUS78xRKgNLnB9IfZh

4、测试

编写单元测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class FaceTest {
    
    


    @Autowired
    private AipFaceTemplate template;

    @Test
    public void detectFace() {
    
    
        String image = "图片路径";
        boolean detect = template.detect(image); //是否存在人脸
    }
}

猜你喜欢

转载自blog.csdn.net/qq_51808107/article/details/131300678
今日推荐