| 9 | |
| 10 | |
| 11 | class FaceAlignment(object): |
| 12 | def __init__(self, gpu_id=None, alignment_model_path="", det_model_path=""): |
| 13 | expand_ratio = 0.15 |
| 14 | |
| 15 | self.face_alignment_net_222 = create_onnx_session( |
| 16 | alignment_model_path, gpu_id=gpu_id |
| 17 | ) |
| 18 | self.onnx_input_name_222 = self.face_alignment_net_222.get_inputs()[0].name |
| 19 | self.onnx_output_name_222 = [ |
| 20 | output.name for output in self.face_alignment_net_222.get_outputs() |
| 21 | ] |
| 22 | self.face_image_size = 128 |
| 23 | |
| 24 | self.face_detector = FaceDet(det_model_path, gpu_id=gpu_id) |
| 25 | self.expand_ratio = expand_ratio |
| 26 | |
| 27 | def onnx_infer(self, input_uint8): |
| 28 | assert input_uint8.shape[0] == input_uint8.shape[1] == self.face_image_size |
| 29 | onnx_input = ( |
| 30 | input_uint8.transpose((2, 0, 1)).astype(np.float32)[np.newaxis, :, :, :] |
| 31 | / 255.0 |
| 32 | ) |
| 33 | landmark, euler, prob = self.face_alignment_net_222.run( |
| 34 | self.onnx_output_name_222, {self.onnx_input_name_222: onnx_input} |
| 35 | ) |
| 36 | |
| 37 | landmark = ( |
| 38 | np.reshape(landmark[0], (2, -1)).transpose((1, 0)) * self.face_image_size |
| 39 | ) |
| 40 | left_eye_corner = landmark[74] |
| 41 | right_eye_corner = landmark[96] |
| 42 | radian = np.arctan2( |
| 43 | right_eye_corner[1] - left_eye_corner[1], |
| 44 | right_eye_corner[0] - left_eye_corner[0] + 0.00000001, |
| 45 | ) |
| 46 | euler_rad = np.array([euler[0, 0], euler[0, 1], radian], dtype=np.float32) |
| 47 | prob = prob[0] |
| 48 | |
| 49 | return landmark, euler_rad, prob |
| 50 | |
| 51 | def forward(self, src_image, face_box=None, pre_pts=None, iterations=3): |
| 52 | if pre_pts is None: |
| 53 | if face_box is None: |
| 54 | # Detect max size face |
| 55 | bounding_boxes, _, score = self.face_detector.detect(src_image) |
| 56 | print("facedet score", score) |
| 57 | if len(bounding_boxes) == 0: |
| 58 | return None |
| 59 | bbox = np.zeros(4, dtype=np.float32) |
| 60 | if len(bounding_boxes) >= 1: |
| 61 | max_area = 0.0 |
| 62 | for each_bbox in bounding_boxes: |
| 63 | area = (each_bbox[2] - each_bbox[0]) * ( |
| 64 | each_bbox[3] - each_bbox[1] |
| 65 | ) |
| 66 | if area > max_area: |
| 67 | bbox[:4] = each_bbox[:4] |
| 68 | max_area = area |
no outgoing calls
no test coverage detected