#实战项目一:智能人脸考勤系统
📂 所属阶段:第六阶段 — 顶级综合项目实战
🔗 相关章节:边缘计算初探 · 实战项目二:工业缺陷检测
#1. 系统架构
输入:摄像头视频流
↓
人脸检测(MTCNN)
↓
人脸对齐
↓
人脸识别(ArcFace)
↓
数据库匹配
↓
输出:考勤记录#2. 人脸检测(MTCNN)
from mtcnn import MTCNN
import cv2
detector = MTCNN()
# 检测人脸
image = cv2.imread("photo.jpg")
faces = detector.detect_faces(image)
for face in faces:
x, y, w, h = face['box']
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Faces", image)
cv2.waitKey(0)#3. 人脸识别(ArcFace)
import torch
from arcface import ArcFace
# 加载模型
model = ArcFace.load_pretrained()
# 提取特征
face_embedding = model.get_embedding(face_image)
# 计算相似度
similarity = torch.nn.functional.cosine_similarity(
face_embedding, database_embedding
)
# 匹配
if similarity > 0.6:
print("识别成功")
else:
print("陌生人")#4. 完整系统
import cv2
from mtcnn import MTCNN
from arcface import ArcFace
import sqlite3
class AttendanceSystem:
def __init__(self):
self.detector = MTCNN()
self.model = ArcFace.load_pretrained()
self.db = sqlite3.connect("attendance.db")
def register_face(self, name, image_path):
image = cv2.imread(image_path)
faces = self.detector.detect_faces(image)
if faces:
face = faces[0]
x, y, w, h = face['box']
face_image = image[y:y+h, x:x+w]
embedding = self.model.get_embedding(face_image)
# 保存到数据库
cursor = self.db.cursor()
cursor.execute(
"INSERT INTO users (name, embedding) VALUES (?, ?)",
(name, embedding.numpy().tobytes())
)
self.db.commit()
def recognize(self, image_path):
image = cv2.imread(image_path)
faces = self.detector.detect_faces(image)
results = []
for face in faces:
x, y, w, h = face['box']
face_image = image[y:y+h, x:x+w]
embedding = self.model.get_embedding(face_image)
# 从数据库查询
cursor = self.db.cursor()
cursor.execute("SELECT name, embedding FROM users")
for name, db_embedding in cursor.fetchall():
db_embedding = torch.from_numpy(np.frombuffer(db_embedding, dtype=np.float32))
similarity = torch.nn.functional.cosine_similarity(
embedding, db_embedding.unsqueeze(0)
)
if similarity > 0.6:
results.append((name, similarity.item()))
return results#5. 小结
人脸考勤系统关键技术:
1. 人脸检测:MTCNN(快速准确)
2. 人脸识别:ArcFace(业界标准)
3. 数据库:存储人脸特征
4. 实时处理:视频流处理
性能指标:
- 检测速度:30+ FPS
- 识别准确率:99%+
- 误识率:< 0.1%💡 记住:ArcFace 是人脸识别的黄金标准。它的准确率和速度都是业界最好的。
🔗 扩展阅读

