逐行解释以下代码class MediaPipeFace: def init(self, staticimagemode=True, maxnumfaces=1,…
- User
逐行解释以下代码class MediaPipeFace: def init(self, static_image_mode=True, max_num_faces=1, refine_landmarks=True):
Access MediaPipe Solutions Python API
mp_faces = mp.solutions.face_mesh
Initialize MediaPipe FaceMesh
static_image_mode:
For video processing set to False:
Will use previous frame to localize face to reduce latency
For unrelated images set to True:
To allow face detection to run on every input images
max_num_faces:
Maximum number of faces to detect
refine_landmarks
Set to True to further refine the landmark coordinates around the eyes and lips
and output additional landmarks around the irises. Default to false.
min_detection_confidence:
Confidence value [0,1] from face detection model
for detection to be considered successful
min_tracking_confidence:
Minimum confidence value [0,1] from landmark-tracking model
for 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.
self.pipe = mp_faces.FaceMesh( static_image_mode=static_image_mode, max_num_faces=max_num_faces, refine_landmarks=refine_landmarks, min_detection_confidence=0.5, min_tracking_confidence=0.5)
Define face parameter
self.param = [] for i in range(max_num_faces): num = 478 if refine_landmarks else 468 p = { ‘detect’ : False, # Boolean to indicate whether a face is detected ‘keypt’ : np.zeros((num,2)), # 2D keypt in image coordinate (pixel) ‘joint’ : np.zeros((num,3)), # 3D joint in relative coordinate ‘fps’ : -1, # Frame per sec } self.param.append§
def result_to_param(self, result, img):
Convert mediapipe result to my own param
img_height, img_width, _ = img.shape
Reset param
for p in self.param: p[‘detect’] = False
if result.multi_face_landmarks is not None:
Loop through different faces
for i, res in enumerate(result.multi_face_landmarks): self.param[i][‘detect’] = True
Loop through 468 landmark for each face
for j, lm in enumerate(res.landmark): self.param[i][‘keypt’][j,0] = lm.x * img_width # Convert normalized coor to pixel [0,1] -> [0,width] self.param[i][‘keypt’][j,1] = lm.y * img_height # Convert normalized coor to pixel [0,1] -> [0,height]
self.param[i][‘joint’][j,0] = lm.x self.param[i][‘joint’][j,1] = lm.y self.param[i][‘joint’][j,2] = lm.z
- Assistant
该代码定义了一个名为MediaPipeFace的类,该类用于人脸检测和关键点提取。init函数用于初始化MediaPipe FaceMesh(人脸检测和关键点提取解决方案)。其中,static_image_mode参数用于控制处理静态图像或视频流,max_num_faces参数用于设置检测的最大人脸数,refine_landmarks参数用于精确化眼睛和嘴巴周围的地标坐标,并输出虹膜周围的额外地标。min_detection_confidence参数用于设置脸部检测的置信度阈值,min_tracking_confidence参数用于设置跟踪器的置信度阈值。
该类还定义了result_to_param函数,该函数用于将MediaPipe FaceMesh输出的结果转换为自己定义的参数param。具体来说,该函数遍历每个检测到的人脸,并将其关键点和关节坐标存储在对应的param变量中。
最后,类还定义了forward函数,该函数用于前向传递并处理图像。该函数首先将图像从BGR转换为RGB,然后调用MediaPipe FaceMesh的process方法提取结果,并将结果转换为自定义的参数param。最后,函数返回param。