diff --git a/examples/src/main/java/smartai/examples/face/FaceDemo.java b/examples/src/main/java/smartai/examples/face/FaceDemo.java index 1c5a96f..acb15e4 100644 --- a/examples/src/main/java/smartai/examples/face/FaceDemo.java +++ b/examples/src/main/java/smartai/examples/face/FaceDemo.java @@ -3,6 +3,7 @@ package smartai.examples.face; import cn.smartjavaai.common.entity.Rectangle; import cn.smartjavaai.face.*; import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang3.time.StopWatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import smartai.examples.utils.ImageUtils; @@ -28,7 +29,7 @@ public class FaceDemo { public static void main(String[] args) { try { - //detectFace(); + //detectFace2(); verifyIDCard(); } catch (Exception e) { e.printStackTrace(); @@ -42,10 +43,18 @@ public class FaceDemo { * 应用场景:如监控摄像头、智能安防系统等需要高精度检测的场合 */ public static void detectFace() throws Exception { + // 创建并启动计时器 + StopWatch sw = StopWatch.createStarted(); //创建人脸算法 FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createFaceAlgorithm(); + sw.stop(); + logger.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)); //使用图片流检测 File input = new File("src/main/resources/largest_selfie.jpg"); @@ -66,10 +75,18 @@ public class FaceDemo { * 应用场景:如监控摄像头、智能安防系统等需要高精度检测的场合 */ public static void detectFace2() throws Exception { + // 创建并启动计时器 + StopWatch sw = StopWatch.createStarted(); //创建轻量人脸算法 FaceAlgorithm currentAlgorithm = FaceAlgorithmFactory.createLightFaceAlgorithm(); + sw.stop(); + logger.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)); //使用图片流检测 //File imageFile = new File("/Users/wenjie/Downloads/djl-master/examples/src/test/resources/largest_selfie.jpg"); @@ -87,10 +104,18 @@ public class FaceDemo { * @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/kana1.jpg"); + sw.stop(); + logger.info("人脸检测耗时:" + sw.getTime() + "ms"); //提取身份证人脸特征(从图片流获取) //File input = new File("src/main/resources/kana1.jpg"); //float[] featureIdCard = currentAlgorithm.featureExtraction(new FileInputStream(input)); diff --git a/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/FeatureExtractionAlgo.java b/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/FeatureExtractionAlgo.java index 934c542..9d8d997 100644 --- a/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/FeatureExtractionAlgo.java +++ b/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/FeatureExtractionAlgo.java @@ -33,6 +33,10 @@ public class FeatureExtractionAlgo extends AbstractFaceAlgorithm { private Criteria faceFeatureCriteria; + private Predictor predictor; + + private ZooModel model; + public static final List mean = Arrays.asList( 127.5f / 255.0f, @@ -62,6 +66,8 @@ public class FeatureExtractionAlgo extends AbstractFaceAlgorithm { .optProgress(new ProgressBar()) .optEngine("PyTorch") // Use PyTorch engine .build(); + model = faceFeatureCriteria.loadModel(); + predictor = model.newPredictor(); } @@ -76,10 +82,7 @@ public class FeatureExtractionAlgo extends AbstractFaceAlgorithm { Path imageFile = Paths.get(imagePath); Image img = ImageFactory.getInstance().fromFile(imageFile); img.getWrappedImage(); - try (ZooModel model = faceFeatureCriteria.loadModel()) { - Predictor predictor = model.newPredictor(); - return predictor.predict(img); - } + return predictor.predict(img); } /** @@ -92,10 +95,7 @@ public class FeatureExtractionAlgo extends AbstractFaceAlgorithm { public float[] featureExtraction(InputStream inputStream) throws Exception { Image img = ImageFactory.getInstance().fromInputStream(inputStream); img.getWrappedImage(); - try (ZooModel model = faceFeatureCriteria.loadModel()) { - Predictor predictor = model.newPredictor(); - return predictor.predict(img); - } + return predictor.predict(img); } /** diff --git a/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/RetinaFace.java b/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/RetinaFace.java index 66bd67f..98b2512 100644 --- a/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/RetinaFace.java +++ b/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/RetinaFace.java @@ -38,6 +38,10 @@ public class RetinaFace extends AbstractFaceAlgorithm { private Criteria faceFeatureCriteria; + private Predictor predictor; + + private ZooModel model; + /** * 特征图层的基础缩放比例 */ @@ -57,7 +61,7 @@ public class RetinaFace extends AbstractFaceAlgorithm { * @param config */ @Override - public void loadModel(ModelConfig config) { + public void loadModel(ModelConfig config) throws ModelNotFoundException, MalformedModelException, IOException { FaceDetectionTranslator translator = new FaceDetectionTranslator(config.getConfidenceThreshold(), config.getNmsThresh(), variance, config.getMaxFaceCount(), scales, steps); criteria = @@ -71,6 +75,8 @@ public class RetinaFace extends AbstractFaceAlgorithm { .optProgress(new ProgressBar()) .optEngine("PyTorch") // Use PyTorch engine .build(); + model = criteria.loadModel(); + predictor = model.newPredictor(); } @@ -85,11 +91,8 @@ public class RetinaFace extends AbstractFaceAlgorithm { public FaceDetectedResult detect(String imagePath) throws Exception{ Path facePath = Paths.get(imagePath); Image img = ImageFactory.getInstance().fromFile(facePath); - try (ZooModel model = criteria.loadModel(); - Predictor predictor = model.newPredictor()) { - DetectedObjects detection = predictor.predict(img); - return convertToFaceDetectedResult(detection,img); - } + DetectedObjects detection = predictor.predict(img); + return convertToFaceDetectedResult(detection,img); } /** @@ -101,13 +104,8 @@ public class RetinaFace extends AbstractFaceAlgorithm { @Override public FaceDetectedResult detect(InputStream imageInputStream) throws Exception { Image img = ImageFactory.getInstance().fromInputStream(imageInputStream); - try (ZooModel model = criteria.loadModel(); - Predictor predictor = model.newPredictor()) { - DetectedObjects detection = predictor.predict(img); - return convertToFaceDetectedResult(detection,img); - /*saveBoundingBoxImage(img, detection); - return detection;*/ - } + DetectedObjects detection = predictor.predict(img); + return convertToFaceDetectedResult(detection,img); } /** diff --git a/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/UltraLightFastGenericFace.java b/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/UltraLightFastGenericFace.java index 992823f..1468654 100644 --- a/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/UltraLightFastGenericFace.java +++ b/smartjavaai-face/src/main/java/cn/smartjavaai/face/algo/UltraLightFastGenericFace.java @@ -1,17 +1,20 @@ package cn.smartjavaai.face.algo; +import ai.djl.MalformedModelException; import ai.djl.inference.Predictor; import ai.djl.modality.cv.Image; import ai.djl.modality.cv.ImageFactory; import ai.djl.modality.cv.output.DetectedObjects; import ai.djl.modality.cv.translator.ImageFeatureExtractorFactory; import ai.djl.repository.zoo.Criteria; +import ai.djl.repository.zoo.ModelNotFoundException; import ai.djl.repository.zoo.ZooModel; import ai.djl.training.util.ProgressBar; import cn.smartjavaai.common.entity.Point; import cn.smartjavaai.common.entity.Rectangle; import cn.smartjavaai.face.*; +import java.io.IOException; import java.io.InputStream; import java.nio.file.Path; import java.nio.file.Paths; @@ -41,6 +44,10 @@ public class UltraLightFastGenericFace extends AbstractFaceAlgorithm { */ private static final double[] variance = {0.1f, 0.2f}; + private Predictor predictor; + + private ZooModel model; + @@ -49,7 +56,7 @@ public class UltraLightFastGenericFace extends AbstractFaceAlgorithm { * @param config */ @Override - public void loadModel(ModelConfig config) { + public void loadModel(ModelConfig config) throws ModelNotFoundException, MalformedModelException, IOException { FaceDetectionTranslator translator = new FaceDetectionTranslator(config.getConfidenceThreshold(), config.getNmsThresh(), variance, config.getMaxFaceCount(), scales, steps); criteria = @@ -60,6 +67,8 @@ public class UltraLightFastGenericFace extends AbstractFaceAlgorithm { .optProgress(new ProgressBar()) .optEngine("PyTorch") // Use PyTorch engine .build(); + model = criteria.loadModel(); + predictor = model.newPredictor(); } /** @@ -72,11 +81,8 @@ public class UltraLightFastGenericFace extends AbstractFaceAlgorithm { public FaceDetectedResult detect(String imagePath) throws Exception{ Path facePath = Paths.get(imagePath); Image img = ImageFactory.getInstance().fromFile(facePath); - try (ZooModel model = criteria.loadModel(); - Predictor predictor = model.newPredictor()) { - DetectedObjects detection = predictor.predict(img); - return convertToFaceDetectedResult(detection,img); - } + DetectedObjects detection = predictor.predict(img); + return convertToFaceDetectedResult(detection,img); } /** @@ -88,13 +94,8 @@ public class UltraLightFastGenericFace extends AbstractFaceAlgorithm { @Override public FaceDetectedResult detect(InputStream imageInputStream) throws Exception { Image img = ImageFactory.getInstance().fromInputStream(imageInputStream); - try (ZooModel model = criteria.loadModel(); - Predictor predictor = model.newPredictor()) { - DetectedObjects detection = predictor.predict(img); - return convertToFaceDetectedResult(detection,img); - /*saveBoundingBoxImage(img, detection); - return detection;*/ - } + DetectedObjects detection = predictor.predict(img); + return convertToFaceDetectedResult(detection,img); } /**