Spaces:
Paused
Paused
Update facial_diagnostics.py
Browse files- facial_diagnostics.py +92 -90
facial_diagnostics.py
CHANGED
|
@@ -1,90 +1,92 @@
|
|
| 1 |
-
# facial_diagnostics.py
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import cv2
|
| 5 |
-
import numpy as np
|
| 6 |
-
import
|
| 7 |
-
import
|
| 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 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
"
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"
|
| 87 |
-
"
|
| 88 |
-
"
|
| 89 |
-
"
|
| 90 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# facial_diagnostics.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
import math
|
| 7 |
+
from statistics import median
|
| 8 |
+
|
| 9 |
+
# Facial metric helpers
|
| 10 |
+
LEFT_EYE_IDX = [33, 160, 158, 133, 153, 144]
|
| 11 |
+
RIGHT_EYE_IDX = [362, 385, 387, 263, 373, 380]
|
| 12 |
+
MOUTH_IDX = [13, 14, 78, 308]
|
| 13 |
+
SMILE_LEFT = 61
|
| 14 |
+
SMILE_RIGHT = 291
|
| 15 |
+
|
| 16 |
+
def eye_aspect_ratio(pts, idx):
|
| 17 |
+
a = np.linalg.norm(np.array(pts[idx[1]]) - np.array(pts[idx[5]]))
|
| 18 |
+
b = np.linalg.norm(np.array(pts[idx[2]]) - np.array(pts[idx[4]]))
|
| 19 |
+
c = np.linalg.norm(np.array(pts[idx[0]]) - np.array(idx[3])) + 1e-8
|
| 20 |
+
return float((a + b) / (2.0 * c))
|
| 21 |
+
|
| 22 |
+
def mouth_ratio(pts):
|
| 23 |
+
top, bottom, left, right = pts[MOUTH_IDX[0]], pts[MOUTH_IDX[1]], pts[MOUTH_IDX[2]], pts[MOUTH_IDX[3]]
|
| 24 |
+
return float(np.linalg.norm(np.array(top) - np.array(bottom)) /
|
| 25 |
+
(np.linalg.norm(np.array(left) - np.array(right)) + 1e-8))
|
| 26 |
+
|
| 27 |
+
def head_tilt_angle(pts):
|
| 28 |
+
left = np.mean([pts[33], pts[133]], axis=0)
|
| 29 |
+
right = np.mean([pts[362], pts[263]], axis=0)
|
| 30 |
+
diff = np.array(right) - np.array(left)
|
| 31 |
+
return float(math.degrees(math.atan2(diff[1], diff[0])))
|
| 32 |
+
|
| 33 |
+
def smile_symmetry(pts):
|
| 34 |
+
left = np.array(pts[SMILE_LEFT])
|
| 35 |
+
right = np.array(pts[SMILE_RIGHT])
|
| 36 |
+
center = (left + right) / 2
|
| 37 |
+
L = np.linalg.norm(left - center)
|
| 38 |
+
R = np.linalg.norm(right - center)
|
| 39 |
+
if L + R == 0:
|
| 40 |
+
return 1.0
|
| 41 |
+
return float(min(L, R) / max(L, R))
|
| 42 |
+
|
| 43 |
+
# Face mesh
|
| 44 |
+
import mediapipe as mp
|
| 45 |
+
mp_face_mesh = mp.solutions.face_mesh
|
| 46 |
+
FACE_MESH = mp_face_mesh.FaceMesh(
|
| 47 |
+
static_image_mode=False,
|
| 48 |
+
max_num_faces=1,
|
| 49 |
+
refine_landmarks=True,
|
| 50 |
+
min_detection_confidence=0.6,
|
| 51 |
+
min_tracking_confidence=0.6
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def analyze_frame(frame):
|
| 55 |
+
h, w, _ = frame.shape
|
| 56 |
+
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 57 |
+
res = FACE_MESH.process(rgb)
|
| 58 |
+
|
| 59 |
+
if not res.multi_face_landmarks:
|
| 60 |
+
return {"found": False}
|
| 61 |
+
|
| 62 |
+
lm = res.multi_face_landmarks[0]
|
| 63 |
+
pts = [(int(p.x * w), int(p.y * h)) for p in lm.landmark]
|
| 64 |
+
|
| 65 |
+
ear = (eye_aspect_ratio(pts, LEFT_EYE_IDX) + eye_aspect_ratio(pts, RIGHT_EYE_IDX)) / 2
|
| 66 |
+
mratio = mouth_ratio(pts)
|
| 67 |
+
tilt = head_tilt_angle(pts)
|
| 68 |
+
symmetry = smile_symmetry(pts)
|
| 69 |
+
|
| 70 |
+
return {
|
| 71 |
+
"found": True,
|
| 72 |
+
"ear": float(ear),
|
| 73 |
+
"mouth_ratio": float(mratio),
|
| 74 |
+
"head_tilt": float(tilt),
|
| 75 |
+
"smile_sym": float(symmetry)
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
def aggregate_metrics(metrics):
|
| 79 |
+
if not metrics:
|
| 80 |
+
return {}
|
| 81 |
+
ears = [m["ear"] for m in metrics]
|
| 82 |
+
mouths = [m["mouth_ratio"] for m in metrics]
|
| 83 |
+
tilts = [m["head_tilt"] for m in metrics]
|
| 84 |
+
smiles = [m["smile_sym"] for m in metrics]
|
| 85 |
+
return {
|
| 86 |
+
"count": len(metrics),
|
| 87 |
+
"ear_median": median(ears),
|
| 88 |
+
"mouth_median": median(mouths),
|
| 89 |
+
"tilt_median": median(tilts),
|
| 90 |
+
"smile_median": median(smiles)
|
| 91 |
+
}
|
| 92 |
+
|