readme及案例更新

This commit is contained in:
dengwenjie
2025-07-07 14:53:02 +08:00
parent ba64e3b38b
commit 9eb313c80a
3 changed files with 115 additions and 55 deletions

View File

@@ -123,7 +123,20 @@ SmartJavaAI是专为JAVA 开发者打造的一个功能丰富、开箱即用的
</td> </td>
<td> <td>
<div align="center"> <div align="center">
<img src="https://cdn.jsdelivr.net/gh/geekwenjie/SmartJavaAI-Site/images/liveness.jpg" width = "500px"/> <img src="https://cdn.jsdelivr.net/gh/geekwenjie/SmartJavaAI-Site/images/liveness2.jpg" width = "500px"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="left">
<p>人脸表情识别</p>
- 7种表情检测 <br>
</div>
</td>
<td>
<div align="center">
<img src="https://cdn.jsdelivr.net/gh/geekwenjie/SmartJavaAI-Site/images/emotion.jpg" width = "500px"/>
</div> </div>
</td> </td>
</tr> </tr>

View File

@@ -88,12 +88,15 @@ public class ExpressionRecDemo {
*/ */
@Test @Test
public void testExpressionDetect() { public void testExpressionDetect() {
ExpressionModel model = getExpressionModel(); try (ExpressionModel model = getExpressionModel()){
R<ExpressionResult> result = model.detectTopFace("src/main/resources/emotion/happy.png"); R<ExpressionResult> result = model.detectTopFace("src/main/resources/emotion/happy.png");
if(result.isSuccess()){ if(result.isSuccess()){
log.info("识别结果:{}", JSONObject.toJSONString(result.getData().getExpression().getDescription())); log.info("识别结果:{}", JSONObject.toJSONString(result.getData().getExpression().getDescription()));
}else{ }else{
log.info("识别失败:{}", result.getMessage()); log.info("识别失败:{}", result.getMessage());
}
} catch (Exception e) {
throw new RuntimeException(e);
} }
} }
@@ -103,15 +106,18 @@ public class ExpressionRecDemo {
*/ */
@Test @Test
public void testExpressionDetect2() { public void testExpressionDetect2() {
ExpressionModel model = getExpressionModel(); try (ExpressionModel model = getExpressionModel()){
R<DetectionResponse> result = model.detect("src/main/resources/emotion/happy.png"); R<DetectionResponse> result = model.detect("src/main/resources/emotion/happy.png");
if(result.isSuccess()){ if(result.isSuccess()){
//log.info("识别结果:{}", JSONObject.toJSONString(result.getData())); //log.info("识别结果:{}", JSONObject.toJSONString(result.getData()));
for (DetectionInfo detectionInfo : result.getData().getDetectionInfoList()) { for (DetectionInfo detectionInfo : result.getData().getDetectionInfoList()) {
log.info("识别结果:{}", JSONObject.toJSONString(detectionInfo.getFaceInfo().getExpressionResult().getExpression().getDescription())); log.info("识别结果:{}", JSONObject.toJSONString(detectionInfo.getFaceInfo().getExpressionResult().getExpression().getDescription()));
}
}else{
log.info("识别失败:{}", result.getMessage());
} }
}else{ } catch (Exception e) {
log.info("识别失败:{}", result.getMessage()); throw new RuntimeException(e);
} }
} }
@@ -122,27 +128,24 @@ public class ExpressionRecDemo {
*/ */
@Test @Test
public void testExpressionDetect3() { public void testExpressionDetect3() {
FaceDetModel faceDetModel = getFaceDetModel(); try (FaceDetModel faceDetModel = getFaceDetModel();
ExpressionModel model = getExpressionModel(); ExpressionModel model = getExpressionModel()){
// 将图片路径转换为 BufferedImage BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString()));
BufferedImage image = null; R<DetectionResponse> detResult = faceDetModel.detect(image);
try { if(detResult.isSuccess()){
image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString())); R<List<ExpressionResult>> result = model.detect(image, detResult.getData());
} catch (IOException e) { if(result.isSuccess()){
throw new FaceException("无效图片路径", e); result.getData().forEach(expressionResult -> {
} log.info("识别结果:{}", JSONObject.toJSONString(expressionResult.getExpression().getDescription()));
R<DetectionResponse> detResult = faceDetModel.detect(image); });
if(detResult.isSuccess()){ }else{
R<List<ExpressionResult>> result = model.detect(image, detResult.getData()); log.info("识别失败:{}", result.getMessage());
if(result.isSuccess()){ }
result.getData().forEach(expressionResult -> {
log.info("识别结果:{}", JSONObject.toJSONString(expressionResult.getExpression().getDescription()));
});
}else{ }else{
log.info("识别失败:{}", result.getMessage()); log.info("人脸检测失败:{}", detResult.getMessage());
} }
}else{ } catch (Exception e) {
log.info("人脸检测失败:{}", detResult.getMessage()); throw new RuntimeException(e);
} }
} }
@@ -153,27 +156,47 @@ public class ExpressionRecDemo {
*/ */
@Test @Test
public void testExpressionDetect4() { public void testExpressionDetect4() {
FaceDetModel faceDetModel = getFaceDetModel(); try (FaceDetModel faceDetModel = getFaceDetModel();
ExpressionModel model = getExpressionModel(); ExpressionModel model = getExpressionModel()){
// 将图片路径转换为 BufferedImage BufferedImage image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString()));
BufferedImage image = null; R<DetectionResponse> detResult = faceDetModel.detect(image);
try { if(detResult.isSuccess()){
image = ImageIO.read(new File(Paths.get("src/main/resources/emotion/happy.png").toAbsolutePath().toString())); for (DetectionInfo detectionInfo : detResult.getData().getDetectionInfoList()) {
} catch (IOException e) { R<ExpressionResult> result = model.detect(image, detectionInfo.getDetectionRectangle(), detectionInfo.getFaceInfo().getKeyPoints());
throw new FaceException("无效图片路径", e); if(result.isSuccess()){
} log.info("识别结果:{}", JSONObject.toJSONString(result.getData().getExpression().getDescription()));
R<DetectionResponse> detResult = faceDetModel.detect(image); }else{
if(detResult.isSuccess()){ log.info("识别失败:{}", result.getMessage());
for (DetectionInfo detectionInfo : detResult.getData().getDetectionInfoList()) { }
R<ExpressionResult> 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{ } catch (Exception e) {
log.info("人脸检测失败:{}", detResult.getMessage()); 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<DetectionResponse> 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 @Test
public void testLivenessDetectCamera(){ public void testExpressionDetectCamera(){
try (ExpressionModel expressionModel = getExpressionModel()){ try (ExpressionModel expressionModel = getExpressionModel()){
OpenCV.loadShared(); OpenCV.loadShared();
VideoCapture capture = new VideoCapture(0); VideoCapture capture = new VideoCapture(0);

View File

@@ -25,6 +25,7 @@ import cn.smartjavaai.face.factory.LivenessModelFactory;
import cn.smartjavaai.face.model.expression.ExpressionModel; import cn.smartjavaai.face.model.expression.ExpressionModel;
import cn.smartjavaai.face.model.facedect.FaceDetModel; import cn.smartjavaai.face.model.facedect.FaceDetModel;
import cn.smartjavaai.face.model.liveness.LivenessDetModel; import cn.smartjavaai.face.model.liveness.LivenessDetModel;
import cn.smartjavaai.face.utils.FaceUtils;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import nu.pattern.OpenCV; import nu.pattern.OpenCV;
@@ -71,7 +72,7 @@ public class LivenessDetDemo {
config.setModelEnum(LivenessModelEnum.IIC_FL_MODEL); config.setModelEnum(LivenessModelEnum.IIC_FL_MODEL);
config.setDevice(device); 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,超过阈值则认为是真人,低于阈值是非活体 //人脸活体阈值,可选,默认0.8,超过阈值则认为是真人,低于阈值是非活体
config.setRealityThreshold(LivenessConstant.DEFAULT_REALITY_THRESHOLD); config.setRealityThreshold(LivenessConstant.DEFAULT_REALITY_THRESHOLD);
/*视频检测帧数可选默认10输出帧数超过这个number之后就可以输出识别结果。 /*视频检测帧数可选默认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<DetectionResponse> 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);
}
}
/** /**
* 图片活体检测(分数最高人脸) * 图片活体检测(分数最高人脸)
*/ */