Java表情识别
人脸表情识别是一种基于计算机视觉与人工智能技术的分析方法,能够自动识别图像或视频中人脸所呈现的情绪状态。通过提取面部关键点、纹理变化等特征,模型可判断出常见的情绪类型,如高兴、愤怒、悲伤、惊讶等。该技术广泛应用于人机交互、心理分析、智能监控和教育辅导等领域。
🚀 快速开始
今天我们使用开源AI工具SmartJavaAI来实现人脸表情识别
SmartJavaAI是什么?
SmartJavaAI是专为JAVA 开发者打造的一个功能丰富、开箱即用的 JAVA AI算法工具包,致力于帮助JAVA开发者零门槛使用各种AI算法模型,开发者无需深入了解底层实现,即可轻松在 Java 代码中调用人脸识别、目标检测、OCR 等功能。底层支持包括基于 DJL (Deep Java Library) 封装的深度学习模型,以及通过 JNI 接入的 C++/Python 算法,兼容多种主流深度学习框架如 PyTorch、TensorFlow、ONNX、Paddle 等,屏蔽复杂的模型部署与调用细节,开发者无需了解 AI 底层实现即可直接在 Java 项目中集成使用,后续将持续扩展更多算法,目标是构建一个“像 Hutool 一样简单易用”的 JAVA AI 通用工具箱
1、引入 Maven 依赖
在项目的pom.xml的dependencies中加入以下内容(全部功能),也可以根据需求单独引入人脸模块,具体可以查看 SmartJavaAI官方文档
<dependency>
<groupId>cn.smartjavaai</groupId>
<artifactId>smartjavaai-all</artifactId>
<version>1.0.19</version>
</dependency>
2、模型简介下载
人脸表情识别模型(FacialExpressionRecognition、fer)
支持功能:
- 支持识别7种表情:neutral(中性)、happy(高兴)、sad(悲伤)、surprise(惊讶)、fear(恐惧)、disgust(厌恶)、anger(愤怒)
| 模型名称 | 模型简介 | 模型开源网站 |
|---|---|---|
| DensNet121 | FaceLib的densnet121表情识别模型 | Github |
| FrEmotion | FaceRecognition-LivenessDetection-Javascript | Github |
获取表情识别模型:
FaceExpressionConfig config = new FaceExpressionConfig();
config.setModelEnum(ExpressionModelEnum.FrEmotion);
config.setModelPath("/Users/xxx/Documents/develop/model/emotion/fr_expression.onnx");
config.setDevice(device);
config.setAlign(true);
config.setDetectModel(getFaceDetModel());
ExpressionModel model = ExpressionModelFactory.getInstance().getModel(config);
FaceExpressionConfig参数说明
| 字段名称 | 字段类型 | 默认值 | 说明 |
|---|---|---|---|
| modelEnum | ExpressionModelEnum | 无 | 模型枚举 |
| modelPath | String | NULL | 手动指定离线模型路径 |
| device | DeviceEnum | CPU | 指定运行设备,支持 CPU/GPU |
| detectModel | FaceDetModel | 无 | 人脸检测模型 |
| cropFace | boolean | true | 是否对输入图片中的人脸进行裁剪。推荐对原始图像开启此项,以提取更精确的特征。若图像已是标准人脸区域,可关闭以提升效率。 |
| align | boolean | false | 是否进行人脸对齐。仅在 cropFace = true 时生效。若人脸角度较正,建议关闭可提升性能;若角度偏差大,建议开启提高准确性。 |
::: warning 注意事项:
1、如果输入的图片未经过人脸矫正,则请务必设置 align = true,否则特征提取结果可能不准确。
2、如果图片未经过人脸裁剪,则请务必设置 cropFace = true,否则特征提取结果可能不准确。
:::
表情识别(多人脸)
R<DetectionResponse> detect(String imagePath)
R<DetectionResponse> detect(BufferedImage image)
R<DetectionResponse> detect(byte[] imageData)
R<DetectionResponse> detectBase64(String base64Image)
DetectionResponse字段说明
- 返回并非json格式,仅用于字段讲解
{
"detectionInfoList": [ // 检测信息列表
{
"detectionRectangle": { //矩形框
"height": 174, // 矩形高度
"width": 147, // 矩形宽度
"x": 275, // 左上角横坐标
"y": 143 // 左上角纵坐标
},
"faceInfo": { // 人脸信息
"expressionResult": {
"expression": "ANGRY", //表情枚举
"score": 1.0 // 分数
},
"keyPoints": [
{
"x": 339.5083751678467,
"y": 192.76402664184573
},
{
"x": 404.7374267578125,
"y": 197.89914321899414
},
{
"x": 388.9555263519287,
"y": 231.50675201416016
},
{
"x": 339.8661708831787,
"y": 265.51241302490234
},
{
"x": 397.7071800231933,
"y": 269.7657699584961
}
]
}
}
]
}
人脸表情识别(多人脸)- 基于已检测出的人脸区域和关键点
仅返回表情识别结果
R<List<ExpressionResult>> detect(String imagePath, DetectionResponse faceDetectionResponse)
R<List<ExpressionResult>> detect(BufferedImage image,DetectionResponse faceDetectionResponse)
R<List<ExpressionResult>> detect(byte[] imageData,DetectionResponse faceDetectionResponse)
R<List<ExpressionResult>> detectBase64(String base64Image, DetectionResponse faceDetectionResponse)
ExpressionResult字段讲解
| 字段名称 | 字段类型 | 说明 |
|---|---|---|
| expression | FacialExpression | 表情枚举 |
| score | float | 分数 |
FacialExpression表情枚举
| 枚举常量 | 英文名称 | 中文含义 |
|---|---|---|
| ANGRY | angry | 愤怒 |
| DISGUST | disgust | 厌恶 |
| FEAR | fear | 害怕 |
| HAPPY | happy | 高兴 |
| SAD | sad | 伤心 |
| SURPRISE | surprise | 惊讶 |
| NEUTRAL | neutral | 中性 |
人脸表情识别(单人脸)- 基于已检测出的人脸区域和关键点
- imagePath:图片路径
- faceDetectionRectangle:人脸检测框(人脸检测返回结果)
- keyPoints:5个人脸关键点(人脸检测返回结果),部分模型不需要
R<ExpressionResult> detect(String imagePath, DetectionRectangle faceDetectionRectangle, List<Point> keyPoints)
R<ExpressionResult> detect(BufferedImage image, DetectionRectangle faceDetectionRectangle, List<Point> keyPoints)
R<ExpressionResult> detect(byte[] imageData, DetectionRectangle faceDetectionRectangle, List<Point> keyPoints)
R<ExpressionResult> detectBase64(String base64Image, DetectionRectangle faceDetectionRectangle, List<Point> keyPoints)
R<ExpressionResult> detect(String imagePath, DetectionRectangle faceDetectionRectangle)
R<ExpressionResult> detect(BufferedImage image, DetectionRectangle faceDetectionRectangle)
R<ExpressionResult> detect(byte[] imageData, DetectionRectangle faceDetectionRectangle)
R<ExpressionResult> detectBase64(String base64Image, DetectionRectangle faceDetectionRectangle)
人脸表情识别(分数最高人脸)
检测图片中分数最高的人脸
R<ExpressionResult> detectTopFace(BufferedImage image)
R<ExpressionResult> detectTopFace(String imagePath)
R<ExpressionResult> detectTopFace(byte[] imageData)
R<ExpressionResult> detectTopFaceBase64(String base64Image)
项目地址
- 完整代码地址:examples
- SmartJavaAI 开发文档:官方文档
- SmartJavaAI Gitee开源地址:Gitee
- SmartJavaAI Github开源地址:Github
- SmartJavaAI GitCode开源地址:GitCode
113



被折叠的 条评论
为什么被折叠?



