1、集成车牌识别模型,支持车牌检测与识别

2、新增 Milvus 身份验证支持
3、目标检测功能升级:可指定类别及topk
4、支持自定义线程池线程数量
This commit is contained in:
dengwenjie
2025-07-28 12:04:02 +08:00
parent 1bd74d1bb8
commit 1d45bc597d
117 changed files with 3490 additions and 437 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.20</smartjavaai.version>
<smartjavaai.version>1.0.22</smartjavaai.version>
<!--如果打包运行需要替换成你的main-->
<exec.mainClass>smartai.examples.face.facedet.FaceDetDemo</exec.mainClass>
@@ -95,6 +95,13 @@
<dependency>
<groupId>cn.smartjavaai</groupId>
<artifactId>smartjavaai-face</artifactId>
<version>1.0.22</version>
</dependency>
<dependency>
<groupId>cn.smartjavaai</groupId>
<artifactId>smartjavaai-all</artifactId>
<version>1.0.22</version>
</dependency>

View File

@@ -0,0 +1,25 @@
package smartai.examples.face;
import cn.smartjavaai.common.utils.VideoUtils;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
/**
* 视频预处理
* @author dwj
* @date 2025/7/17
*/
public class VideoDemo {
public static void main(String[] args) {
try {
VideoUtils.rotateVideo("/Users/wenjie/Downloads/girl.mp4", "/Users/wenjie/Downloads/girl_rotate.mp4", 180,"mp4", avcodec.AV_CODEC_ID_H264);
} catch (FFmpegFrameRecorder.Exception e) {
throw new RuntimeException(e);
} catch (FFmpegFrameGrabber.Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -57,7 +57,8 @@ public class FaceAttributeDetDemo {
*/
@Test
public void testFaceAttributeDetect(){
try (FaceAttributeModel faceAttributeModel = getFaceAttributeModel()){
try {
FaceAttributeModel faceAttributeModel = getFaceAttributeModel();
DetectionResponse detectionResponse = faceAttributeModel.detect("src/main/resources/iu_1.jpg");
//绘制并导出人脸属性图片,小人脸仅有人脸框
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/iu_1.jpg").toAbsolutePath().toString()));
@@ -73,7 +74,8 @@ public class FaceAttributeDetDemo {
*/
@Test
public void testFaceAttributeDetect2(){
try (FaceAttributeModel faceAttributeModel = getFaceAttributeModel()){
try {
FaceAttributeModel faceAttributeModel = getFaceAttributeModel();
FaceAttribute faceAttribute = faceAttributeModel.detectTopFace("src/main/resources/iu_1.jpg");
log.info("人脸属性检测结果:{}", JSONObject.toJSONString(faceAttribute));
} catch (Exception e) {
@@ -86,7 +88,8 @@ public class FaceAttributeDetDemo {
*/
@Test
public void testFaceAttributeDetect3(){
try (FaceAttributeModel faceAttributeModel = getFaceAttributeModel()){
try {
FaceAttributeModel faceAttributeModel = getFaceAttributeModel();
FaceAttribute faceAttribute = faceAttributeModel.detectTopFace("src/main/resources/iu_1.jpg");
log.info("人脸属性检测结果:{}", JSONObject.toJSONString(faceAttribute));
} catch (Exception e) {
@@ -103,8 +106,9 @@ public class FaceAttributeDetDemo {
*/
@Test
public void testFaceAttributeDetect4(){
try (FaceDetModel faceDetModel = getFaceDetModel();
FaceAttributeModel faceAttributeModel = getFaceAttributeModel()){
try {
FaceDetModel faceDetModel = getFaceDetModel();
FaceAttributeModel faceAttributeModel = getFaceAttributeModel();
//人脸检测
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/iu_1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> detectionResponse = faceDetModel.detect(image);

View File

@@ -88,7 +88,8 @@ public class ExpressionRecDemo {
*/
@Test
public void testExpressionDetect() {
try (ExpressionModel model = getExpressionModel()){
try {
ExpressionModel model = getExpressionModel();
R<ExpressionResult> result = model.detectTopFace("src/main/resources/emotion/happy.png");
if(result.isSuccess()){
log.info("识别结果:{}", JSONObject.toJSONString(result.getData().getExpression().getDescription()));
@@ -106,7 +107,8 @@ public class ExpressionRecDemo {
*/
@Test
public void testExpressionDetect2() {
try (ExpressionModel model = getExpressionModel()){
try {
ExpressionModel model = getExpressionModel();
R<DetectionResponse> result = model.detect("src/main/resources/emotion/happy.png");
if(result.isSuccess()){
//log.info("识别结果:{}", JSONObject.toJSONString(result.getData()));
@@ -128,8 +130,9 @@ public class ExpressionRecDemo {
*/
@Test
public void testExpressionDetect3() {
try (FaceDetModel faceDetModel = getFaceDetModel();
ExpressionModel model = getExpressionModel()){
try {
FaceDetModel faceDetModel = getFaceDetModel();
ExpressionModel model = getExpressionModel();
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString()));
R<DetectionResponse> detResult = faceDetModel.detect(image);
if(detResult.isSuccess()){
@@ -156,8 +159,9 @@ public class ExpressionRecDemo {
*/
@Test
public void testExpressionDetect4() {
try (FaceDetModel faceDetModel = getFaceDetModel();
ExpressionModel model = getExpressionModel()){
try {
FaceDetModel faceDetModel = getFaceDetModel();
ExpressionModel model = getExpressionModel();
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString()));
R<DetectionResponse> detResult = faceDetModel.detect(image);
if(detResult.isSuccess()){
@@ -182,7 +186,8 @@ public class ExpressionRecDemo {
*/
@Test
public void testExpressionDetectAndDraw(){
try (ExpressionModel model = getExpressionModel()){
try {
ExpressionModel model = getExpressionModel();
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/surprise.png").toAbsolutePath().toString()));
R<DetectionResponse> result = model.detect(image);
if(result.isSuccess()){
@@ -206,7 +211,8 @@ public class ExpressionRecDemo {
*/
@Test
public void testExpressionDetectCamera(){
try (ExpressionModel expressionModel = getExpressionModel()){
try {
ExpressionModel expressionModel = getExpressionModel();
OpenCV.loadShared();
VideoCapture capture = new VideoCapture(0);
if (!capture.isOpened()) {

View File

@@ -82,7 +82,8 @@ public class FaceDetDemo {
*/
@Test
public void testFaceDetect(){
try (FaceDetModel faceModel = FaceDetModelFactory.getInstance().getModel()) {
try {
FaceDetModel faceModel = FaceDetModelFactory.getInstance().getModel();
R<DetectionResponse> detectedResult = faceModel.detect(imgPath);
if(detectedResult.isSuccess()){
log.info("人脸检测结果:{}", JSONObject.toJSONString(detectedResult.getData()));
@@ -100,7 +101,8 @@ public class FaceDetDemo {
*/
@Test
public void testFaceDetectCustomConfig(){
try (FaceDetModel faceModel = getFaceDetModel()){
try {
FaceDetModel faceModel = getFaceDetModel();
R<DetectionResponse> detectedResult = faceModel.detect(imgPath);
if(detectedResult.isSuccess()){
log.info("人脸检测结果:{}", JSONObject.toJSONString(detectedResult.getData()));
@@ -118,7 +120,8 @@ public class FaceDetDemo {
*/
@Test
public void testFaceDetectAndDraw(){
try (FaceDetModel faceModel = getFaceDetModel()){
try {
FaceDetModel faceModel = getFaceDetModel();
faceModel.detectAndDraw("src/main/resources/largest_selfie.jpg","output/largest_selfie_detected.png");
} catch (Exception e) {
throw new RuntimeException(e);
@@ -131,7 +134,8 @@ public class FaceDetDemo {
*/
@Test
public void testFaceDetectAndDraw2(){
try (FaceDetModel faceModel = getFaceDetModel()){
try {
FaceDetModel faceModel = getFaceDetModel();
BufferedImage image = null;
String imagePath = "src/main/resources/largest_selfie.jpg";
image = ImageIO.read(new File(Paths.get(imagePath).toAbsolutePath().toString()));
@@ -154,11 +158,12 @@ public class FaceDetDemo {
*/
@Test
public void testDetectFaceOffine(){
FaceDetConfig config = new FaceDetConfig();
config.setModelEnum(FaceDetModelEnum.RETINA_FACE);//人脸模型
//模型路径,不同模型下载路径请参看文档
config.setModelPath("/Users/xxx/Documents/develop/face_model/retinaface.pt");
try (FaceDetModel faceModel = FaceDetModelFactory.getInstance().getModel(config)) {
try {
FaceDetConfig config = new FaceDetConfig();
config.setModelEnum(FaceDetModelEnum.RETINA_FACE);//人脸模型
//模型路径,不同模型下载路径请参看文档
config.setModelPath("/Users/xxx/Documents/develop/face_model/retinaface.pt");
FaceDetModel faceModel = FaceDetModelFactory.getInstance().getModel(config);
R<DetectionResponse> detectedResult = faceModel.detect(imgPath);
if(detectedResult.isSuccess()){
log.info("人脸检测结果:{}", JSONObject.toJSONString(detectedResult.getData()));
@@ -175,10 +180,11 @@ public class FaceDetDemo {
*/
@Test
public void testDetectFaceGPU(){
FaceDetConfig config = new FaceDetConfig();
config.setModelEnum(FaceDetModelEnum.RETINA_FACE);//人脸模型
config.setDevice(DeviceEnum.GPU);
try (FaceDetModel faceModel = FaceDetModelFactory.getInstance().getModel(config)) {
try {
FaceDetConfig config = new FaceDetConfig();
config.setModelEnum(FaceDetModelEnum.RETINA_FACE);//人脸模型
config.setDevice(DeviceEnum.GPU);
FaceDetModel faceModel = FaceDetModelFactory.getInstance().getModel(config);
R<DetectionResponse> detectedResult = faceModel.detect(imgPath);
if(detectedResult.isSuccess()){
log.info("人脸检测结果:{}", JSONObject.toJSONString(detectedResult.getData()));
@@ -196,7 +202,8 @@ public class FaceDetDemo {
*/
@Test
public void testFaceDetectSeetaface6(){
try (FaceDetModel faceModel = getSeetaface6DetModel()){
try {
FaceDetModel faceModel = getSeetaface6DetModel();
R<DetectionResponse> detectedResult = faceModel.detect(imgPath);
if(detectedResult.isSuccess()){
log.info("人脸检测结果:{}", JSONObject.toJSONString(detectedResult.getData()));
@@ -215,7 +222,8 @@ public class FaceDetDemo {
*/
@Test
public void testDetectCamera(){
try (FaceDetModel faceModel = getFaceDetModel()){
try {
FaceDetModel faceModel = getFaceDetModel();
OpenCV.loadShared();
VideoCapture capture = new VideoCapture(0);
if (!capture.isOpened()) {

View File

@@ -95,6 +95,8 @@ public class FaceRecDemo {
MilvusConfig vectorDBConfig = new MilvusConfig();
vectorDBConfig.setHost("127.0.0.1");
vectorDBConfig.setPort(19530);
//vectorDBConfig.setUsername("root");
//vectorDBConfig.setPassword("Milvus");
//vectorDBConfig.setCollectionName("face5");
//ID策略自动生成
vectorDBConfig.setIdStrategy(IdStrategy.AUTO);
@@ -135,7 +137,8 @@ public class FaceRecDemo {
*/
@Test
public void testExtractFeatures(){
try (FaceRecModel faceRecModel = getFaceRecModel()){
try {
FaceRecModel faceRecModel = getFaceRecModel();
//提取图片中所有人脸特征
R<DetectionResponse> faceResult = faceRecModel.extractFeatures("src/main/resources/iu_1.jpg");
if(faceResult.isSuccess()){
@@ -158,7 +161,8 @@ public class FaceRecDemo {
*/
@Test
public void featureComparison(){
try (FaceRecModel faceRecModel = getFaceRecModel()){
try {
FaceRecModel faceRecModel = getFaceRecModel();
//基于图像直接比对人脸特征
R<Float> similarResult = faceRecModel.featureComparison("src/main/resources/iu_1.jpg","src/main/resources/iu_2.jpg");
if(similarResult.isSuccess()){
@@ -183,7 +187,8 @@ public class FaceRecDemo {
*/
@Test
public void featureComparison2(){
try (FaceRecModel faceRecModel = getFaceRecModel()){
try {
FaceRecModel faceRecModel = getFaceRecModel();
//特征提取(提取分数最高人脸特征),适用于单人脸场景
R<float[]> featureResult1 = faceRecModel.extractTopFaceFeature("src/main/resources/iu_1.jpg");
if(featureResult1.isSuccess()){
@@ -220,7 +225,8 @@ public class FaceRecDemo {
*/
@Test
public void searchFace(){
try (FaceRecModel faceRecModel = getFaceRecModelWithDbConfig()){
try {
FaceRecModel faceRecModel = getFaceRecModelWithDbConfig();
//等待加载人脸库结束
while (!faceRecModel.isLoadFaceCompleted()){
Thread.sleep(100);
@@ -296,7 +302,8 @@ public class FaceRecDemo {
*/
@Test
public void searchFace2(){
try (FaceRecModel faceRecModel = getFaceRecModelWithSQLiteConfig()){
try {
FaceRecModel faceRecModel = getFaceRecModelWithSQLiteConfig();
//等待加载人脸库结束
while (!faceRecModel.isLoadFaceCompleted()){
Thread.sleep(100);
@@ -367,7 +374,8 @@ public class FaceRecDemo {
@Test
public void getFaceInfo(){
//使用ID获取人脸信息
try (FaceRecModel faceRecModel = getFaceRecModelWithSQLiteConfig()){
try {
FaceRecModel faceRecModel = getFaceRecModelWithSQLiteConfig();
//等待加载人脸库结束
while (!faceRecModel.isLoadFaceCompleted()){
Thread.sleep(100);
@@ -390,7 +398,8 @@ public class FaceRecDemo {
@Test
public void listFaces(){
//使用ID获取人脸信息
try (FaceRecModel faceRecModel = getFaceRecModelWithDbConfig()){
try {
FaceRecModel faceRecModel = getFaceRecModelWithDbConfig();
//等待加载人脸库结束
while (!faceRecModel.isLoadFaceCompleted()){
Thread.sleep(100);

View File

@@ -133,7 +133,8 @@ public class LivenessDetDemo {
*/
@Test
public void testLivenessDetect(){
try (LivenessDetModel livenessDetModel = getLivenessDetModel()){
try {
LivenessDetModel livenessDetModel = getLivenessDetModel();
R<DetectionResponse> response = livenessDetModel.detect("src/main/resources/liveness/1.jpg");
if(response.isSuccess()){
for (DetectionInfo detectionInfo : response.getData().getDetectionInfoList()){
@@ -152,7 +153,8 @@ public class LivenessDetDemo {
*/
@Test
public void testLivenessDetectAndDraw(){
try (LivenessDetModel livenessDetModel = getLivenessDetModel()){
try {
LivenessDetModel livenessDetModel = getLivenessDetModel();
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/liveness/1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> response = livenessDetModel.detect(image);
if(response.isSuccess()){
@@ -175,7 +177,8 @@ public class LivenessDetDemo {
*/
@Test
public void testLivenessDetect2(){
try (LivenessDetModel livenessDetModel = getLivenessDetModel()){
try {
LivenessDetModel livenessDetModel = getLivenessDetModel();
//指定文件夹路径
File dir = new File("face-example/src/main/resources/liveness");
File[] files = dir.listFiles();
@@ -198,8 +201,9 @@ public class LivenessDetDemo {
*/
@Test
public void testLivenessDetect3(){
try (FaceDetModel faceDetectModel = getFaceDetModel();
LivenessDetModel livenessDetModel = getLivenessDetModel()){
try {
FaceDetModel faceDetectModel = getFaceDetModel();
LivenessDetModel livenessDetModel = getLivenessDetModel();
// 将图片路径转换为 BufferedImage
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/liveness/1.jpg").toAbsolutePath().toString()));
//人脸检测
@@ -230,8 +234,9 @@ public class LivenessDetDemo {
*/
@Test
public void testLivenessDetect4(){
try (FaceDetModel faceDetModel = getFaceDetModel();
LivenessDetModel livenessDetModel = getMiniVisionLivenessDetModel()){
try {
FaceDetModel faceDetModel = getFaceDetModel();
LivenessDetModel livenessDetModel = getMiniVisionLivenessDetModel();
// 将图片路径转换为 BufferedImage
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/liveness/1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> detResult = faceDetModel.detect(image);
@@ -259,7 +264,8 @@ public class LivenessDetDemo {
*/
@Test
public void testLivenessDetectVideo(){
try (LivenessDetModel livenessDetModel = getLivenessDetModel()){
try {
LivenessDetModel livenessDetModel = getLivenessDetModel();
//视频路径
R<LivenessResult> livenessStatus = livenessDetModel.detectVideo("video.mp4");
if (livenessStatus.isSuccess()){
@@ -278,7 +284,8 @@ public class LivenessDetDemo {
*/
@Test
public void testLivenessDetectCamera(){
try (LivenessDetModel livenessDetModel = getLivenessDetModel()){
try {
LivenessDetModel livenessDetModel = getLivenessDetModel();
OpenCV.loadShared();
VideoCapture capture = new VideoCapture(0);
if (!capture.isOpened()) {

View File

@@ -78,8 +78,9 @@ public class FaceQualityDetDemo {
*/
@Test
public void evaluateBrightness(){
try (FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel()){
try {
FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel();
//人脸检测
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/iu_1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> detectionResponse = faceDetModel.detect(image);
@@ -110,8 +111,9 @@ public class FaceQualityDetDemo {
*/
@Test
public void evaluateCompleteness(){
try (FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel()){
try {
FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel();
//人脸检测
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/iu_1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> detectionResponse = faceDetModel.detect(image);
@@ -142,8 +144,9 @@ public class FaceQualityDetDemo {
*/
@Test
public void evaluateClarity(){
try (FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel()){
try {
FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel();
//人脸检测
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/iu_1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> detectionResponse = faceDetModel.detect(image);
@@ -174,8 +177,9 @@ public class FaceQualityDetDemo {
*/
@Test
public void evaluatePose(){
try (FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel()){
try {
FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel();
//人脸检测
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/iu_1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> detectionResponse = faceDetModel.detect(image);
@@ -207,8 +211,9 @@ public class FaceQualityDetDemo {
*/
@Test
public void evaluateResolution(){
try (FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel()){
try {
FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel();
//人脸检测
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/iu_1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> detectionResponse = faceDetModel.detect(image);
@@ -240,8 +245,9 @@ public class FaceQualityDetDemo {
*/
@Test
public void evaluateAll(){
try (FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel()){
try {
FaceQualityModel faceQualityModel = getFaceQualityModel();
FaceDetModel faceDetModel = getFaceDetModel();
//人脸检测
BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/iu_1.jpg").toAbsolutePath().toString()));
R<DetectionResponse> detectionResponse = faceDetModel.detect(image);

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.20</smartjavaai.version>
<smartjavaai.version>1.0.22</smartjavaai.version>
<!--如果打包运行需要替换成你的main-->
<exec.mainClass>smartai.examples.objectdetection.ObjectDetection</exec.mainClass>

View File

@@ -41,10 +41,8 @@ import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -71,7 +69,8 @@ public class ObjectDetection {
@Test
public void objectDetection(){
//默认cpu
try (DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel()){
try {
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel();
DetectionResponse detectionResponse = detectorModel.detect("src/main/resources/object_detection.jpg");
log.info("目标检测结果:{}", JSONObject.toJSONString(detectionResponse));
} catch (Exception e) {
@@ -84,10 +83,15 @@ public class ObjectDetection {
*/
@Test
public void objectDetection2(){
DetectorModelConfig config = new DetectorModelConfig();
config.setModelEnum(DetectorModelEnum.SSD_300_RESNET50);//检测模型目前支持19种预置模型
config.setDevice(device);
try (DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config)){
try {
DetectorModelConfig config = new DetectorModelConfig();
config.setModelEnum(DetectorModelEnum.SSD_300_RESNET50);//检测模型目前支持19种预置模型
// 指定允许的类别
// config.setAllowedClasses(Arrays.asList("person"));
//指定返回检测数量
config.setTopK(100);
config.setDevice(device);
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config);
DetectionResponse detectionResponse = detectorModel.detect("src/main/resources/dog_bike_car.jpg");
log.info("目标检测结果:{}", JSONObject.toJSONString(detectionResponse));
} catch (Exception e) {
@@ -100,7 +104,8 @@ public class ObjectDetection {
*/
@Test
public void objectDetectionAndDraw(){
try (DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel()){
try {
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel();
detectorModel.detectAndDraw("src/main/resources/object_detection.jpg","output/object_detection_detected.png");
} catch (Exception e) {
e.printStackTrace();
@@ -112,7 +117,8 @@ public class ObjectDetection {
*/
@Test
public void objectDetectionAndDraw2(){
try (DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel()){
try {
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel();
String imagePath = "src/main/resources/object_detection.jpg";
BufferedImage image = ImageIO.read(new File(Paths.get(imagePath).toAbsolutePath().toString()));
//可以根据后续业务场景使用detectedImage
@@ -131,15 +137,16 @@ public class ObjectDetection {
*/
@Test
public void objectDetectionWithOfficialModel(){
DetectorModelConfig config = new DetectorModelConfig();
config.setThreshold(0.3f);
//也支持YoloV8YOLOV8_OFFICIAL 模型可以从文档中提供的地址下载
config.setModelEnum(DetectorModelEnum.YOLOV12_OFFICIAL);//检测模型目前支持19种模型
// 指定模型路径,需要更改为自己的模型路径
config.setModelPath("/Users/xxx/Documents/yolov12n.onnx");
config.setDevice(device);
//一定要将yolo官方的类别文件synset.txt文档中下载放在模型同目录下否则报错
try (DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config)){
try {
DetectorModelConfig config = new DetectorModelConfig();
config.setThreshold(0.3f);
//也支持YoloV8YOLOV8_OFFICIAL 模型可以从文档中提供的地址下载
config.setModelEnum(DetectorModelEnum.YOLOV12_OFFICIAL);//检测模型目前支持19种模型
// 指定模型路径,需要更改为自己的模型路径
config.setModelPath("/Users/xxx/Documents/yolov12n.onnx");
config.setDevice(device);
//一定要将yolo官方的类别文件synset.txt文档中下载放在模型同目录下否则报错
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config);
DetectionResponse detect = detectorModel.detect("src/main/resources/dog_bike_car.jpg");
log.info("目标检测结果:{}", JSONObject.toJSONString(detect));
} catch (Exception e) {
@@ -152,16 +159,21 @@ public class ObjectDetection {
*/
@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");
config.putCustomParam("width", 640);//resize 宽
config.putCustomParam("height", 640);// resize
config.putCustomParam("nmsThreshold", 0.5f);
config.setDevice(device);
try (DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config)){
try {
DetectorModelConfig config = new DetectorModelConfig();
//也支持YoloV8YOLOV8_CUSTOM 模型需要自己训练,训练教程可以查看文档
config.setModelEnum(DetectorModelEnum.YOLOV12_CUSTOM);//自定义YOLOV12模型
// 指定模型路径,需要更改为自己的模型路径
config.setModelPath("/Users/xxx/Documents/develop/fire_model/best.onnx");
config.putCustomParam("width", 640);//resize
config.putCustomParam("height", 640);// resize 高
config.putCustomParam("nmsThreshold", 0.5f);
// 指定允许的类别
// config.setAllowedClasses(Arrays.asList("person"));
//指定返回检测数量
config.setTopK(100);
config.setDevice(device);
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel(config);
DetectionResponse detect = detectorModel.detect("src/main/resources/dog_bike_car.jpg");
log.info("目标检测结果:{}", JSONObject.toJSONString(detect));
} catch (Exception e) {
@@ -176,7 +188,8 @@ public class ObjectDetection {
*/
@Test
public void testDetectCamera(){
try (DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel()){
try {
DetectorModel detectorModel = ObjectDetectionModelFactory.getInstance().getModel();
OpenCV.loadShared();
VideoCapture capture = new VideoCapture(0);
if (!capture.isOpened()) {

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.20</smartjavaai.version>
<smartjavaai.version>1.0.22</smartjavaai.version>
<!--如果打包运行需要替换成你的main-->
<exec.mainClass>smartai.examples.ocr.common.OcrRecognizeDemo</exec.mainClass>

View File

@@ -11,7 +11,6 @@ import cn.smartjavaai.ocr.entity.OcrInfo;
import cn.smartjavaai.ocr.enums.CommonDetModelEnum;
import cn.smartjavaai.ocr.factory.OcrModelFactory;
import cn.smartjavaai.ocr.model.common.detect.OcrCommonDetModel;
import cn.smartjavaai.ocr.opencv.OcrOpenCVUtils;
import cn.smartjavaai.ocr.utils.OcrUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
@@ -60,7 +59,8 @@ public class OcrDetectionDemo {
*/
@Test
public void detect(){
try (OcrCommonDetModel model = getDetectionModel()){
try {
OcrCommonDetModel model = getDetectionModel();
List<OcrBox> boxes = model.detect("src/main/resources/ocr_1.jpg");
log.info("OCR检测结果{}", JSONObject.toJSONString(boxes));
} catch (Exception e) {
@@ -77,7 +77,8 @@ public class OcrDetectionDemo {
*/
@Test
public void detectAndDraw(){
try (OcrCommonDetModel model = getDetectionModel()){
try {
OcrCommonDetModel model = getDetectionModel();
model.detectAndDraw("src/main/resources/ocr_1.jpg", "output/ocr_1_detected.jpg");
} catch (Exception e) {
e.printStackTrace();
@@ -94,7 +95,8 @@ public class OcrDetectionDemo {
*/
@Test
public void batchDetect(){
try (OcrCommonDetModel model = getDetectionModel()){
try {
OcrCommonDetModel model = getDetectionModel();
//批量检测要求图片宽高一致
String folderPath = "/Users/xxx/Downloads/testing33";
//读取文件夹中所有图片

View File

@@ -67,7 +67,8 @@ public class OcrDirectionDetDemo {
*/
@Test
public void detect(){
try (OcrDirectionModel directionModel = getDirectionModel()){
try {
OcrDirectionModel directionModel = getDirectionModel();
List<OcrItem> itemList = directionModel.detect("src/main/resources/ocr_1.jpg");
log.info("OCR方向检测结果1{}", JSONObject.toJSONString(itemList));
} catch (Exception e) {
@@ -84,7 +85,8 @@ public class OcrDirectionDetDemo {
*/
@Test
public void detectAndDraw(){
try (OcrDirectionModel directionModel = getDirectionModel()){
try {
OcrDirectionModel directionModel = getDirectionModel();
directionModel.detectAndDraw("src/main/resources/ocr_3.jpg", "output/ocr_3_detected.png");
} catch (Exception e) {
e.printStackTrace();

View File

@@ -106,7 +106,8 @@ public class OcrRecognizeDemo {
*/
@Test
public void recognize(){
try (OcrCommonRecModel recModel = getRecModel()){
try {
OcrCommonRecModel recModel = getRecModel();
//不带方向矫正,分行返回文本
OcrRecOptions options = new OcrRecOptions(false, true);
OcrInfo ocrInfo = recModel.recognize("src/main/resources/ocr_2.jpg",options);
@@ -127,7 +128,8 @@ public class OcrRecognizeDemo {
*/
@Test
public void recognizeHandWriting(){
try (OcrCommonRecModel recModel = getRecModel()){
try {
OcrCommonRecModel recModel = getRecModel();
OcrInfo ocrInfo = recModel.recognize("src/main/resources/handwriting_1.jpg",new OcrRecOptions());
log.info("OCR识别结果{}", JSONObject.toJSONString(ocrInfo));
} catch (Exception e) {
@@ -146,7 +148,8 @@ public class OcrRecognizeDemo {
*/
@Test
public void recognize2(){
try (OcrCommonRecModel recModel = getRecModelWithDirection()){
try {
OcrCommonRecModel recModel = getRecModelWithDirection();
//带方向矫正,分行返回文本
OcrRecOptions options = new OcrRecOptions(true, true);
OcrInfo ocrInfo = recModel.recognize("src/main/resources/ocr_3.jpg",options);
@@ -168,7 +171,8 @@ public class OcrRecognizeDemo {
*/
@Test
public void recognizeAndDraw(){
try (OcrCommonRecModel recModel = getRecModelWithDirection()){
try {
OcrCommonRecModel recModel = getRecModelWithDirection();
int fontSize = 18;
recModel.recognizeAndDraw("src/main/resources/general_ocr_002.png", "output/ocr_4_recognized.jpg", fontSize, new OcrRecOptions());
} catch (Exception e) {
@@ -184,7 +188,8 @@ public class OcrRecognizeDemo {
*/
@Test
public void batchRecognize(){
try (OcrCommonRecModel recModel = getRecModelWithDirection()){
try {
OcrCommonRecModel recModel = getRecModelWithDirection();
//批量检测要求图片宽高一致
String folderPath = "/Users/xxx/Downloads/testing33";
//读取文件夹中所有图片

View File

@@ -0,0 +1,81 @@
package smartai.examples.ocr.plate;
import ai.djl.util.JsonUtils;
import cn.smartjavaai.common.entity.R;
import cn.smartjavaai.common.enums.DeviceEnum;
import cn.smartjavaai.common.utils.ImageUtils;
import cn.smartjavaai.ocr.config.PlateDetModelConfig;
import cn.smartjavaai.ocr.config.PlateRecModelConfig;
import cn.smartjavaai.ocr.entity.PlateInfo;
import cn.smartjavaai.ocr.enums.PlateDetModelEnum;
import cn.smartjavaai.ocr.enums.PlateRecModelEnum;
import cn.smartjavaai.ocr.factory.PlateModelFactory;
import cn.smartjavaai.ocr.model.plate.PlateDetModel;
import cn.smartjavaai.ocr.model.plate.PlateRecModel;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.io.File;
import java.util.List;
/**
* @author dwj
*/
@Slf4j
public class PlateRecDemo {
//设备类型
public static DeviceEnum device = DeviceEnum.CPU;
/**
* 获取车牌检测模型
* @return
*/
public PlateDetModel getPlateDetModel() {
PlateDetModelConfig config = new PlateDetModelConfig();
config.setModelEnum(PlateDetModelEnum.YOLOV5);
config.setModelPath("/Users/wenjie/Downloads/modelscope-2b3747db53adb48370c6edaccac56eb1ca0da1b5/onnx_export/model.onnx");
// config.setModelPath("/Users/wenjie/Documents/develop/model/plate/yolov8s.onnx");
config.setModelPath("/Users/wenjie/Downloads/数据集/车牌/onnx/plate_detect.onnx");
config.setPredictorPoolSize(3);
config.setDevice(device);
return PlateModelFactory.getInstance().getDetModel(config);
}
/**
* 获取车牌识别模型
* @return
*/
public PlateRecModel getPlateRecModel() {
PlateRecModelConfig recModelConfig = new PlateRecModelConfig();
recModelConfig.setModelEnum(PlateRecModelEnum.PLATE_REC_CRNN);
recModelConfig.setModelPath("/Users/wenjie/Downloads/数据集/车牌/onnx/plate_rec_color.onnx");
recModelConfig.setPlateDetModel(getPlateDetModel());
return PlateModelFactory.getInstance().getRecModel(recModelConfig);
}
@Test
public void testDetect() {
PlateRecModel plateRecModel = getPlateRecModel();
R<List<PlateInfo>> result = plateRecModel.recognize("src/main/resources/plate/Quicker_20220930_180856.png");
if(result.isSuccess()){
log.info("车牌识别结果:{}", JsonUtils.toJson(result.getData()));
}else{
log.error("车牌识别失败:{}", result.getMessage());
}
}
@Test
public void recognizeAndDraw() {
PlateRecModel plateRecModel = getPlateRecModel();
R<Void> result = plateRecModel.recognizeAndDraw("src/main/resources/plate/single_green.jpg", "output/plate_recognized2.jpg");
if(result.isSuccess()){
log.info("车牌识别成功");
}else{
log.error("车牌识别失败:{}", result.getMessage());
}
}
}

View File

@@ -113,10 +113,11 @@ public class TableRecDemo {
*/
@Test
public void recognize(){
try (TableStructureModel tableStructureModel = getTableStructureModel();
OcrCommonDetModel detModel = getDetectionModel();
OcrCommonRecModel recModel = getRecModel();
OcrDirectionModel directionModel = getDirectionModel()){
try {
TableStructureModel tableStructureModel = getTableStructureModel();
OcrCommonDetModel detModel = getDetectionModel();
OcrCommonRecModel recModel = getRecModel();
OcrDirectionModel directionModel = getDirectionModel();
//创建表格识别器
TableRecognizer tableRecognizer = TableRecognizer.builder()
.withStructureModel(tableStructureModel)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 241 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 328 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 400 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 903 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 932 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 999 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 943 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 KiB

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.20</smartjavaai.version>
<smartjavaai.version>1.0.22</smartjavaai.version>
<!--如果打包运行需要替换成你的main-->
<exec.mainClass>smartai.examples.nlp.translation.TranslationDemo</exec.mainClass>