Spaces:
No application file
No application file
Arthur PAILLE commited on
Commit ·
6c0f8cc
1
Parent(s): 6a261a1
affichage des signaux faciaux en overlay
Browse files- visualisation_live.py +43 -3
visualisation_live.py
CHANGED
|
@@ -3,7 +3,6 @@ import mediapipe as mp
|
|
| 3 |
import numpy as np
|
| 4 |
import sys
|
| 5 |
|
| 6 |
-
# Indices landmarks
|
| 7 |
NOSE_TIP = 1
|
| 8 |
CHIN = 152
|
| 9 |
FOREHEAD = 10
|
|
@@ -11,6 +10,12 @@ LEFT_EYE_OUTER = 263
|
|
| 11 |
RIGHT_EYE_OUTER = 33
|
| 12 |
LEFT_MOUTH = 61
|
| 13 |
RIGHT_MOUTH = 291
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
FACE_3D_MODEL = np.array([
|
| 16 |
[0.0, 0.0, 0.0],
|
|
@@ -50,7 +55,7 @@ def dessiner_axe_tete(frame, landmarks, frame_w, frame_h):
|
|
| 50 |
FACE_3D_MODEL, image_points, camera_matrix, np.zeros((4, 1))
|
| 51 |
)
|
| 52 |
if not success:
|
| 53 |
-
return
|
| 54 |
|
| 55 |
axis_length = 80
|
| 56 |
axes_3d = np.float32([
|
|
@@ -65,6 +70,38 @@ def dessiner_axe_tete(frame, landmarks, frame_w, frame_h):
|
|
| 65 |
cv2.line(frame, nose, tuple(axes_2d[1].ravel().astype(int)), (0, 255, 0), 2)
|
| 66 |
cv2.line(frame, nose, tuple(axes_2d[2].ravel().astype(int)), (255, 0, 0), 2)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def visualiser_live(chemin_video):
|
| 70 |
cap = cv2.VideoCapture(chemin_video)
|
|
@@ -96,7 +133,10 @@ def visualiser_live(chemin_video):
|
|
| 96 |
lm = face_landmarks.landmark
|
| 97 |
|
| 98 |
dessiner_landmarks(frame, face_landmarks, frame_w, frame_h)
|
| 99 |
-
dessiner_axe_tete(frame, lm, frame_w, frame_h)
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
cv2.imshow("Visualisation faciale - Q pour quitter", frame)
|
| 102 |
if cv2.waitKey(wait_ms) & 0xFF == ord('q'):
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import sys
|
| 5 |
|
|
|
|
| 6 |
NOSE_TIP = 1
|
| 7 |
CHIN = 152
|
| 8 |
FOREHEAD = 10
|
|
|
|
| 10 |
RIGHT_EYE_OUTER = 33
|
| 11 |
LEFT_MOUTH = 61
|
| 12 |
RIGHT_MOUTH = 291
|
| 13 |
+
TOP_LIP = 13
|
| 14 |
+
BOTTOM_LIP = 14
|
| 15 |
+
LEFT_EYE_TOP = 159
|
| 16 |
+
LEFT_EYE_BOTTOM = 145
|
| 17 |
+
RIGHT_EYE_TOP = 386
|
| 18 |
+
RIGHT_EYE_BOTTOM = 374
|
| 19 |
|
| 20 |
FACE_3D_MODEL = np.array([
|
| 21 |
[0.0, 0.0, 0.0],
|
|
|
|
| 55 |
FACE_3D_MODEL, image_points, camera_matrix, np.zeros((4, 1))
|
| 56 |
)
|
| 57 |
if not success:
|
| 58 |
+
return None, None, None
|
| 59 |
|
| 60 |
axis_length = 80
|
| 61 |
axes_3d = np.float32([
|
|
|
|
| 70 |
cv2.line(frame, nose, tuple(axes_2d[1].ravel().astype(int)), (0, 255, 0), 2)
|
| 71 |
cv2.line(frame, nose, tuple(axes_2d[2].ravel().astype(int)), (255, 0, 0), 2)
|
| 72 |
|
| 73 |
+
rotation_mat, _ = cv2.Rodrigues(rotation_vec)
|
| 74 |
+
angles, _, _, _, _, _ = cv2.RQDecomp3x3(rotation_mat)
|
| 75 |
+
return angles[0], angles[1], angles[2]
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def dessiner_signaux(frame, lm, pitch, yaw, roll):
|
| 79 |
+
face_height = abs(lm[CHIN].y - lm[FOREHEAD].y)
|
| 80 |
+
face_width = abs(lm[LEFT_EYE_OUTER].x - lm[RIGHT_EYE_OUTER].x)
|
| 81 |
+
|
| 82 |
+
mouth_opening = abs(lm[TOP_LIP].y - lm[BOTTOM_LIP].y) / face_height if face_height > 0 else 0
|
| 83 |
+
left_eye = abs(lm[LEFT_EYE_TOP].y - lm[LEFT_EYE_BOTTOM].y) / face_height if face_height > 0 else 0
|
| 84 |
+
right_eye = abs(lm[RIGHT_EYE_TOP].y - lm[RIGHT_EYE_BOTTOM].y) / face_height if face_height > 0 else 0
|
| 85 |
+
smile = abs(lm[LEFT_MOUTH].x - lm[RIGHT_MOUTH].x) / face_width if face_width > 0 else 0
|
| 86 |
+
|
| 87 |
+
lignes = [
|
| 88 |
+
f"Pitch : {pitch:.1f}",
|
| 89 |
+
f"Yaw : {yaw:.1f}",
|
| 90 |
+
f"Roll : {roll:.1f}",
|
| 91 |
+
f"Bouche: {mouth_opening:.3f}",
|
| 92 |
+
f"Oeil G: {left_eye:.3f}",
|
| 93 |
+
f"Oeil D: {right_eye:.3f}",
|
| 94 |
+
f"Sourire: {smile:.3f}",
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
overlay = frame.copy()
|
| 98 |
+
cv2.rectangle(overlay, (8, 8), (200, 18 + len(lignes) * 20), (0, 0, 0), -1)
|
| 99 |
+
cv2.addWeighted(overlay, 0.5, frame, 0.5, 0, frame)
|
| 100 |
+
|
| 101 |
+
for i, texte in enumerate(lignes):
|
| 102 |
+
cv2.putText(frame, texte, (12, 24 + i * 20),
|
| 103 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 180), 1)
|
| 104 |
+
|
| 105 |
|
| 106 |
def visualiser_live(chemin_video):
|
| 107 |
cap = cv2.VideoCapture(chemin_video)
|
|
|
|
| 133 |
lm = face_landmarks.landmark
|
| 134 |
|
| 135 |
dessiner_landmarks(frame, face_landmarks, frame_w, frame_h)
|
| 136 |
+
pitch, yaw, roll = dessiner_axe_tete(frame, lm, frame_w, frame_h)
|
| 137 |
+
|
| 138 |
+
if pitch is not None:
|
| 139 |
+
dessiner_signaux(frame, lm, pitch, yaw, roll)
|
| 140 |
|
| 141 |
cv2.imshow("Visualisation faciale - Q pour quitter", frame)
|
| 142 |
if cv2.waitKey(wait_ms) & 0xFF == ord('q'):
|