File size: 1,374 Bytes
32d37f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
import mediapipe as mp
import streamlit as st


class Detection:
    multi_face_bboxes = []
    multi_face_landmarks = []


#@st.cache_resource
def load_detection_model(max_faces=2, detection_confidence=0.5, tracking_confidence=0.5):
    model = mp.solutions.face_mesh.FaceMesh(
            refine_landmarks=True,
            min_detection_confidence=detection_confidence,
            min_tracking_confidence=tracking_confidence,
            max_num_faces=max_faces,
        )
    return model


def detect_faces(frame, model):
    
    # Process the frame with MediaPipe Face Mesh
    results = model.process(frame)

    # Get the Bounding Boxes from the detected faces
    detections = []
    if results.multi_face_landmarks:
        for landmarks in results.multi_face_landmarks:
            x_coords = [
                landmark.x * frame.shape[1] for landmark in landmarks.landmark
            ]
            y_coords = [
                landmark.y * frame.shape[0] for landmark in landmarks.landmark
            ]

            x_min, x_max = int(min(x_coords)), int(max(x_coords))
            y_min, y_max = int(min(y_coords)), int(max(y_coords))

            detection = Detection()
            detection.multi_face_bboxes=[x_min, y_min, x_max, y_max]
            detection.multi_face_landmarks=landmarks
            detections.append(detection)
    return detections