目录
DeepFace的诞生
简单总结
DeepFace能干嘛都
1. 人脸验证(Face Verification)
2. 人脸识别(Face Recognition)
3. 表情识别(Emotion Recognition)
4. 性别、年龄和种族识别
5. 生成人脸特征向量(Face Embeddings)
6. 多种深度学习模型支持
7. 即时视频分析(实时人脸识别)
8. 训练自己的数据集
步骤
总结
可以做的项目
DeepFace的诞生
DeepFace 由 Facebook(Meta) 于 2014 年 研发,最初是一个 深度学习 的人脸识别模型,旨在提升 Facebook 平台的 自动人脸识别和标记功能。
不过,现在你所使用的 DeepFace(Python 库) 并不是 Facebook 官方的,而是 Serengil(一个开源开发者)基于 Facebook 的 DeepFace 研究 独立开发的 Python 库。这个库利用了多个深度学习模型(如 VGG-Face、Facenet、OpenFace、DeepID、ArcFace 等),并支持人脸识别、验证、表情分析、年龄/性别预测等功能。
简单总结
Facebook DeepFace(2014):Meta 研发的人脸识别模型,应用于 Facebook 平台。
Python DeepFace(开源库):由 Serengil 开发的 Python 库,支持多个深度学习模型,广泛用于人脸分析。
你现在用的 DeepFace Python 库 其实是一个 非官方的开源项目,并不是 Facebook 官方维护的。
DeepFace能干嘛都
1. 人脸验证(Face Verification)
作用:判断两张人脸是否属于同一个人,类似于门禁或身份验证系统。 应用场景:考勤打卡、门禁系统、身份认证。
示例代码:
from deepface import DeepFace
result = DeepFace.verify(img1_path="person1.jpg", img2_path="person2.jpg")
print("是否同一个人:", result["verified"])
输出:
{"verified": true, "distance": 0.32, "threshold": 0.40}
如果 distance 小于 threshold,则是同一个人。
2. 人脸识别(Face Recognition)
作用:从数据库中找出最相似的人脸。 应用场景:智能相册、访客管理、安防系统。
示例代码:
from deepface import DeepFace
result = DeepFace.find(img_path="test.jpg", db_path="face_database/")
print(result)
它会在 face_database/ 目录下搜索最匹配的面部图像。
3. 表情识别(Emotion Recognition)
作用:检测人脸的表情,如 开心、愤怒、悲伤 等。 应用场景:情绪监测、市场分析、心理健康检测。
示例代码:
from deepface import DeepFace
result = DeepFace.analyze(img_path="person.jpg", actions=['emotion'])
print(result[0]["dominant_emotion"])
可能的表情类别:
happy(高兴)
sad(悲伤)
angry(愤怒)
fear(恐惧)
surprise(惊讶)
neutral(中性)
4. 性别、年龄和种族识别
作用:预测人的 年龄、性别、种族。 应用场景:市场分析、广告投放、用户画像。
示例代码:
from deepface import DeepFace
result = DeepFace.analyze(img_path="person.jpg", actions=['age', 'gender', 'race'])
print(result)
输出示例:
{ "age": 25, "gender": "Man", "dominant_race": "White" }
支持的种族:
white(白种人)
black(黑种人)
asian(亚洲人)
indian(印度人)
middle eastern(中东人)
latino hispanic(拉丁美洲人)
5. 生成人脸特征向量(Face Embeddings)
DeepFace 可以提取 128D/512D 人脸特征向量,用于机器学习任务(聚类、人脸相似度计算)。
示例代码:
from deepface.basemodels import Facenet
from deepface.commons import functions
model = Facenet.loadModel()
img = functions.preprocess_face("person.jpg")
embedding = model.predict(img)[0]
print("人脸特征向量:", embedding)
可以用 cosine similarity 计算相似度:
import numpy as np
sim = np.dot(embedding1, embedding2) / (np.linalg.norm(embedding1) * np.linalg.norm(embedding2))
print("相似度:", sim)
6. 多种深度学习模型支持
DeepFace 允许切换不同的深度学习模型来分析人脸:
VGG-Face(默认)
Facenet
OpenFace
DeepID
Dlib
ArcFace
示例代码:
from deepface import DeepFace
result = DeepFace.verify("img1.jpg", "img2.jpg", model_name="ArcFace")
print(result)
7. 即时视频分析(实时人脸识别)
可以结合 OpenCV 进行 实时人脸识别:
import cv2
from deepface import DeepFace
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
try:
result = DeepFace.analyze(frame, actions=['age', 'gender', 'emotion'], enforce_detection=False)
emotion = result[0]['dominant_emotion']
age = result[0]['age']
gender = result[0]['gender']
cv2.putText(frame, f'Emotion: {emotion}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(frame, f'Age: {age}', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(frame, f'Gender: {gender}', (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
except Exception as e:
print("无法检测到人脸:", e)
cv2.imshow("Real-Time Face Analysis", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
8. 训练自己的数据集
你可以使用自己的 人脸数据库 进行训练,提高识别准确率。
步骤
收集人脸数据,存放在 dataset/ 目录。
训练人脸模型:
DeepFace.find(img_path="test.jpg", db_path="dataset/")
优化模型,调整 distance_metric(可选 cosine, euclidean, euclidean_l2):
DeepFace.find(img_path="test.jpg", db_path="dataset/", distance_metric="cosine")
总结
功能作用代码示例人脸验证判断两张脸是否是同一个人DeepFace.verify(img1, img2)人脸识别在数据库中查找匹配人脸DeepFace.find(img, db_path)表情识别识别表情,如开心、愤怒等DeepFace.analyze(img, actions=['emotion'])性别/年龄/种族预测性别、年龄、种族DeepFace.analyze(img, actions=['age', 'gender', 'race'])人脸特征向量获取 128D/512D 的人脸特征model.predict(img)自定义模型使用不同的深度学习模型DeepFace.verify(img1, img2, model_name="Facenet")实时人脸分析结合 OpenCV 进行视频流分析cv2.VideoCapture(0)
可以做的项目
✅ 智能考勤:基于人脸识别的员工考勤系统 ✅ 门禁系统:自动识别人脸,判断是否放行 ✅ 情绪分析:分析顾客或用户情绪,优化服务 ✅ 智能相册:按人脸分类照片,找出相似面孔 ✅ 个性化广告:根据用户年龄、性别、种族推送广告 ✅ 安全监控:安防系统检测陌生人脸