Spaces:
Runtime error
Runtime error
Update utils.py
Browse files
utils.py
CHANGED
@@ -37,64 +37,3 @@ def add_timecode_to_image_body(image, timecode):
|
|
37 |
draw.text((10, 10), timecode, (255, 0, 0), font=font)
|
38 |
return np.array(img_pil)
|
39 |
|
40 |
-
def create_annotated_video(video_path, df, mse_embeddings, largest_cluster, output_path):
|
41 |
-
import cv2
|
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)
|
49 |
-
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
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:
|
78 |
-
mp_drawing.draw_landmarks(
|
79 |
-
image=frame,
|
80 |
-
landmark_list=face_landmarks,
|
81 |
-
connections=mp_face_mesh.FACEMESH_TESSELATION,
|
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()
|
|
|
37 |
draw.text((10, 10), timecode, (255, 0, 0), font=font)
|
38 |
return np.array(img_pil)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|