mirror of
https://github.com/geekwenjie/SmartJavaAI.git
synced 2026-09-14 22:18:42 +00:00
集成算法seetaface6
This commit is contained in:
@@ -2,7 +2,9 @@ package smartai.examples.face;
|
||||
|
||||
import cn.smartjavaai.common.entity.Rectangle;
|
||||
import cn.smartjavaai.face.*;
|
||||
import cn.smartjavaai.face.entity.FaceResult;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -22,14 +24,13 @@ import java.nio.file.Paths;
|
||||
/**
|
||||
* @author dwj
|
||||
*/
|
||||
@Slf4j
|
||||
public class FaceDemo {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FaceDemo.class);
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
verifyIDCard();
|
||||
featureComparison();
|
||||
//detectFace2();
|
||||
//verifyIDCard();
|
||||
} catch (Exception e) {
|
||||
@@ -49,18 +50,18 @@ public class FaceDemo {
|
||||
//创建人脸算法
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceAlgorithm();
|
||||
sw.stop();
|
||||
logger.info("创建人脸算法耗时:" + sw.getTime() + "ms");
|
||||
log.info("创建人脸算法耗时:" + sw.getTime() + "ms");
|
||||
sw.reset();
|
||||
sw.start();
|
||||
//使用图片路径检测
|
||||
FaceDetectedResult result = currentAlgorithm.detect("src/main/resources/largest_selfie.jpg");
|
||||
sw.stop();
|
||||
logger.info("人脸检测耗时:" + sw.getTime() + "ms");
|
||||
logger.info("人脸检测结果:{}", JSONObject.toJSONString(result));
|
||||
log.info("人脸检测耗时:" + sw.getTime() + "ms");
|
||||
log.info("人脸检测结果:{}", JSONObject.toJSONString(result));
|
||||
//使用图片流检测
|
||||
File input = new File("src/main/resources/largest_selfie.jpg");
|
||||
//FaceDetectedResult result = currentAlgorithm.detect(new FileInputStream(input));
|
||||
//logger.info("人脸检测结果:{}", JSONObject.toJSONString(result));
|
||||
//log.info("人脸检测结果:{}", JSONObject.toJSONString(result));
|
||||
BufferedImage image = ImageIO.read(input);
|
||||
//创建保存路径
|
||||
Path imagePath = Paths.get("output").resolve("retinaface_detected.jpg");
|
||||
@@ -81,14 +82,14 @@ public class FaceDemo {
|
||||
//创建轻量人脸算法
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createLightFaceAlgorithm();
|
||||
sw.stop();
|
||||
logger.info("创建人脸算法耗时:" + sw.getTime() + "ms");
|
||||
log.info("创建人脸算法耗时:" + sw.getTime() + "ms");
|
||||
sw.reset();
|
||||
sw.start();
|
||||
//使用图片路径检测
|
||||
FaceDetectedResult result = currentAlgorithm.detect("src/main/resources/largest_selfie.jpg");
|
||||
sw.stop();
|
||||
logger.info("人脸检测耗时:" + sw.getTime() + "ms");
|
||||
logger.info("轻量人脸检测结果:{}", JSONObject.toJSONString(result));
|
||||
log.info("人脸检测耗时:" + sw.getTime() + "ms");
|
||||
log.info("轻量人脸检测结果:{}", JSONObject.toJSONString(result));
|
||||
//使用图片流检测
|
||||
//File imageFile = new File("/Users/wenjie/Downloads/djl-master/examples/src/test/resources/largest_selfie.jpg");
|
||||
//FaceDetectedResult result = currentAlgorithm.detect(new FileInputStream(imageFile));
|
||||
@@ -100,39 +101,7 @@ public class FaceDemo {
|
||||
ImageUtils.drawBoundingBoxes(image, result, imagePath.toAbsolutePath().toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 人证核验
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void verifyIDCard() throws Exception {
|
||||
// 创建并启动计时器
|
||||
StopWatch sw = StopWatch.createStarted();
|
||||
//创建脸算法
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceFeatureAlgorithm();
|
||||
sw.stop();
|
||||
logger.info("创建人脸算法耗时:" + sw.getTime() + "ms");
|
||||
sw.reset();
|
||||
sw.start();
|
||||
//提取身份证人脸特征(图片仅供测试)
|
||||
float[] featureIdCard = currentAlgorithm.featureExtraction("src/main/resources/MJ_20250213_155245.png");
|
||||
sw.stop();
|
||||
logger.info("人脸特征提取耗时:" + sw.getTime() + "ms");
|
||||
//提取身份证人脸特征(从图片流获取)
|
||||
//File input = new File("src/main/resources/kana1.jpg");
|
||||
//float[] featureIdCard = currentAlgorithm.featureExtraction(new FileInputStream(input));
|
||||
logger.info("身份证人脸特征:{}", JSONObject.toJSONString(featureIdCard));
|
||||
//提取实时人脸特征(图片仅供测试)
|
||||
float[] realTimeFeature = currentAlgorithm.featureExtraction("src/main/resources/MJ_20250213_155228.png");
|
||||
logger.info("实时人脸特征:{}", JSONObject.toJSONString(realTimeFeature));
|
||||
if(realTimeFeature != null){
|
||||
System.out.println("相似度:" + currentAlgorithm.calculSimilar(featureIdCard, realTimeFeature));
|
||||
if(currentAlgorithm.calculSimilar(featureIdCard, realTimeFeature) > 0.8){
|
||||
logger.info("人脸核验通过");
|
||||
}else{
|
||||
logger.info("人脸核验不通过");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 人脸检测(离线模型)
|
||||
@@ -143,7 +112,7 @@ public class FaceDemo {
|
||||
public static void detectFaceOffine() throws Exception {
|
||||
// 初始化配置
|
||||
ModelConfig config = new ModelConfig();
|
||||
config.setAlgorithmName("retinaface");//人脸算法模型,目前支持:retinaface及ultralightfastgenericface
|
||||
config.setAlgorithmName("retinaface");//人脸算法模型,目前支持:retinaface/ultralightfastgenericface/seetaface6
|
||||
//config.setAlgorithmName("ultralightfastgenericface");//轻量模型
|
||||
config.setConfidenceThreshold(FaceConfig.DEFAULT_CONFIDENCE_THRESHOLD);//置信度阈值
|
||||
config.setMaxFaceCount(FaceConfig.MAX_FACE_LIMIT);//每张特征图保留的最大候选框数量
|
||||
@@ -158,7 +127,7 @@ public class FaceDemo {
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceAlgorithm(config);
|
||||
//使用图片路径检测
|
||||
FaceDetectedResult result = currentAlgorithm.detect("src/main/resources/largest_selfie.jpg");
|
||||
logger.info("人脸检测结果:{}", JSONObject.toJSONString(result));
|
||||
log.info("人脸检测结果:{}", JSONObject.toJSONString(result));
|
||||
//使用图片流检测
|
||||
File input = new File("src/main/resources/largest_selfie.jpg");
|
||||
//FaceDetectedResult result = currentAlgorithm.detect(new FileInputStream(input));
|
||||
@@ -171,34 +140,113 @@ public class FaceDemo {
|
||||
}
|
||||
|
||||
/**
|
||||
* 人证核验(离线模型)
|
||||
* 人脸比对(1:1)
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void verifyIDCardOffine() throws Exception {
|
||||
// 初始化配置
|
||||
ModelConfig config = new ModelConfig();
|
||||
config.setAlgorithmName("featureExtraction");
|
||||
//模型下载地址:https://resources.djl.ai/test-models/pytorch/face_feature.zip
|
||||
//改为模型存放路径
|
||||
config.setModelPath("/Users/xxx/Documents/develop/face_model/face_feature.pt");
|
||||
//创建脸算法
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceFeatureAlgorithm(config);
|
||||
//提取身份证人脸特征(图片仅供测试)
|
||||
float[] featureIdCard = currentAlgorithm.featureExtraction("src/main/resources/kana1.jpg");
|
||||
//提取身份证人脸特征(从图片流获取)
|
||||
//File input = new File("src/main/resources/kana1.jpg");
|
||||
//float[] featureIdCard = currentAlgorithm.featureExtraction(new FileInputStream(input));
|
||||
logger.info("身份证人脸特征:{}", JSONObject.toJSONString(featureIdCard));
|
||||
//提取实时人脸特征(图片仅供测试)
|
||||
float[] realTimeFeature = currentAlgorithm.featureExtraction("src/main/resources/kana2.jpg");
|
||||
logger.info("实时人脸特征:{}", JSONObject.toJSONString(realTimeFeature));
|
||||
if(realTimeFeature != null){
|
||||
if(currentAlgorithm.calculSimilar(featureIdCard, realTimeFeature) > 0.8){
|
||||
logger.info("人脸核验通过");
|
||||
}else{
|
||||
logger.info("人脸核验不通过");
|
||||
}
|
||||
public static void featureComparison(){
|
||||
try {
|
||||
// 初始化配置
|
||||
ModelConfig config = new ModelConfig();
|
||||
config.setAlgorithmName("seetaface6");//目前支持人脸比对的算法只有:seetaface6
|
||||
//人脸库路径 如果不指定人脸库,无法使用 1:N人脸搜索
|
||||
config.setFaceDbPath("C:/Users/Administrator/Downloads/faces-data.db");
|
||||
//改为模型存放路径
|
||||
config.setModelPath("/opt/sf3.0_models");
|
||||
//创建人脸算法
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceAlgorithm(config);
|
||||
//自动裁剪人脸并比对人脸特征
|
||||
float similar = currentAlgorithm.featureComparison("src/main/resources/kana1.jpg","src/main/resources/kana2.jpg");
|
||||
log.info("相似度:{}", similar);
|
||||
}
|
||||
catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 人脸特征提取及比对
|
||||
*/
|
||||
public static void featureExtractionAndCompare(){
|
||||
try {
|
||||
// 初始化配置
|
||||
ModelConfig config = new ModelConfig();
|
||||
config.setAlgorithmName("seetaface6");
|
||||
//人脸库路径 如果不指定人脸库,无法使用 1:N人脸搜索
|
||||
config.setFaceDbPath("C:/Users/Administrator/Downloads/faces-data.db");
|
||||
//改为模型存放路径
|
||||
config.setModelPath("C:/Users/Administrator/Downloads/sf3.0_models/sf3.0_models");
|
||||
//创建人脸算法
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceAlgorithm(config);
|
||||
//提取图像中最大人脸的特征
|
||||
float[] feature1 = currentAlgorithm.featureExtraction("src/main/resources/kana1.jpg");
|
||||
float[] feature2 = currentAlgorithm.featureExtraction("src/main/resources/kana2.jpg");
|
||||
float similar = currentAlgorithm.calculSimilar(feature1, feature2);
|
||||
log.info("相似度:{}", similar);
|
||||
}
|
||||
catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册人脸及搜索人脸(1:N)
|
||||
*/
|
||||
public static void registerAndSearchFace(){
|
||||
try {
|
||||
// 初始化配置
|
||||
ModelConfig config = new ModelConfig();
|
||||
config.setAlgorithmName("seetaface6");
|
||||
//人脸库路径 如果不指定人脸库,无法使用 1:N人脸搜索
|
||||
config.setFaceDbPath("C:/Users/Administrator/Downloads/faces-data.db");
|
||||
//改为模型存放路径
|
||||
config.setModelPath("C:/Users/Administrator/Downloads/sf3.0_models/sf3.0_models");
|
||||
//创建人脸算法 自动将人脸库加载到内存中
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceAlgorithm(config);
|
||||
//等待人脸库加载完毕
|
||||
Thread.sleep(1000);
|
||||
//注册kana1人脸,参数key建议设置为人名
|
||||
boolean isSuccss = currentAlgorithm.register("kana1","src/main/resources/kana1.jpg");
|
||||
//注册jsy人脸,参数key建议设置为人名
|
||||
isSuccss = currentAlgorithm.register("jsy","src/main/resources/jsy.jpg");
|
||||
FaceResult faceResult = currentAlgorithm.search("src/main/resources/kana2.jpg");
|
||||
if(faceResult != null){
|
||||
log.info("查询到人脸:{}", faceResult.toString());
|
||||
}else{
|
||||
log.info("未查询到人脸");
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除已注册人脸
|
||||
*/
|
||||
public static void removeRegisterFace(){
|
||||
try {
|
||||
// 初始化配置
|
||||
ModelConfig config = new ModelConfig();
|
||||
config.setAlgorithmName("seetaface6");
|
||||
//人脸库路径 如果不指定人脸库,无法使用 1:N人脸搜索
|
||||
config.setFaceDbPath("C:/Users/Administrator/Downloads/faces-data.db");
|
||||
//改为模型存放路径
|
||||
config.setModelPath("C:/Users/Administrator/Downloads/sf3.0_models/sf3.0_models");
|
||||
//创建人脸算法 自动将人脸库加载到内存中
|
||||
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceAlgorithm(config);
|
||||
//等待人脸库加载完毕
|
||||
Thread.sleep(1000);
|
||||
//使用注册人脸时的key值删除,可一次性删除单个
|
||||
long num = currentAlgorithm.removeRegister("kana1");
|
||||
//删除全部人脸
|
||||
//long num = currentAlgorithm.clearFace();
|
||||
log.info("删除成功数量:" + num);
|
||||
}
|
||||
catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ImageUtils {
|
||||
int width = metrics.stringWidth(text) + padding * 2 - stroke / 2;
|
||||
int height = metrics.getHeight() + metrics.getDescent();
|
||||
int ascent = metrics.getAscent();
|
||||
java.awt.Rectangle background = new java.awt.Rectangle(x, y, width, height);
|
||||
Rectangle background = new Rectangle(x, y, width, height);
|
||||
g.fill(background);
|
||||
g.setPaint(Color.WHITE);
|
||||
g.drawString(text, x + padding, y + ascent);
|
||||
|
||||
BIN
examples/src/main/resources/jsy.jpg
Normal file
BIN
examples/src/main/resources/jsy.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
Reference in New Issue
Block a user