Update utils.py
Browse files
utils.py
CHANGED
@@ -42,6 +42,7 @@ def create_annotated_video(video_path, df, mse_embeddings, largest_cluster, outp
|
|
42 |
import torch
|
43 |
from facenet_pytorch import MTCNN
|
44 |
import mediapipe as mp
|
|
|
45 |
|
46 |
video = cv2.VideoCapture(video_path)
|
47 |
fps = video.get(cv2.CAP_PROP_FPS)
|
@@ -49,23 +50,28 @@ def create_annotated_video(video_path, df, mse_embeddings, largest_cluster, outp
|
|
49 |
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
50 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
51 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
frame_number = 0
|
53 |
while True:
|
54 |
ret, frame = video.read()
|
55 |
if not ret:
|
56 |
break
|
|
|
57 |
# Detect face and draw bounding box
|
58 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
59 |
-
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.9, 0.9, 0.9], min_face_size=50)
|
60 |
boxes, _ = mtcnn.detect(frame)
|
61 |
if boxes is not None and len(boxes) > 0:
|
62 |
box = boxes[0]
|
63 |
cv2.rectangle(frame, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
|
|
|
64 |
# Draw facial landmarks
|
65 |
-
mp_face_mesh = mp.solutions.face_mesh
|
66 |
-
mp_drawing = mp.solutions.drawing_utils
|
67 |
-
mp_drawing_styles = mp.solutions.drawing_styles
|
68 |
-
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.5)
|
69 |
face_mesh_results = face_mesh.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
70 |
if face_mesh_results.multi_face_landmarks:
|
71 |
for face_landmarks in face_mesh_results.multi_face_landmarks:
|
@@ -76,11 +82,19 @@ def create_annotated_video(video_path, df, mse_embeddings, largest_cluster, outp
|
|
76 |
landmark_drawing_spec=None,
|
77 |
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()
|
78 |
)
|
|
|
79 |
# Add MSE annotation
|
80 |
if frame_number in df['Frame'].values:
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
cv2.putText(frame, f"MSE: {mse:.4f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
|
|
83 |
out.write(frame)
|
84 |
frame_number += 1
|
|
|
85 |
video.release()
|
86 |
-
out.release()
|
|
|
|
42 |
import torch
|
43 |
from facenet_pytorch import MTCNN
|
44 |
import mediapipe as mp
|
45 |
+
import numpy as np
|
46 |
|
47 |
video = cv2.VideoCapture(video_path)
|
48 |
fps = video.get(cv2.CAP_PROP_FPS)
|
|
|
50 |
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
51 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
52 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
53 |
+
|
54 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
55 |
+
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.9, 0.9, 0.9], min_face_size=50)
|
56 |
+
|
57 |
+
mp_face_mesh = mp.solutions.face_mesh
|
58 |
+
mp_drawing = mp.solutions.drawing_utils
|
59 |
+
mp_drawing_styles = mp.solutions.drawing_styles
|
60 |
+
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.5)
|
61 |
+
|
62 |
frame_number = 0
|
63 |
while True:
|
64 |
ret, frame = video.read()
|
65 |
if not ret:
|
66 |
break
|
67 |
+
|
68 |
# Detect face and draw bounding box
|
|
|
|
|
69 |
boxes, _ = mtcnn.detect(frame)
|
70 |
if boxes is not None and len(boxes) > 0:
|
71 |
box = boxes[0]
|
72 |
cv2.rectangle(frame, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
|
73 |
+
|
74 |
# Draw facial landmarks
|
|
|
|
|
|
|
|
|
75 |
face_mesh_results = face_mesh.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
76 |
if face_mesh_results.multi_face_landmarks:
|
77 |
for face_landmarks in face_mesh_results.multi_face_landmarks:
|
|
|
82 |
landmark_drawing_spec=None,
|
83 |
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()
|
84 |
)
|
85 |
+
|
86 |
# Add MSE annotation
|
87 |
if frame_number in df['Frame'].values:
|
88 |
+
frame_index = np.where(df['Frame'].values == frame_number)[0][0]
|
89 |
+
if mse_embeddings.ndim == 1:
|
90 |
+
mse = mse_embeddings[frame_index]
|
91 |
+
else:
|
92 |
+
mse = mse_embeddings[frame_index, 0] # Assuming MSE is in the first column if 2D
|
93 |
cv2.putText(frame, f"MSE: {mse:.4f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
94 |
+
|
95 |
out.write(frame)
|
96 |
frame_number += 1
|
97 |
+
|
98 |
video.release()
|
99 |
+
out.release()
|
100 |
+
face_mesh.close()
|