File size: 1,090 Bytes
eabc9cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
import cv2 as cv
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision

base_options = python.BaseOptions(model_asset_path='resources/fd_model.tflite')
options = vision.FaceDetectorOptions(base_options=base_options)
detector = vision.FaceDetector.create_from_options(options)


def format_detections(detection_result):
    faces = []
    for detection in detection_result.detections:
        face = {
            "bbox": {
                "x": detection.bounding_box.origin_x,
                "y": detection.bounding_box.origin_y,
                "width": detection.bounding_box.width,
                "height": detection.bounding_box.height,
            },
        }
        faces.append(face)
    return faces


def faces_detection_controller(image: bytes):
    cv_image = cv.imdecode(np.frombuffer(image, np.uint8), cv.IMREAD_COLOR)
    mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=cv_image)
    # Process image
    detection_result = detector.detect(mp_image)
    return format_detections(detection_result)