百度人脸识别
人脸识别(Face Recognition)基于图像或视频中的人脸检测、分析和比对技术,提供对您已获授权前提下的私有数据的人脸检测与属性分析、人脸对比、人脸搜索、活体检测等能力。灵活应用于金融、泛安防、零售等行业场景,满足身份核验、人脸考勤、闸机通行等业务需求
一、官网示例代码
| public class Sample { |
| |
| public static final String APP_ID = "你的 App ID"; |
| public static final String API_KEY = "你的 Api Key"; |
| public static final String SECRET_KEY = "你的 Secret Key"; |
| |
| public static void main(String[] args) { |
| |
| AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY); |
| |
| |
| client.setConnectionTimeoutInMillis(2000); |
| client.setSocketTimeoutInMillis(60000); |
| |
| |
| client.setHttpProxy("proxy_host", proxy_port); |
| client.setSocketProxy("proxy_host", proxy_port); |
| |
| |
| String image = "取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串"; |
| String imageType = "BASE64"; |
| |
| |
| 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(image, imageType, options); |
| System.out.println(res.toString(2)); |
| |
| } |
| } |
二、项目代码
2.1先测试,FaceApiTest
检测一张图片中是否包含有人脸
| package com.itheima.test; |
| |
| import com.baidu.aip.face.AipFace; |
| import org.json.JSONObject; |
| |
| import java.util.HashMap; |
| |
| public class FaceApiTest { |
| |
| |
| public static final String APP_ID = "27915129"; |
| public static final String API_KEY = "iYbOuyOqi9BsVw4SbAEQifgR"; |
| public static final String SECRET_KEY = "Z5kpiL2Fnb1ujQIlcPLjZ9fg0dcAWjaO"; |
| |
| public static void main(String[] args) { |
| |
| AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY); |
| |
| |
| client.setConnectionTimeoutInMillis(2000); |
| client.setSocketTimeoutInMillis(60000); |
| |
| |
| String image = "https://project-of-tanhua.oss-cn-hangzhou.aliyuncs.com/2022/10/14/f716e89e-1364-435a-b3f3-4c9b42d563a2.jpg"; |
| 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(image, imageType, options); |
| System.out.println(res.toString(2)); |
| |
| } |
| } |
| |
三、抽取模板工具
3.1在tanhua-app-server的applicatin.yaml配置信息
| tanhua: |
| faceapi: |
| appId: 27915129 |
| apiKey: iYbOuyOqi9BsVw4SbAEQifgR |
| secret: Z5kpiL2Fnb1ujQIlcPLjZ9fg0dcAWjaO |
3.2FaceApiProperties配置类,读取配置
| package com.tanhua.autoconfig.properties; |
| |
| import com.baidu.aip.face.AipFace; |
| import lombok.Data; |
| import org.springframework.boot.context.properties.ConfigurationProperties; |
| import org.springframework.context.annotation.Bean; |
| |
| @Data |
| @ConfigurationProperties("tanhua.faceapi") |
| public class FaceApiProperties { |
| |
| |
| private String appId; |
| private String apiKey; |
| private String secret; |
| |
| |
| @Bean |
| public AipFace client(){ |
| AipFace client = new AipFace(appId, apiKey, secret); |
| |
| client.setConnectionTimeoutInMillis(2000); |
| client.setSocketTimeoutInMillis(60000); |
| return client; |
| } |
| } |
| |
3.3FaceApiTemplate
| package com.tanhua.autoconfig.template; |
| |
| import com.baidu.aip.face.AipFace; |
| import org.json.JSONObject; |
| import org.springframework.beans.factory.annotation.Autowired; |
| |
| import java.util.HashMap; |
| |
| public class FaceApiTemplate { |
| |
| @Autowired |
| private AipFace client; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public boolean checkFace(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; |
| } |
| |
| } |
| |
3.4测试
| package com.itheima.test; |
| |
| import com.tanhua.autoconfig.template.FaceApiTemplate; |
| import com.tanhua.server.AppServerApplication; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.boot.test.context.SpringBootTest; |
| import org.springframework.test.context.junit4.SpringRunner; |
| |
| @RunWith(SpringRunner.class) |
| @SpringBootTest(classes = AppServerApplication.class) |
| public class FaceApiTest2 { |
| |
| |
| @Autowired |
| private FaceApiTemplate faceApiTemplate; |
| |
| @Test |
| public void test (){ |
| |
| String imageUrl = "https://project-of-tanhua.oss-cn-hangzhou.aliyuncs.com/2022/10/14/f716e89e-1364-435a-b3f3-4c9b42d563a2.jpg"; |
| boolean result = faceApiTemplate.checkFace(imageUrl); |
| System.out.println("图片是否含有人脸?"+result); |
| } |
| |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?