File size: 1,260 Bytes
0e50a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import numpy as np
import cv2
import mediapipe as mp
from torchvision import transforms

# --- Torch image preprocessing (same as training) ---
preprocess = transforms.Compose([
    transforms.Grayscale(num_output_channels=3),
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize([0.5]*3, [0.5]*3)
])

# --- MediaPipe FaceMesh setup ---
mp_face = mp.solutions.face_mesh
face_mesh = mp_face.FaceMesh(static_image_mode=True, max_num_faces=1)

def get_landmark_attention(image, size=224):
    """Compute attention map from MediaPipe face mesh landmarks and resize to match input size."""
    h, w, _ = image.shape
    attn = np.zeros((h, w), dtype=np.float32)

    results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
    if results.multi_face_landmarks:
        for lm in results.multi_face_landmarks[0].landmark:
            x, y = int(lm.x * w), int(lm.y * h)
            if 0 <= x < w and 0 <= y < h:
                cv2.circle(attn, (x, y), 2, 1.0, -1)
        attn = cv2.GaussianBlur(attn, (31, 31), 0)

    attn = attn / np.max(attn) if np.max(attn) > 0 else attn
    attn_resized = cv2.resize(attn, (size, size))
    return torch.tensor(attn_resized).unsqueeze(0).float()  # shape (1,224,224)