人脸识别demo

This commit is contained in:
dengwenjie
2025-02-21 20:20:07 +08:00
parent 28f55e85cc
commit 4f1564c04b
15 changed files with 12 additions and 77 deletions

View File

@@ -5,7 +5,6 @@ import java.io.InputStream;
/**
* 人脸识别算法
* @author dwj
* @date 2025/2/20
*/
public abstract class AbstractFaceAlgorithm implements FaceAlgorithm{
@Override

View File

@@ -10,7 +10,6 @@ import java.io.InputStream;
/**
* 人脸识别算法
* @author dwj
* @date 2025/2/19
*/
public interface FaceAlgorithm {

View File

@@ -9,7 +9,6 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* 人脸算法工厂
* @author dwj
* @date 2025/2/19
*/
public class FaceAlgorithmFactory {

View File

@@ -2,7 +2,6 @@ package cn.smartjavaai.face;
/**
* @author dwj
* @date 2025/2/20
*/
public class FaceConfig {

View File

@@ -8,7 +8,6 @@ import java.util.List;
/**
* 人脸检测结果
* @author dwj
* @date 2025/2/19
*/
public class FaceDetectedResult {

View File

@@ -3,7 +3,6 @@ package cn.smartjavaai.face;
/**
* 模型配置
* @author dwj
* @date 2025/2/19
*/
public class ModelConfig {

View File

@@ -1,62 +0,0 @@
package cn.smartjavaai.face;
import cn.smartjavaai.common.entity.Rectangle;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
/**
* @author dwj
* @date 2025/2/19
*/
public class Test {
public static void main(String[] args) throws Exception {
// 初始化配置
ModelConfig config = new ModelConfig();
config.setAlgorithmName("retinaface");
//config.setAlgorithmName("ultralightfastgenericface");
//config.setModelPath("/Users/wenjie/Documents/idea_workplace/SmartJavaAI/models/retinaface.pt");
config.setConfidenceThreshold(0.85f);
config.setMaxFaceCount(100);
config.setNmsThresh(0.45f);
FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceAlgorithm(config);
File imageFile = new File("/Users/wenjie/Downloads/djl-master/examples/src/test/resources/largest_selfie.jpg");
FaceDetectedResult result = currentAlgorithm.detect("/Users/wenjie/Downloads/djl-master/examples/src/test/resources/largest_selfie.jpg");
//FaceDetectedResult result = currentAlgorithm.detect(new FileInputStream(imageFile));
// 1. 加载原始图片
File input = new File("/Users/wenjie/Downloads/djl-master/examples/src/test/resources/largest_selfie.jpg");
BufferedImage image = ImageIO.read(input);
// 2. 创建绘图上下文
Graphics2D graphics = image.createGraphics();
// 3. 配置绘制参数
graphics.setColor(Color.RED); // 边框颜色
graphics.setStroke(new BasicStroke(2)); // 线宽2像素
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
for(Rectangle rectangle : result.getRectangles()){
// 4. 绘制矩形左上角坐标x=50,y=100宽200高150
graphics.drawRect(rectangle.getPointList().get(0).getX(),
rectangle.getPointList().get(0).getY(), rectangle.getWidth(), rectangle.getHeight());
}
// 5. 释放资源并保存
graphics.dispose();
ImageIO.write(image, "jpg", new File("output.jpg"));
System.out.println("111111");
}
}

View File

@@ -31,7 +31,6 @@ import java.util.stream.StreamSupport;
/**
* RetinaFace实现
* @author dwj
* @date 2025/2/19
*/
public class RetinaFace extends AbstractFaceAlgorithm {

View File

@@ -22,7 +22,6 @@ import java.util.stream.Collectors;
/**
* @author dwj
* @date 2025/2/19
*/
public class UltraLightFastGenericFace extends AbstractFaceAlgorithm {