1、目标检测:支持自己训练的模型推理

2、目标检测:支持yolo12模型
3、支持JDK8使用
4、引入离线依赖库,支持完全离线使用
5、优化FaceNet人脸比对速度
6、支持4通道图片检测
This commit is contained in:
dengwenjie
2025-05-17 11:19:46 +08:00
parent ab669d58e3
commit a7a118c5aa
27 changed files with 401 additions and 237 deletions

View File

@@ -12,7 +12,7 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<smartjavaai.version>1.0.12</smartjavaai.version>
<smartjavaai.version>1.0.13</smartjavaai.version>
<!--如果打包运行需要替换成你的main-->
<exec.mainClass>smartai.examples.face.facerec.RetinaFaceDemo</exec.mainClass>
@@ -104,6 +104,14 @@
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-jni</artifactId>
<version>2.5.1-0.32.0</version>
<scope>runtime</scope>
</dependency>
<!-- windows平台 (保留对应平台的配置,可以减小包大小)-->
<dependency>
<groupId>org.bytedeco</groupId>
@@ -132,6 +140,15 @@
<classifier>${javacv.platform.windows-x86_64}</classifier>
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-native-cpu</artifactId>
<classifier>${djl.platform.windows-x86_64}</classifier>
<version>2.5.1</version>
<scope>runtime</scope>
</dependency>
<!-- linux x86 平台 (保留对应平台的配置,可以减小包大小)-->
<dependency>
@@ -161,6 +178,14 @@
<classifier>${javacv.platform.linux-x86_64}</classifier>
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-native-cpu</artifactId>
<classifier>${djl.platform.linux-x86_64}</classifier>
<version>2.5.1</version>
<scope>runtime</scope>
</dependency>
<!-- macOS M系列 平台 (保留对应平台的配置,可以减小包大小)-->
<dependency>
@@ -190,6 +215,14 @@
<classifier>${javacv.platform.macosx-arm64}</classifier>
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-native-cpu</artifactId>
<classifier>${djl.platform.osx-aarch64}</classifier>
<version>2.5.1</version>
<scope>runtime</scope>
</dependency>
<!-- linux aarch64 平台 (保留对应平台的配置,可以减小包大小)-->
<dependency>
@@ -220,8 +253,13 @@
<classifier>${javacv.platform.linux-arm64}</classifier>
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-native-cpu-precxx11</artifactId>
<classifier>${djl.platform.linux-aarch64}</classifier>
<version>2.5.1</version>
<scope>runtime</scope>
</dependency>

View File

@@ -54,14 +54,17 @@ public class FaceNetDemo {
@Test
public void testExtractFeaturesWithCustomConfig(){
try {
//人脸特征提取模型
FaceModel faceModel = FaceModelFactory.getInstance().getModel(
new FaceModelConfig(FaceModelEnum.FACENET_FEATURE_EXTRACTION));
//人脸模型参数
FaceModelConfig config = new FaceModelConfig();
config.setModelEnum(FaceModelEnum.FACENET_FEATURE_EXTRACTION);
//人脸特征提取参数
FaceExtractConfig extractConfig = new FaceExtractConfig();
//人脸检测模型配置
extractConfig.setDetectModelConfig(new FaceModelConfig(FaceModelEnum.ULTRA_LIGHT_FAST_GENERIC_FACE));
List<float[]> faceResult = faceModel.extractFeatures("src/main/resources/kana1.jpg",extractConfig);
extractConfig.setDetectModel(FaceModelFactory.getInstance().getModel(new FaceModelConfig(FaceModelEnum.ULTRA_LIGHT_FAST_GENERIC_FACE)));
config.setExtractConfig(extractConfig);
//人脸特征提取模型
FaceModel faceModel = FaceModelFactory.getInstance().getModel(config);
List<float[]> faceResult = faceModel.extractFeatures("src/main/resources/kana1.jpg");
log.info("人脸特征提取结果:{}", JSONObject.toJSONString(faceResult));
}catch (Exception e){
e.printStackTrace();
@@ -94,13 +97,16 @@ public class FaceNetDemo {
@Test
public void testExtractTopFaceFeatureWithCustomConfig(){
try {
//人脸特征提取模型
FaceModel faceModel = FaceModelFactory.getInstance().getModel(
new FaceModelConfig(FaceModelEnum.FACENET_FEATURE_EXTRACTION));
//人脸模型参数
FaceModelConfig config = new FaceModelConfig();
config.setModelEnum(FaceModelEnum.FACENET_FEATURE_EXTRACTION);
//人脸特征提取参数
FaceExtractConfig extractConfig = new FaceExtractConfig();
//人脸检测模型配置
extractConfig.setDetectModelConfig(new FaceModelConfig(FaceModelEnum.ULTRA_LIGHT_FAST_GENERIC_FACE));
extractConfig.setDetectModel(FaceModelFactory.getInstance().getModel(new FaceModelConfig(FaceModelEnum.ULTRA_LIGHT_FAST_GENERIC_FACE)));
config.setExtractConfig(extractConfig);
//人脸特征提取模型
FaceModel faceModel = FaceModelFactory.getInstance().getModel(config);
float[] faceResult = faceModel.extractTopFaceFeature("src/main/resources/kana1.jpg");
log.info("人脸特征提取结果:{}", JSONObject.toJSONString(faceResult));
}catch (Exception e){

View File

@@ -13,8 +13,8 @@ import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;
import cn.smartjavaai.common.entity.DetectionResponse;
import cn.smartjavaai.common.enums.DeviceEnum;
import cn.smartjavaai.objectdetection.DetectorModelConfig;
import cn.smartjavaai.objectdetection.DetectorModelEnum;
import cn.smartjavaai.objectdetection.config.DetectorModelConfig;
import cn.smartjavaai.objectdetection.enums.DetectorModelEnum;
import cn.smartjavaai.objectdetection.exception.DetectionException;
import cn.smartjavaai.objectdetection.model.DetectorModel;
import cn.smartjavaai.objectdetection.model.ObjectDetectionModelFactory;
@@ -62,7 +62,7 @@ public class ObjectDetection {
@Test
public void objectDetection2(){
DetectorModelConfig config = new DetectorModelConfig();
config.setModelEnum(DetectorModelEnum.SSD_300_RESNET50);//检测模型目前支持19种模型
config.setModelEnum(DetectorModelEnum.SSD_300_RESNET50);//检测模型目前支持19种预置模型
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config);
DetectionResponse detectionResponse = detectorModel.detect("src/main/resources/dog_bike_car.jpg");
log.info("目标检测结果:{}", JSONObject.toJSONString(detectionResponse));
@@ -110,5 +110,35 @@ public class ObjectDetection {
}
/**
* 使用yolo官方模型检测
*/
@Test
public void objectDetectionWithOfficialModel(){
DetectorModelConfig config = new DetectorModelConfig();
//也支持YoloV8YOLOV8_OFFICIAL 模型可以从文档中提供的地址下载
config.setModelEnum(DetectorModelEnum.YOLOV12_OFFICIAL);//检测模型目前支持19种模型
// 指定模型路径,需要更改为自己的模型路径
config.setModelPath("/Users/xxx/Documents/develop/face_model/yolov12n.onnx");
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config);
//一定要将yolo官方的类别文件synset.txt文档中下载放在模型同目录下否则报错
detectorModel.detectAndDraw("src/main/resources/object_detection.jpg","output/object_detection_detected.png");
}
/**
* 使用自己训练的模型检测
*/
@Test
public void objectDetectionWithCustomModel(){
DetectorModelConfig config = new DetectorModelConfig();
//也支持YoloV8YOLOV8_CUSTOM 模型需要自己训练,训练教程可以查看文档
config.setModelEnum(DetectorModelEnum.YOLOV12_CUSTOM);//自定义YOLOV12模型
// 指定模型路径,需要更改为自己的模型路径
config.setModelPath("/Users/xxx/Documents/develop/fire_model/best.onnx");
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config);
//一定要将类别文件synset.txt 放在模型同目录下,否则报错(具体请参看文档)
detectorModel.detectAndDraw("/Users/xxx/Downloads/test.jpg","output/test_detected.jpg");
}
}