Skip to content

Repository files navigation

https://github.com/serengil/deepface

逐次运行下面的命令

一、环境配置(新建一个conda环境或uv环境)

1.进入路径

cd \path\to\your\project

2.克隆项目

git clone https://github.com/serengil/deepface.git
cd deepface
pip install -e .

3.进行缺失模块管理和版本调动

pip install dlib
pip uninstall tensorflow
pip install tensorflow==2.12
pip install insightface
pip install onnxruntime
pip install openpyxl

4.由于通过自动下载的带宽较小,建议将权重文件手动下载到path\to\your\user\.deepface\weights,例如:C:\Users\Lenovo\.deepface\weights。 下载地址:

https://github.com/serengil/deepface_models/releases/

需要下载的文件: ![[Pasted image 20251102092731.png]]

==下载压缩包,同样解压到weight文件夹下==

https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip

==注:其余的权重文件建议使用自动下载==

二.测试

新建一个测试文件进行测试

from deepface import DeepFace
objs = DeepFace.analyze(
  img_path = r"E:\source\face_detect\deepface\tests\dataset\img1.jpg", actions = ['age', 'gender', 'race', 'emotion']
)
print(objs)
print('\n')
print("-----------------------------------------------------------------------------------------------------------------------")
print('\n')
models = [
    "VGG-Face", "Facenet", "Facenet512", "OpenFace", "DeepFace",
    "DeepID", "ArcFace", "Dlib", "SFace", "GhostFaceNet",
    "Buffalo_L",
]
#result = DeepFace.verify(
#  img1_path = "img1.jpg", img2_path = "img2.jpg", model_name = models[0]
#)
#print(result)
#dfs = DeepFace.find(
#  img_path = "img1.jpg", db_path = "C:/my_db", model_name = models[1]
#)
#print(dfs)

for i in range(len(models)):
    embeddings = DeepFace.represent(
    img_path = r"E:\source\face_detect\deepface\tests\dataset\img1.jpg", model_name = models[i]
    )
    print(embeddings)
    print('\n')
    print("-----------------------------------------------------------------------------------------------------------------------")
    print('\n')

三.运行效果

[{'age': 32, 'region': {'x': 339, 'y': 218, 'w': 768, 'h': 768, 'left_eye': (850, 524), 'right_eye': (571, 517)}, 'face_confidence': 0.92, 'gender': {'Woman': 99.996018409729, 'Man': 0.003984810609836131}, 'dominant_gender': 'Woman', 'race': {'asian': 11.139613389968872, 'indian': 14.312215149402618, 'black': 3.4734193235635757, 'white': 22.394436597824097, 'middle eastern': 20.415005087852478, 'latino hispanic': 28.26530635356903}, 'dominant_race': 'latino hispanic', 'emotion': {'angry': 2.421008077555166e-16, 'disgust': 4.031997291524061e-32, 'fear': 2.3676907330436928e-21, 'happy': 99.99997019767584, 'sad': 6.027658865505478e-15, 'surprise': 4.22508719880881e-09, 'neutral': 3.2343873153593514e-05}, 'dominant_emotion': 'happy'}]

完整输出示例(含微表情字段)

  • 通过本工具的图片分析(与 DeepFace 字段命名一致,追加微表情):
{
  "id": 1,
  "source": ".\\deepface\\icon\\stock-1.jpg",
  "age": 44,
  "gender": { "Woman": 50.39, "Man": 49.61 },
  "dominant_gender": "Woman",
  "race": {
    "asian": 25.69, "indian": 13.14, "black": 12.62,
    "white": 15.51, "middle eastern": 15.09, "latino hispanic": 17.95
  },
  "dominant_race": "asian",
  "region": { "x": 0, "y": 0, "w": 176, "h": 252, "left_eye": null, "right_eye": null },
  "face_confidence": 0.0,
  "emotion": {
    "angry": 81.83, "disgust": 0.00, "fear": 0.05,
    "happy": 2.03, "sad": 2.96, "surprise": 2.59, "neutral": 10.54
  },
  "dominant_emotion": "angry",
  "primary": { "label": "angry", "confidence": 81.831654 },
  "microexpression": {
    "landmarks": [ /* 68 点坐标,已省略 */ ],
    "au": { "AU1": 0.316, "AU2": 0.296, "AU4": 0.694, "AU6": 0.812, "AU12": 0.571, "AU25": 0.672, "AU26": 1.008, "AU27": 1.344 },
    "label": "真笑(Duchenne)",
    "labels": [ "真笑(Duchenne)", "皱眉", "轻微张口", "下颌下垂", "大张口" ]
  },
  "micro_events": []
}

说明:

  • DeepFace 原生字段:agegenderdominant_genderracedominant_raceregionface_confidenceemotiondominant_emotion
  • 本工具追加字段:idsourceprimary(情绪最高项/置信度)、microexpression(含 landmarks / au / 标签)、micro_events(视频/实时场景)。

四.微表情识别(AU 与事件)

  1. 单张图片(写入 AU 与标签)
python deepface_emotion_detection.py --input .\deepface\icon\stock-1.jpg --task image --enable_microexpressions true --gpu false --output_dir .\out
  • 输出位置:out\image_stock-1.jpg_YYYYMMDD_HHMMSS\cache\*.json
  • 关键字段示例:
{
  "payload": {
    "microexpression": {
      "landmarks": [ /* 68 点坐标 */ ],
      "au": { "AU1": 0.32, "AU12": 0.57, "AU25": 0.41, "AU26": 0.72, "AU27": 1.25 },
      "label": "真笑(Duchenne)",
      "labels": ["真笑(Duchenne)", "皱眉", "轻微张口", "下颌下垂", "大张口"]
    },
    "micro_events": []
  }
}
  • 说明:单图无时间维度,micro_events 为空;label 为主标签,labels 为全部命中标签。
  1. 视频/实时(输出微表情事件)
  • 视频:
python deepface_emotion_detection.py --input .\samples\demo.mp4 --task video --enable_microexpressions true --micro_window_size 15 --micro_delta 0.15 --micro_min_gap 5 --gpu false --output_dir .\out
  • 实时:
python deepface_emotion_detection.py --input 0 --task stream --display true --enable_microexpressions true --micro_window_size 15 --micro_delta 0.15 --micro_min_gap 5
  • 输出位置:out\<源文件>_YYYYMMDD_HHMMSS\micro_events.json
  • 事件示例:
[
  {
    "frame": 123,
    "au": { "AU12": 0.8, "AU6": 0.6 },
    "delta": { "AU12": 0.25 },
    "intensity": 0.8,
    "label": "真笑(Duchenne)",
    "labels": ["真笑(Duchenne)"]
  }
]
  1. 参数说明(简要)
  • --enable_microexpressions:启用 68 点与 AU、事件检测。
  • --micro_window_size:时序窗口长度(帧),越大越稳。
  • --micro_delta:AU 相对窗口均值的突增阈值。
  • --micro_min_gap:两事件最小间隔帧数,降低抖动。
  • --output_dir:指定输出根目录。
  1. 依赖与提示
  • 需要 models\shape_predictor_68_face_landmarks.dat(已随项目提供)。
  • 未检测到人脸或关键点时,microexpressionlandmarks/au/labels 可能为空。
  • --gpu true 仅在正确安装 CUDA/cuDNN 的环境下有效;否则用 CPU。

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages