当前位置: 首页 > news >正文

百度AI人脸检测与对比

1.注册账号

打开网站 https://ai.baidu.com/ ,注册百度账号并登录

2.创建应用

 

 

 

 

 3.技术文档

https://ai.baidu.com/ai-doc/FACE/yk37c1u4t

4.Spring Boot简单集成测试

pom.xml 配置:
<!--百度AI-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.9.0</version>
</dependency>
人脸识别 java
下面的 APPID/AK/SK 改成你的,找 2 张人像图片就可简单测试。
package com.hz.test;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.hz.utils.FileUtil;
import org.apache.tomcat.util.codec.binary.Base64;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @Author: LNH
* @Date: 2024-11-12-21:27
* @Description:
*/
public class FaceTest {// 设置APPID/AK/SKprivate static String APP_ID = "116216384";private static String API_KEY = "VYnavoYcxNRGnahgk50WaTSl";private static String SECRET_KEY = "qHhZBJIZSDfcrn830R05BkPyHoRLSqZ9";public static void main(String[] args) {try {// 初始化一个AipFaceAipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);// 可选:设置网络连接参数client.setConnectionTimeoutInMillis(2000);client.setSocketTimeoutInMillis(60000);// 读本地图片byte[] bytes =FileUtil.readFileByBytes("C:\\Users\\Administrator\\Desktop\\a\\周杰伦1.jpg");byte[] bytes2 =FileUtil.readFileByBytes("C:\\Users\\Administrator\\Desktop\\a\\周杰伦1.jpg");// 将字节转base64String image1 = Base64.encodeBase64String(bytes);String image2 = Base64.encodeBase64String(bytes2);// 人脸对比MatchRequest req1 = new MatchRequest(image1, "BASE64");MatchRequest req2 = new MatchRequest(image2, "BASE64");ArrayList<MatchRequest> requests = new ArrayList<>();requests.add(req1);requests.add(req2);JSONObject res = client.match(requests);System.out.println("人脸对比结果:");System.out.println(res.toString(2));//调用处理函数handleMatchResult(res);// 人脸检测// 传入可选参数调用接口HashMap<String, String> options = new HashMap<>();options.put("face_field", "age");options.put("max_face_num", "2");options.put("face_type", "LIVE");options.put("liveness_control", "LOW");JSONObject res2 = client.detect(image1, "BASE64", options);System.out.println("人脸检测信息:");System.out.println(res2.toString(2));//调用处理函数handleDetectResult(res2);} catch (Exception e) {e.printStackTrace();System.err.println("文件读取失败: " + e.getMessage());}
}private static void handleMatchResult(JSONObject result) {int errorCode = result.getInt("error_code");if (errorCode == 0) { // 成功的情况double score = result.getJSONObject("result").getDouble("score");String isSamePerson = score > 80 ? "==>可能是同一个人" : "==>不是同一个人"; // 根据实际需求调整阈值System.out.println("------人脸对比结果: ------\n" + isSamePerson + "\n相似度:" + score + "\n");} else {System.out.println("人脸对比失败: " + result.getString("error_msg"));}}private static void handleDetectResult(JSONObject result) {int errorCode = result.getInt("error_code");if (errorCode == 0) { // 成功的情况int faceNum = result.getJSONObject("result").getInt("face_num");if (faceNum > 0) {JSONObject faceInfo =result.getJSONObject("result").getJSONArray("face_list").getJSONObject(0);int age = faceInfo.getInt("age");System.out.println("\n------人脸检测结果: ------ \n检测到人脸,年龄约
为 " + age + " 岁");} else {System.out.println("人脸检测结果: 没有检测到人脸");}} else {System.out.println("人脸检测失败: " + result.getString("error_msg"));}}}
测试所需的工具类
package com.hz.utils;
import java.io.*;
/**
* @Author: LNH
* @Date: 2024-11-12-21:32
* @Description:
*/
/**
* 文件读取工具类
*/
public class FileUtil{/*** 读取文件内容,作为字符串返回*/public static String readFileAsString(String filePath) throws IOException {File file = new File(filePath);if (!file.exists()) {throw new FileNotFoundException(filePath);}if (file.length() > 1024 * 1024 * 1024) {throw new IOException("File is too large");}StringBuilder sb = new StringBuilder((int) (file.length()));// 创建字节输入流FileInputStream fis = new FileInputStream(filePath);// 创建一个长度为10240的Bufferbyte[] bbuf = new byte[10240];// 用于保存实际读取的字节数int hasRead = 0;while ( (hasRead = fis.read(bbuf)) > 0 ) {sb.append(new String(bbuf, 0, hasRead));}fis.close();return sb.toString();}
/**
* 根据文件路径读取byte[] 数组
*/
public static byte[] readFileByBytes(String filePath) throws IOException {File file = new File(filePath);if (!file.exists()) {throw new FileNotFoundException(filePath);} else {ByteArrayOutputStream bos = new ByteArrayOutputStream((int)file.length());BufferedInputStream in = null;try {in = new BufferedInputStream(new FileInputStream(file));short bufSize = 1024;byte[] buffer = new byte[bufSize];int len1;while (-1 != (len1 = in.read(buffer, 0, bufSize))) {bos.write(buffer, 0, len1);}byte[] var7 = bos.toByteArray();return var7;} finally {try {if (in != null) {in.close();}} catch (IOException var14) {var14.printStackTrace();}bos.close();}}}
}
人脸检测 相关请求参数、返回参数解释
百度官网解释很清楚

5.Spring Boot接口测试

改写成 Controller 接口,加入前端页面测试
FaceController.java 文件
package com.hz.controller;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import org.apache.tomcat.util.codec.binary.Base64;
import org.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;/**
* @Author: LNH
* @Date: 2024-11-13-9:45
* @Description:
*/
@RestController
public class FaceController {// 设置APPID/AK/SKprivate static final String APP_ID = "116216384";private static final String API_KEY = "VYnavoYcxNRGnahgk50WaTSl";private static final String SECRET_KEY = "qHhZBJIZSDfcrn830R05BkPyHoRLSqZ9";//人脸对比@PostMapping(value = "/match-faces", produces = "application/json")public ResponseEntity<String> matchFaces(@RequestParam("image1")MultipartFile image1,@RequestParam("image2")MultipartFile image2)throws IOException {// 初始化一个AipFaceAipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);// 读本地图片byte[] bytes1 = image1.getBytes();byte[] bytes2 = image2.getBytes();// 将字节转base64String imageStr1 = Base64.encodeBase64String(bytes1);String imageStr2 = Base64.encodeBase64String(bytes2);// 人脸对比MatchRequest req1 = new MatchRequest(imageStr1, "BASE64");MatchRequest req2 = new MatchRequest(imageStr2, "BASE64");ArrayList<MatchRequest> requests = new ArrayList<>();requests.add(req1);requests.add(req2);JSONObject res = client.match(requests);JSONObject result = handleMatchResult(res);// 返回 JSON 字符串return ResponseEntity.ok(result.toString());
}
//人脸检测
@PostMapping("/detect-face")
public ResponseEntity<String> detectFace(@RequestParam("image")MultipartFile image)throwsIOException {// 初始化一个AipFaceAipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);// 读本地图片byte[] bytes = image.getBytes();// 将字节转base64String imageStr = Base64.encodeBase64String(bytes);// 人脸检测// 传入可选参数调用接口HashMap<String, String> options = new HashMap<>();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(imageStr, "BASE64", options);JSONObject result = handleDetectResult(res);// 返回 JSON 字符串return ResponseEntity.ok(result.toString());
}//对比判断是否为同一个人private JSONObject handleMatchResult(JSONObject result) {int errorCode = result.getInt("error_code");if (errorCode == 0) { // 成功的情况double score = result.getJSONObject("result").getDouble("score");String isSamePerson = score > 80 ? "可能是同一个人" : "不是同一个人"; //
根据实际需求调整阈值result.put("isSamePerson", isSamePerson);result.put("score", score);} else {result.put("error_msg", "人脸对比失败: " +result.getString("error_msg"));}return result;}//人脸检测信息private JSONObject handleDetectResult(JSONObject result) {int errorCode = result.getInt("error_code");if (errorCode == 0) { // 成功的情况int faceNum = result.getJSONObject("result").getInt("face_num");if (faceNum > 0) {JSONObject faceInfo =result.getJSONObject("result").getJSONArray("face_list").getJSONObject(0);int age = faceInfo.getInt("age");} else {result.put("error_msg", "人脸检测结果: 没有检测到人脸");}} else {result.put("error_msg", "人脸检测失败: " +result.getString("error_msg"));}return result;}
}
result.put("age", age);
index.html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>人脸对比与检测</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
h1 {
text-align: center;
color: #333;
}
form {
max-width: 600px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
input[type="file"] {
display: block;
margin-bottom: 10px;
}
button {
display: block;
width: 100%;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #0056b3;
}
#matchResult, #detectResult {
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
font-size: 18px;
color: #333;
}
#matchResult p, #detectResult p {
margin: 0;
}
.success {
color: green;
}
.error {
color: red;
}
</style>
</head>
<body>
<h1>人脸对比与检测</h1>
<!-- 人脸对比表单 -->
<h2>人脸对比</h2>
<form id="matchForm">
<input type="file" name="image1" accept="image/*" required>
<input type="file" name="image2" accept="image/*" required>
<button type="submit">对比</button>
</form>
<div id="matchResult"></div>
<!-- 人脸检测表单 -->
<h2>人脸检测</h2>
<form id="detectForm">
<input type="file" name="image" accept="image/*" required>
<button type="submit">检测</button>
</form>
<div id="detectResult"></div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(document).ready(function() {
$('#matchForm').on('submit', function(event) {
event.preventDefault();
var formData = new FormData(this);
$.ajax({
url: '/match-faces',
type: 'POST',
data: formData,
contentType: false,
processData: false,
headers: {
'Accept': 'application/json'
},
success: function(response) {
console.log("Match response:", response);
if (response.isSamePerson && response.score !== undefined) {
$('#matchResult').html('<p class="success">人脸对比结果: '
+ response.isSamePerson + ' (相似度:' + response.score + ')</p>');
} else {
$('#matchResult').html('<p class="error">人脸对比失败: ' +
response.error_msg + '</p>');
}
},
error: function(error) {
console.error("Match error:", error);
$('#matchResult').html('<p class="error">人脸对比失败: ' +
error.responseJSON.error_msg + '</p>');
}
});
});
$('#detectForm').on('submit', function(event) {
event.preventDefault();
var formData = new FormData(this);
$.ajax({
url: '/detect-face',
type: 'POST',
data: formData,
contentType: false,
processData: false,
headers: {
'Accept': 'application/json'
},
success: function(response) {
console.log("Detect response:", response);
if (response.age !== undefined) {
$('#detectResult').html('<p class="success">人脸检测结果:
检测到人脸,年龄约为 ' + response.age + ' 岁</p>');
} else {
$('#detectResult').html('<p class="error">人脸检测结果: ' +
response.error_msg + '</p>');
}
},
error: function(error) {
console.error("Detect error:", error);
$('#detectResult').html('<p class="error">人脸检测失败: ' +
error.responseJSON.error_msg + '</p>');
}
});
});
});
</script>
</body>
</html>

http://www.lryc.cn/news/487885.html

相关文章:

  • 贴代码框架PasteForm特性介绍之outer,outers,object,objects,outerdisplay
  • sql数据库-权限控制-DCL
  • 【计组笔记】目录
  • 深度学习中的Pixel Shuffle和Pixel Unshuffle:图像超分辨率的秘密武器
  • AntFlow 0.11.0版发布,增加springboot starter模块,一款设计上借鉴钉钉工作流的免费企业级审批流平台
  • golang操作mysql基础驱动github.com/go-sql-driver/mysql使用
  • 正则表达式完全指南,总结全面通俗易懂
  • 运维面试题.云计算面试题之三ELK
  • C# DataTable使用Linq查询详解
  • 【企业级分布式系统】ELK优化
  • 51单片机基础05 定时器
  • tdengine学习笔记实战-jdbc连接tdengine数据库
  • vue3项目执行npm install下载依赖报错问题排查方法
  • 【vue】项目迭代部署后 自动清除浏览器缓存
  • Leetcode(滑动窗口习题思路总结,持续更新。。。)
  • 【UNIAPP】uniapp版图片压缩工具
  • PaddlePaddle 开源产业级文档印章识别PaddleX-Pipeline “seal_recognition”模型 开箱即用篇(一)
  • Vue3 + Vite 项目引入 Typescript
  • 微信小程序实战篇-分类页面制作
  • 第三十七章 如何清理docker 日志
  • 二刷代码随想录第七天
  • 1.tree of thought (使用LangChain解决4x4数独问题)
  • 网络基础(4)IP协议
  • 124. 二叉树中的最大路径和【 力扣(LeetCode) 】
  • echarts:简单实现默认显示两柱子折线,点击按钮后显示新的柱子
  • 视频里的音频怎么提取出来成单独文件?音频提取照着这些方法做
  • Excel——宏教程(精简版)
  • C++中的std::tuple和std::pair
  • 引力搜索算法
  • 【时间之外】IT人求职和创业应知【35】-RTE三进宫