Download app/utils/preprocess.py from Akash297/TEPI: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/Akash297/TEPI/resolve/main/app/utils/preprocess.py
- Command line
-
hf download hf://spaces/Akash297/TEPI/app/utils/preprocess.py
-
curl -L -o preprocess.py https://huggingface.co/spaces/Akash297/TEPI/resolve/main/app/utils/preprocess.py
1.26 kB
| 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) | |