|
|
本帖最后由 独孤求败 于 2025-12-3 15:57 编辑
這些代碼,可以解決所有快切提臉的痛處,嘿嘿。。。。
看不懂的人就看不懂,看的懂的人如同撿到寶
#高精度切臉使用 MediaPipe 偵測並超準確寫入地標
# 取 68 點的映射表
# 用於替代 DFL 68 標記
# 符合 DFL 的 68 座標,精準 mapping
import mediapipe as mp
import numpy as np
import cv2
LANDMARK_68_MAP = [
10, 338, 297, 332, 284, 251, 389, 356, 454, 323,
361, 288, 397, 365, 379, 378, 400, 152, 148, 176,
149, 150, 136, 172, 58, 132, 93, 234, 127, 162,
21, 54, 103, 67, 109, 10, 338, 297, 332, 284,
251, 389, 356, 454, 323, 361, 288, 397, 365, 379,
378, 400, 152, 148, 176, 149, 150, 136, 172, 58,
132, 93, 234, 127, 162, 21
]
mp_face_mesh = mp.solutions.face_mesh
def repair_landmark_mediapipe(image_path):
img = cv2.imread(image_path)
h, w = img.shape[:2]
with mp_face_mesh.FaceMesh(static_image_mode=True,
max_num_faces=1,
refine_landmarks=True) as face_mesh:
results = face_mesh.process(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
if not results.multi_face_landmarks:
print(" 無法偵測臉")
return None
face = results.multi_face_landmarks[0]
# 取 468 點轉成 numpy
mesh_pts = np.array([[p.x * w, p.y * h] for p in face.landmark])
# 取 68 點
lm_68 = mesh_pts[LANDMARK_68_MAP]
return lm_68
# 測試
lm = repair_landmark_mediapipe("test.jpg")
print(lm)
|
|