diff --git a/README.md b/README.md index 6fff487..e6dc1d8 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,20 @@ SmartJavaAI是专为JAVA 开发者打造的一个功能丰富、开箱即用的
- + +
+ + + + +
+

人脸表情识别

+ - 7种表情检测
+
+ + +
+
diff --git a/examples/face-example/src/main/java/smartai/examples/face/expression/ExpressionRecDemo.java b/examples/face-example/src/main/java/smartai/examples/face/expression/ExpressionRecDemo.java index 2c74f78..cabe47d 100644 --- a/examples/face-example/src/main/java/smartai/examples/face/expression/ExpressionRecDemo.java +++ b/examples/face-example/src/main/java/smartai/examples/face/expression/ExpressionRecDemo.java @@ -88,12 +88,15 @@ public class ExpressionRecDemo { */ @Test public void testExpressionDetect() { - ExpressionModel model = getExpressionModel(); - R result = model.detectTopFace("src/main/resources/emotion/happy.png"); - if(result.isSuccess()){ - log.info("识别结果:{}", JSONObject.toJSONString(result.getData().getExpression().getDescription())); - }else{ - log.info("识别失败:{}", result.getMessage()); + try (ExpressionModel model = getExpressionModel()){ + R result = model.detectTopFace("src/main/resources/emotion/happy.png"); + if(result.isSuccess()){ + log.info("识别结果:{}", JSONObject.toJSONString(result.getData().getExpression().getDescription())); + }else{ + log.info("识别失败:{}", result.getMessage()); + } + } catch (Exception e) { + throw new RuntimeException(e); } } @@ -103,15 +106,18 @@ public class ExpressionRecDemo { */ @Test public void testExpressionDetect2() { - ExpressionModel model = getExpressionModel(); - R result = model.detect("src/main/resources/emotion/happy.png"); - if(result.isSuccess()){ - //log.info("识别结果:{}", JSONObject.toJSONString(result.getData())); - for (DetectionInfo detectionInfo : result.getData().getDetectionInfoList()) { - log.info("识别结果:{}", JSONObject.toJSONString(detectionInfo.getFaceInfo().getExpressionResult().getExpression().getDescription())); + try (ExpressionModel model = getExpressionModel()){ + R result = model.detect("src/main/resources/emotion/happy.png"); + if(result.isSuccess()){ + //log.info("识别结果:{}", JSONObject.toJSONString(result.getData())); + for (DetectionInfo detectionInfo : result.getData().getDetectionInfoList()) { + log.info("识别结果:{}", JSONObject.toJSONString(detectionInfo.getFaceInfo().getExpressionResult().getExpression().getDescription())); + } + }else{ + log.info("识别失败:{}", result.getMessage()); } - }else{ - log.info("识别失败:{}", result.getMessage()); + } catch (Exception e) { + throw new RuntimeException(e); } } @@ -122,27 +128,24 @@ public class ExpressionRecDemo { */ @Test public void testExpressionDetect3() { - FaceDetModel faceDetModel = getFaceDetModel(); - ExpressionModel model = getExpressionModel(); - // 将图片路径转换为 BufferedImage - BufferedImage image = null; - try { - image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString())); - } catch (IOException e) { - throw new FaceException("无效图片路径", e); - } - R detResult = faceDetModel.detect(image); - if(detResult.isSuccess()){ - R> result = model.detect(image, detResult.getData()); - if(result.isSuccess()){ - result.getData().forEach(expressionResult -> { - log.info("识别结果:{}", JSONObject.toJSONString(expressionResult.getExpression().getDescription())); - }); + try (FaceDetModel faceDetModel = getFaceDetModel(); + ExpressionModel model = getExpressionModel()){ + BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString())); + R detResult = faceDetModel.detect(image); + if(detResult.isSuccess()){ + R> result = model.detect(image, detResult.getData()); + if(result.isSuccess()){ + result.getData().forEach(expressionResult -> { + log.info("识别结果:{}", JSONObject.toJSONString(expressionResult.getExpression().getDescription())); + }); + }else{ + log.info("识别失败:{}", result.getMessage()); + } }else{ - log.info("识别失败:{}", result.getMessage()); + log.info("人脸检测失败:{}", detResult.getMessage()); } - }else{ - log.info("人脸检测失败:{}", detResult.getMessage()); + } catch (Exception e) { + throw new RuntimeException(e); } } @@ -153,27 +156,47 @@ public class ExpressionRecDemo { */ @Test public void testExpressionDetect4() { - FaceDetModel faceDetModel = getFaceDetModel(); - ExpressionModel model = getExpressionModel(); - // 将图片路径转换为 BufferedImage - BufferedImage image = null; - try { - image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString())); - } catch (IOException e) { - throw new FaceException("无效图片路径", e); - } - R detResult = faceDetModel.detect(image); - if(detResult.isSuccess()){ - for (DetectionInfo detectionInfo : detResult.getData().getDetectionInfoList()) { - R result = model.detect(image, detectionInfo.getDetectionRectangle(), detectionInfo.getFaceInfo().getKeyPoints()); - if(result.isSuccess()){ - log.info("识别结果:{}", JSONObject.toJSONString(result.getData().getExpression().getDescription())); - }else{ - log.info("识别失败:{}", result.getMessage()); + try (FaceDetModel faceDetModel = getFaceDetModel(); + ExpressionModel model = getExpressionModel()){ + BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString())); + R detResult = faceDetModel.detect(image); + if(detResult.isSuccess()){ + for (DetectionInfo detectionInfo : detResult.getData().getDetectionInfoList()) { + R result = model.detect(image, detectionInfo.getDetectionRectangle(), detectionInfo.getFaceInfo().getKeyPoints()); + if(result.isSuccess()){ + log.info("识别结果:{}", JSONObject.toJSONString(result.getData().getExpression().getDescription())); + }else{ + log.info("识别失败:{}", result.getMessage()); + } } + }else{ + log.info("人脸检测失败:{}", detResult.getMessage()); } - }else{ - log.info("人脸检测失败:{}", detResult.getMessage()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 图片活体检测并绘制结果 + */ + @Test + public void testExpressionDetectAndDraw(){ + try (ExpressionModel model = getExpressionModel()){ + BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/surprise.png").toAbsolutePath().toString())); + R result = model.detect(image); + if(result.isSuccess()){ + //log.info("识别结果:{}", JSONObject.toJSONString(result.getData())); + for (DetectionInfo detectionInfo : result.getData().getDetectionInfoList()) { + log.info("识别结果:{}", JSONObject.toJSONString(detectionInfo.getFaceInfo().getExpressionResult().getExpression().getDescription())); + ImageUtils.drawImageRectWithText(image, detectionInfo.getDetectionRectangle(), detectionInfo.getFaceInfo().getExpressionResult().getExpression().getDescription(), Color.red); + } + ImageUtils.saveImage(image, "output/detect.jpg"); + }else{ + log.info("识别失败:{}", result.getMessage()); + } + } catch (Exception e) { + throw new RuntimeException(e); } } @@ -182,7 +205,7 @@ public class ExpressionRecDemo { * 注意事项:如果视频比较卡,可以使用轻量的人脸检测模型 */ @Test - public void testLivenessDetectCamera(){ + public void testExpressionDetectCamera(){ try (ExpressionModel expressionModel = getExpressionModel()){ OpenCV.loadShared(); VideoCapture capture = new VideoCapture(0); diff --git a/examples/face-example/src/main/java/smartai/examples/face/liveness/LivenessDetDemo.java b/examples/face-example/src/main/java/smartai/examples/face/liveness/LivenessDetDemo.java index 14c33d3..1c807b4 100644 --- a/examples/face-example/src/main/java/smartai/examples/face/liveness/LivenessDetDemo.java +++ b/examples/face-example/src/main/java/smartai/examples/face/liveness/LivenessDetDemo.java @@ -25,6 +25,7 @@ import cn.smartjavaai.face.factory.LivenessModelFactory; import cn.smartjavaai.face.model.expression.ExpressionModel; import cn.smartjavaai.face.model.facedect.FaceDetModel; import cn.smartjavaai.face.model.liveness.LivenessDetModel; +import cn.smartjavaai.face.utils.FaceUtils; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import nu.pattern.OpenCV; @@ -71,7 +72,7 @@ public class LivenessDetDemo { config.setModelEnum(LivenessModelEnum.IIC_FL_MODEL); config.setDevice(device); //需替换为实际模型存储路径 - config.setModelPath("/Users/xxx/Documents/develop/model/anti/model.onnx"); + config.setModelPath("/Users/xxx/Documents/develop/model/anti/IIC_Fl.onnx"); //人脸活体阈值,可选,默认0.8,超过阈值则认为是真人,低于阈值是非活体 config.setRealityThreshold(LivenessConstant.DEFAULT_REALITY_THRESHOLD); /*视频检测帧数,可选,默认10,输出帧数超过这个number之后,就可以输出识别结果。 @@ -142,6 +143,29 @@ public class LivenessDetDemo { } } + /** + * 图片活体检测并绘制结果 + */ + @Test + public void testLivenessDetectAndDraw(){ + try (LivenessDetModel livenessDetModel = getLivenessDetModel()){ + BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/liveness/1.jpg").toAbsolutePath().toString())); + R response = livenessDetModel.detect(image); + if(response.isSuccess()){ + for (DetectionInfo detectionInfo : response.getData().getDetectionInfoList()){ + log.info("活体检测结果:{}", JSONObject.toJSONString(detectionInfo.getFaceInfo().getLivenessStatus().getStatus().getDescription())); + Color color = detectionInfo.getFaceInfo().getLivenessStatus().getStatus() == LivenessStatus.LIVE ? Color.GREEN : Color.RED; + ImageUtils.drawImageRectWithText(image, detectionInfo.getDetectionRectangle(), detectionInfo.getFaceInfo().getLivenessStatus().getStatus().getDescription(), color); + } + }else{ + log.info("活体检测失败:{}", response.getMessage()); + } + ImageUtils.saveImage(image, "output/detect.jpg"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /** * 图片活体检测(分数最高人脸) */