Naming style and availability may differ slightly across platforms/languages.
STATIC_IMAGE_MODE
If set to false, the solution treats the input images as a video stream. It will try to detect faces in the first input images, and upon a successful detection further localizes the face landmarks. In subsequent images, once all max_num_faces faces are detected and the corresponding face landmarks are localized, it simply tracks those landmarks without invoking another detection until it loses track of any of the faces. This reduces latency and is ideal for processing video frames. If set to true, face detection runs on every input image, ideal for processing a batch of static, possibly unrelated, images. Default to false.
MAX_NUM_FACES
Maximum number of faces to detect. Default to 1.
MIN_DETECTION_CONFIDENCE
Minimum confidence value ([0.0, 1.0]) from the face detection model for the detection to be considered successful. Default to 0.5.
MIN_TRACKING_CONFIDENCE
Minimum confidence value ([0.0, 1.0]) from the landmark-tracking model for the face landmarks to be considered tracked successfully, or otherwise face detection will be invoked automatically on the next input image. Setting it to a higher value can increase robustness of the solution, at the expense of a higher latency. Ignored if static_image_mode is true, where face detection simply runs on every image. Default to 0.5.
Naming style may differ slightly across platforms/languages.
MULTI_FACE_LANDMARKS
Collection of detected/tracked faces, where each face is represented as a list of 468 face landmarks and each landmark is composed of x, y and z. x and y are normalized to [0.0, 1.0] by the image width and height respectively. z represents the landmark depth with the depth at center of the head being the origin, and the smaller the value the closer the landmark is to the camera. The magnitude of z uses roughly the same scale as x.
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh
# For webcam input:
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
cap = cv2.VideoCapture(0)
with mp_face_mesh.FaceMesh(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as face_mesh:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# 水平翻轉照片
# 轉換檔案格式 BGR image to RGB.
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
# 為了增加效能, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
results = face_mesh.process(image)
# 劃出相關的點在圖片上
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_face_landmarks:
# face_landmarks 只是一個自訂命名,也可以更換為aa
for face_landmarks in results.multi_face_landmarks:
# face_landmarks換為aa的話
mp_drawing.draw_landmarks(
image=image,
landmark_list=face_landmarks,
# 這裡也可以換為aa
connections=mp_face_mesh.FACE_CONNECTIONS,
landmark_drawing_spec=drawing_spec,
connection_drawing_spec=drawing_spec)
cv2.imshow('MediaPipe FaceMesh', image)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
這張圖片下載後,放大看,每個landmarks點都有標註數字
ICSHOP有進階的教學活動,http://www.circuspi.com/index.php/2021/06/23/ai-mediapipe-unit3/
做進階的實作時,要注意幾個點
首先先下載星號圖片(可以自己下載啦),而且更改檔案格式、名稱與程式碼一樣 path2880.png
確認eye_list,裡面的點座標(在抓取各點的過程中,特別針對編號153、159兩個點的座標透過運算求出距離,作為我們眼睛的大小並存放在「eye_size」 )
(也可以更改成右眼的座標,只是要一併修改程式碼第46行,兩個點座標的距離作為右眼,右眼取386.380兩個點)
右眼的 eye_list = [362, 398, 382, 385, 380, 386, 374, 387, 373, 388, 390, 466, 249, 263]
將取得的各點座標清單「eye_point」透過「statistics」計算出眼睛中心座標,並依照「eye_size」將自訂的眼睛圖片轉換成符合的大小
右眼的效果