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

@@ -88,12 +88,15 @@ public class ExpressionRecDemo {
*/
@Test
public void testExpressionDetect() {
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()));
}else{
log.info("识别失败:{}", result.getMessage());
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()));
}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<DetectionResponse> 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<DetectionResponse> 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<DetectionResponse> detResult = faceDetModel.detect(image);
if(detResult.isSuccess()){
R<List<ExpressionResult>> 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<DetectionResponse> detResult = faceDetModel.detect(image);
if(detResult.isSuccess()){
R<List<ExpressionResult>> 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<DetectionResponse> detResult = faceDetModel.detect(image);
if(detResult.isSuccess()){
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());
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()){
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{
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<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
public void testLivenessDetectCamera(){
public void testExpressionDetectCamera(){
try (ExpressionModel expressionModel = getExpressionModel()){
OpenCV.loadShared();
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.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<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);
}
}
/**
* 图片活体检测(分数最高人脸)
*/