| 45 | |
| 46 | |
| 47 | class Mediapipe(): |
| 48 | def __init__(self, mode): |
| 49 | super(Mediapipe, self).__init__() |
| 50 | |
| 51 | self.mode=mode |
| 52 | |
| 53 | base_options = python.BaseOptions( |
| 54 | model_asset_path=os.path.join(SCRIPT_DIR,'./assets/face_landmarker.task'), |
| 55 | delegate=mp.tasks.BaseOptions.Delegate.GPU |
| 56 | ) |
| 57 | options = vision.FaceLandmarkerOptions( |
| 58 | running_mode=mode, |
| 59 | base_options=base_options, |
| 60 | output_face_blendshapes=False, |
| 61 | output_facial_transformation_matrixes=False, |
| 62 | num_faces=10, |
| 63 | min_face_detection_confidence=0.1, |
| 64 | min_face_presence_confidence=0.1, |
| 65 | ) |
| 66 | self.detector = vision.FaceLandmarker.create_from_options(options) |
| 67 | |
| 68 | def run(self, rgb_image_numpy): |
| 69 | |
| 70 | # STEP 3: Load the input image. |
| 71 | image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb_image_numpy) |
| 72 | |
| 73 | |
| 74 | # STEP 4: Detect face landmarks from the input image. |
| 75 | if self.mode==VisionRunningMode.IMAGE: |
| 76 | detection_result = self.detector.detect(image) |
| 77 | else: |
| 78 | raise Exception("Sorry, not implemented") |
| 79 | |
| 80 | |
| 81 | if len (detection_result.face_landmarks) == 0: |
| 82 | print('No face detected') |
| 83 | return None, None |
| 84 | |
| 85 | # face_landmarks = detection_result.face_landmarks[0] |
| 86 | |
| 87 | # Find the largest face by bounding box area |
| 88 | largest_face_index = -1 |
| 89 | max_area = 0 |
| 90 | |
| 91 | print("number of faces detected", len(detection_result.face_landmarks)) |
| 92 | for i, landmarks in enumerate(detection_result.face_landmarks): |
| 93 | x_min = min([lm.x for lm in landmarks]) |
| 94 | x_max = max([lm.x for lm in landmarks]) |
| 95 | y_min = min([lm.y for lm in landmarks]) |
| 96 | y_max = max([lm.y for lm in landmarks]) |
| 97 | |
| 98 | # Compute bounding box area |
| 99 | area = (x_max - x_min) * (y_max - y_min) |
| 100 | |
| 101 | if area > max_area: |
| 102 | max_area = area |
| 103 | largest_face_index = i |
| 104 | |