关键点检测 (Keypoints):人脸 68 点、人体姿态估计

📂 所属阶段:第三阶段 — 核心视觉任务(进阶篇)
🔗 相关章节:语义分割 (Semantic Segmentation) · Vision Transformer (ViT) 详解


1. 关键点检测基础

关键点检测 = 定位特定的解剖学位置

应用:
- 人脸:眼睛、鼻子、嘴角(68 点)
- 人体:关节位置(17 点)
- 手部:指尖位置(21 点)

2. 人脸关键点检测

import cv2
import dlib

# 加载预训练模型
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# 读取图像
img = cv2.imread("face.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 检测人脸
faces = detector(gray)

for face in faces:
    # 检测关键点
    landmarks = predictor(gray, face)
    
    # 绘制关键点
    for i in range(68):
        x = landmarks.part(i).x
        y = landmarks.part(i).y
        cv2.circle(img, (x, y), 2, (0, 255, 0), -1)

cv2.imshow("Face Landmarks", img)
cv2.waitKey(0)

3. 人体姿态估计

import cv2
import numpy as np
from mediapipe import solutions

# 初始化 MediaPipe
mp_pose = solutions.pose
pose = mp_pose.Pose()

# 读取视频
cap = cv2.VideoCapture("video.mp4")

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    
    # 检测姿态
    results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    
    # 绘制骨架
    if results.pose_landmarks:
        for landmark in results.pose_landmarks.landmark:
            x = int(landmark.x * frame.shape[1])
            y = int(landmark.y * frame.shape[0])
            cv2.circle(frame, (x, y), 5, (0, 255, 0), -1)
    
    cv2.imshow("Pose Estimation", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

4. 小结

关键点检测应用:

人脸:
- 人脸识别
- 表情识别
- 虚拟试妆

人体:
- 动作识别
- 健身指导
- 游戏交互

2026 年工具:
- 人脸:dlib、MediaPipe
- 人体:OpenPose、MediaPipe
- 手部:MediaPipe

💡 记住:MediaPipe 是 2026 年最流行的关键点检测库。它快速、准确、易用。


🔗 扩展阅读