1、整体文件
拿到demo需要更改的地方
- 图片路径小技巧,就不用傻傻地自己写了
注册百度云人脸识别Api流程,目前新人而言,实名之后百度云有免费的时候,直接拿去玩
运行可能会遇到的错误
-
成功截图
-
失败截图,(图片不合格导致失败)
图片报错,说left eye is occlusion,意思就是左眼阻塞,其实就是图片不合格,不能作为图片识别的几个重要特征
参考文档
源码
- FaceTest文件
package cn.itcast.baidu;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
public class FaceTest {
@Test
public void testFaceRegister() throws Exception{
//1.创建java代码和百度云交互的client对象
AipFace client=new AipFace("26144507","aajSQhrf543534tkMuB7Nsn5","5UguEfcCoTzzaf43242XGjspQA71r");
//传入参数
HashMap<String, String> options = new HashMap<String, String>();
options.put("quality_control", "NORMAL"); //图片质量 NONE LOW NORMAL HIGH
options.put("liveness_control", "LOW"); //活体检测
//构造图片,自己电脑上图片的绝对路径
String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\001.jpg";
//上传的图片 两种格式 : url格式 Base64字符串格式
byte[] bytes = Files.readAllBytes(Paths.get(path));
String encode = Base64Util.encode(bytes);
//4.调用api方法完成人脸注册
/**
*参数一:(图片的url或者图片的Base64字符串),
* 参数二:图片形式(URL,BASE64)
* 参数三:组ID(固定字符串)
* 参数四:用户ID
* 参数五:hashMap中的基本参数配置
*/
String imageType = "BASE64";
String groupId = "itcast";
String userId = "1000";
JSONObject res = client.addUser(encode, imageType, groupId, userId, options);
System.out.println(res.toString(2));
}
}
- pom 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>face-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.16.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<!-- <scope>test</scope>-->
</dependency>
</dependencies>
</project>
- 增更查代码(增加了其他的方法,更全面些)
package cn.itcast.baidu;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
public class FaceTest {
private AipFace client;
@Before
public void init(){
client=new AipFace("26134907","aajSQhrf8756543MuB7Nsn5","5UguEfcCoTz3122fiXGjspQA71r");
}
/**
* @describe: 人脸识别注册
* @param:
* @author: lijunyun
* @date 2022/5/2 16:40
**/
@Test
public void testFaceRegister() throws Exception{
//传入参数
HashMap<String, String> options = new HashMap<String, String>();
options.put("quality_control", "NORMAL"); //图片质量 NONE LOW NORMAL HIGH
options.put("liveness_control", "LOW"); //活体检测
//构造图片
String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\002.jpg";
//上传的图片 两种格式 : url格式 Base64字符串格式
byte[] bytes = Files.readAllBytes(Paths.get(path));
String encode = Base64Util.encode(bytes);
//4.调用api方法完成人脸注册
/**
*参数一:(图片的url或者图片的Base64字符串),
* 参数二:图片形式(URL,BASE64)
* 参数三:组ID(固定字符串)
* 参数四:用户ID
* 参数五:hashMap中的基本参数配置
*/
String imageType = "BASE64";
String groupId = "itcast";
String userId = "1000";
JSONObject res = client.addUser(encode, imageType, groupId, userId, options);
System.out.println(res.toString(2));
}
/**
* @describe:人脸检测:判断图片中是否具有面具信息
* @param:
* @author: lijunyun
* @date 2022/5/2 16:41
**/
@Test
public void testFaceCheck() throws Exception{
//构造图片
String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\006.jpg";
//上传的图片 两种格式 : url格式 Base64字符串格式
byte[] bytes = Files.readAllBytes(Paths.get(path));
String encode = Base64Util.encode(bytes);
//4.调用api进行人脸检测
/**
*参数一:(图片的url或者图片的Base64字符串),
* 参数二:图片形式(URL,BASE64)
* 参数三:hashMap中的基本参数配置(null,表示使用默认配置)
*/
String imageType = "BASE64";
JSONObject res = client.detect(encode,imageType,null);
System.out.println(res.toString(2));
}
/**
* @describe: 人脸搜索,找到相似度最高的图片(或几个)
* 说明:返回值(数据,只需要一条,相似度最高的数据),score:相似度评分(80分以上可以认为是同一个人)
* @param:
* @author: lijunyun
* @date 2022/5/2 17:00
**/
@Test
public void testFaceSearch() throws Exception{
//构造图片
String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\001.jpg";
//上传的图片 两种格式 : url格式 Base64字符串格式
byte[] bytes = Files.readAllBytes(Paths.get(path));
String encode = Base64Util.encode(bytes);
//4.调用api进行人脸检测
/**
*参数一:(图片的url或者图片的Base64字符串),
* 参数二:图片形式(URL,BASE64)
* 参数三:groupId为组ID
* 参数四:hashMap中的基本参数配置(null,表示使用默认配置)
*/
String imageType = "BASE64";
String groupId = "itcast";
JSONObject res = client.search(encode,imageType,groupId,null);
System.out.println(res.toString(2));
}
/**
* @describe: 人脸更新:更新人脸库中的照片
* @param:
* @author: lijunyun
* @date 2022/5/2 17:08
**/
@Test
public void testFaceUpdate() throws Exception{
//传入参数
HashMap<String, String> options = new HashMap<String, String>();
options.put("quality_control", "NORMAL"); //图片质量 NONE LOW NORMAL HIGH
options.put("liveness_control", "LOW"); //活体检测
//构造图片
String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\003.jpg";
//上传的图片 两种格式 : url格式 Base64字符串格式
byte[] bytes = Files.readAllBytes(Paths.get(path));
String encode = Base64Util.encode(bytes);
//4.调用api方法完成人脸注册
/**
*参数一:(图片的url或者图片的Base64字符串),
* 参数二:图片形式(URL,BASE64)
* 参数三:组ID(固定字符串)
* 参数四:用户ID
* 参数五:hashMap中的基本参数配置
*/
String imageType = "BASE64";
String groupId = "itcast";
String userId = "1000";
JSONObject res = client.updateUser(encode, imageType, groupId, userId, options);
System.out.println(res.toString(2));
}
}