官方文件說明與範例 https://google.github.io/mediapipe/solutions/face_detection.html
以下只是將官方文件範例程式碼加註中文說明
相關參數
Naming style and availability may differ slightly across platforms/languages.
MODEL_SELECTION
An integer index 0 or 1. Use 0 to select a short-range model that works best for faces within 2 meters from the camera, and 1 for a full-range model best for faces within 5 meters. For the full-range option, a sparse model is used for its improved inference speed. Please refer to the model cards for details. Default to 0 if not specified.
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.
Naming style may differ slightly across platforms/languages.
DETECTIONS
Collection of detected faces, where each face is represented as a detection proto message that contains a bounding box and 6 key points (right eye, left eye, nose tip, mouth center, right ear tragion, and left ear tragion). The bounding box is composed of xmin and width (both normalized to [0.0, 1.0] by the image width) and ymin and height (both normalized to [0.0, 1.0] by the image height). Each key point is composed of x and y, which are normalized to [0.0, 1.0] by the image width and height respectively.
有關Python語言的with用法說明:文字擷取自https://blog.gtwang.org/programming/python-with-context-manager-tutorial/
Python 語言提供了 with 這個獨特的語法,可讓程式設計者更容易管理這些開啟的資源,在這樣的語法架構之下,Python 程式會自動進行資源的建立、清理與回收動作,讓程式設計者在使用各種資源時更為方便
例如 # 以 with 開啟檔案
with open(filename) as f:
這裡在使用 with 開啟檔案時,會將開啟的檔案一樣放在 f 變數中,但是這個 f 只有在這個 with 的範圍內可以使用,而離開這個範圍時 f 就會自動被關閉,回收相關的資源。
所以下方的程式碼
with mp_face_detection.FaceDetection(
model_selection=0, min_detection_confidence=0.5) as face_detection:
當偵測時,把偵測的資料放進face_detection這個變數,當結束偵測時,face_detection就會自動關閉,回收相關的資源
import cv2
import mediapipe as mp
# 匯入cv2與mediapipe,並且命名
# face Detection只是Mediapipe的眾多solutions其中的一個
# 新增物件mp_face_detection,mp_drawing
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
# For webcam input:
# 做影片的擷取,0代表使用webcam
cap = cv2.VideoCapture(0)
# FaceDetection的參數model_selection有0跟1可以選擇,選擇0代表是2公尺以內,如果選擇1代表5公尺以內
# min_detection_confidence通常設0.5,當值到0.5時代表偵測到的是一張人臉
with mp_face_detection.FaceDetection(
model_selection=0, min_detection_confidence=0.5) as face_detection:
# 以下都是while的語法,當開啟視訊成功時要執行的,如果成功就開始擷取圖片,視訊沒開啟成功就繼續往下執行
# 如果視訊鏡頭故障或其他因素沒有開啟,把continue換成break會中斷程式
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 影像轉換為 RGB
# cv2.flip(image, 1)裡的1帶表對Y軸做旋轉,0是對X軸
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
# face_detection處理結果傳回到results變數
results = face_detection.process(image)
# 繪製臉部偵測的註解到影像裡,包含裡面的每一個點跟框
# 首先要把image改成可寫入
# 處理完準備輸出前把影像由RGB轉換為BGR
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# 如果有results的值,就把每個偵測的點描繪上去圖片裡
if results.detections:
# 因為有六個點,包括耳朵、眼睛、鼻子跟嘴巴,所以使用for把六個點標註上去
# 也有其他參數可以改變顏色
for detection in results.detections:
mp_drawing.draw_detection(image, detection)
# 視窗標題MediaPipe Face Detection
cv2.imshow('MediaPipe Face Detection', image)
# 如果按ESC就離開
if cv2.waitKey(5) & 0xFF == 27:
break
# 釋出webcam使用資源
cap.release()
差異在下面那一段程式碼
if len(results.detections) > 2: #偵測到的人臉數大於2
for detection in results.detections:
mp_drawing.draw_detection(image, detection,
bbox_drawing_spec=mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=7))
else:
# 如果不需要判斷人數大於幾個人的話就從 if ......else:這一段程式碼刪除
import cv2
import mediapipe as mp
# 匯入cv2與mediapipe,並且命名
# face Detection只是Mediapipe的眾多solutions其中的一個
# 新增物件mp_face_detection,mp_drawing
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
# For webcam input:
# 做影片的擷取,0代表使用webcam
cap = cv2.VideoCapture(0)
# FaceDetection的參數model_selection有0跟1可以選擇,選擇0代表是2公尺以內,如果選擇1代表5公尺以內
# min_detection_confidence通常設0.5,當值到0.5時代表偵測到的是一張人臉
with mp_face_detection.FaceDetection(
model_selection=0, min_detection_confidence=0.5) as face_detection:
# 以下都是while的語法,當開啟視訊成功時要執行的,如果成功就開始擷取圖片,視訊沒開啟成功就繼續往下執行
# 如果視訊鏡頭故障或其他因素沒有開啟,把continue換成break會中斷程式
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 影像轉換為 RGB
# cv2.flip(image, 1)裡的1帶表對Y軸做旋轉,0是對X軸
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
# face_detection處理結果傳回到results變數
results = face_detection.process(image)
# 繪製臉部偵測的註解到影像裡,包含裡面的每一個點跟框
# 首先要把image改成可寫入
# 處理完準備輸出前把影像由RGB轉換為BGR
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# 如果有results的值,就把每個偵測的點描繪上去圖片裡
if results.detections:
# 因為有六個點,包括耳朵、眼睛、鼻子跟嘴巴,所以使用for把六個點標註上去
# 也有其他參數可以改變顏色
if len(results.detections) > 2: #偵測到的人臉數大於2
for detection in results.detections:
mp_drawing.draw_detection(image, detection,
bbox_drawing_spec=mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=7))
else:
# 如果不需要判斷人數大於幾個人的話就從 if ......else:這一段程式碼刪除
for detection in results.detections:
mp_drawing.draw_detection(image, detection)
# 視窗標題MediaPipe Face Detection
cv2.imshow('MediaPipe Face Detection', image)
# 如果按ESC就離開
if cv2.waitKey(5) & 0xFF == 27:
break
# 釋出webcam使用資源
cap.release()