You need to enable JavaScript to run this app. Android人脸情绪检测开发:如何根据识别结果展示对应情绪图片
优惠活动
大模型
产品
解决方案
定价
更多

Android人脸情绪检测开发:如何根据识别结果展示对应情绪图片

实现方案

步骤1:准备情绪对应的图片资源

把每种情绪对应的图片放到项目的res/drawable文件夹下,命名和情绪一一对应即可,参考命名:

  • 愤怒对应ic_anger.png
  • 轻蔑对应ic_contempt.png
  • 厌恶对应ic_disgust.png
  • 恐惧对应ic_fear.png
  • 开心对应ic_happiness.png
  • 平静对应ic_neutral.png
  • 悲伤对应ic_sadness.png
  • 惊讶对应ic_surprise.png

步骤2:修改列表项布局文件item_face_with_description.xml

在原有文字控件text_detected_face下方新增一个ImageView控件,用来展示情绪对应图片:

<!-- 原有控件保持不变,新增以下内容 -->
<ImageView
    android:id="@+id/iv_emotion_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="8dp"
    android:scaleType="centerCrop" />

尺寸、边距可以根据自己的界面需求调整。

步骤3:修改FaceListAdapter逻辑

调整getEmotion方法返回值

原来的方法只返回拼接好的文本,现在需要额外返回情绪类型匹配图片,修改成返回Pair类型,第一个值是情绪类型,第二个值是要展示的文本:

private Pair<String, String> getEmotion(Emotion emotion) {
    String emotionType = "";
    double emotionValue = 0.0;
    String emotionInfo = "";
    if (emotion.anger > emotionValue) {
        emotionValue = emotion.anger;
        emotionType = "Anger";
        emotionInfo = "如果还没喂他/她,说不定小家伙是渴了或者饿了。试试多陪陪他,如果宝宝表现反常最好及时就医。";
    }
    if (emotion.contempt > emotionValue) {
        emotionValue = emotion.contempt;
        emotionType = "Contempt";
        emotionInfo = "可太厉害了!";
    }
    if (emotion.disgust > emotionValue) {
        emotionValue = emotion.disgust;
        emotionType = "Disgust";
        emotionInfo = "看呀,如果宝宝是这个反应,说明他/她可能不喜欢现在的东西。如果你现在做的事对他/她好,可以多引导试试。";
    }
    if (emotion.fear > emotionValue) {
        emotionValue = emotion.fear;
        emotionType = "Fear";
        emotionInfo = "宝宝看起来有点不舒服,先让他/她放松下来,记好是什么让他/她出现这种反应。";
    }
    if (emotion.happiness > emotionValue) {
        emotionValue = emotion.happiness;
        emotionType = "Happiness";
        emotionInfo = "继续保持现在的做法就好,最好记下来什么事能让宝宝这么开心。";
    }
    if (emotion.neutral > emotionValue) {
        emotionValue = emotion.neutral;
        emotionType = "Neutral";
        emotionInfo = "可以先观察一会儿宝宝的状态。";
    }
    if (emotion.sadness > emotionValue) {
        emotionValue = emotion.sadness;
        emotionType = "Sadness";
        emotionInfo = "抱抱宝宝或者轻轻晃晃他/她就好。";
    }
    if (emotion.surprise > emotionValue) {
        emotionValue = emotion.surprise;
        emotionType = "Surprise";
        emotionInfo = "哇,陪宝宝玩玩吧,试试玩躲猫猫。";
    }
    String showText = String.format("%s: %.2f \n\n%s", emotionType, emotionValue, emotionInfo);
    return new Pair<>(emotionType, showText);
}

如果提示Pair找不到,在文件顶部添加导入语句import android.util.Pair;即可。

修改getView方法加载对应图片

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater layoutInflater =
                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.item_face_with_description, parent, false);
    }
    convertView.setId(position);

    // 原有展示人脸缩略图逻辑保持不变
    ((ImageView) convertView.findViewById(R.id.face_thumbnail)).setImageBitmap(
            faceThumbnails.get(position));

    // 获取情绪数据
    Pair<String, String> emotionData = getEmotion(faces.get(position).faceAttributes.emotion);
    String emotionType = emotionData.first;
    String faceDescription = emotionData.second;

    // 设置情绪展示文本
    ((TextView) convertView.findViewById(R.id.text_detected_face)).setText(faceDescription);

    // 加载对应情绪的图片
    ImageView emotionIv = convertView.findViewById(R.id.iv_emotion_image);
    int drawableId = convertView.getResources().getIdentifier(
            "ic_" + emotionType.toLowerCase(), // 转小写和drawable的文件名匹配
            "drawable",
            getPackageName()
    );
    if (drawableId != 0) { // 找到对应资源就设置
        emotionIv.setImageResource(drawableId);
    } else { // 没找到资源可以设置默认图或者隐藏控件
        emotionIv.setVisibility(View.GONE);
    }

    return convertView;
}

修改完成后检测到对应情绪就会自动加载你提前放入的对应图片,后续需要调整图片样式直接修改布局文件里的ImageView参数即可。

内容的提问来源于stack exchange,提问作者Riri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 07:06:00