Spaces:
Runtime error
Runtime error
Update video_processing.py
Browse files- video_processing.py +19 -4
video_processing.py
CHANGED
@@ -18,7 +18,7 @@ import torch
|
|
18 |
import mediapipe as mp
|
19 |
|
20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
-
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.
|
22 |
|
23 |
def extract_frames(video_path, output_folder, desired_fps, progress_callback=None):
|
24 |
os.makedirs(output_folder, exist_ok=True)
|
@@ -43,6 +43,21 @@ def extract_frames(video_path, output_folder, desired_fps, progress_callback=Non
|
|
43 |
return frame_count, original_fps
|
44 |
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
def process_frames(frames_folder, faces_folder, frame_count, progress):
|
47 |
embeddings_by_frame = {}
|
48 |
posture_scores_by_frame = {}
|
@@ -60,15 +75,15 @@ def process_frames(frames_folder, faces_folder, frame_count, progress):
|
|
60 |
posture_scores_by_frame[frame_num] = posture_score
|
61 |
posture_landmarks_by_frame[frame_num] = posture_landmarks
|
62 |
|
63 |
-
boxes, probs = mtcnn.detect(frame)
|
64 |
|
65 |
if boxes is not None and len(boxes) > 0 and probs[0] >= 0.99:
|
66 |
x1, y1, x2, y2 = [int(b) for b in boxes[0]]
|
67 |
face = frame[y1:y2, x1:x2]
|
68 |
-
|
|
|
69 |
face_resized = cv2.resize(face, (160, 160))
|
70 |
output_path = os.path.join(faces_folder, f"frame_{frame_num}_face.jpg")
|
71 |
-
cv2.imwrite(output_path, face_resized)
|
72 |
cv2.imwrite(output_path, cv2.cvtColor(face_resized, cv2.COLOR_RGB2BGR))
|
73 |
face_paths.append(output_path)
|
74 |
embedding = get_face_embedding(face_resized)
|
|
|
18 |
import mediapipe as mp
|
19 |
|
20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
+
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.98, 0.98, 0.98], min_face_size=200, post_process=False)
|
22 |
|
23 |
def extract_frames(video_path, output_folder, desired_fps, progress_callback=None):
|
24 |
os.makedirs(output_folder, exist_ok=True)
|
|
|
43 |
return frame_count, original_fps
|
44 |
|
45 |
|
46 |
+
def is_frontal_face(face, landmarks):
|
47 |
+
if landmarks is None:
|
48 |
+
return False
|
49 |
+
|
50 |
+
left_eye = landmarks[0]
|
51 |
+
right_eye = landmarks[1]
|
52 |
+
nose = landmarks[2]
|
53 |
+
|
54 |
+
eye_angle = np.degrees(np.arctan2(right_eye[1] - left_eye[1], right_eye[0] - left_eye[0]))
|
55 |
+
|
56 |
+
eye_center = ((left_eye[0] + right_eye[0]) / 2, (left_eye[1] + right_eye[1]) / 2)
|
57 |
+
nose_deviation = abs(nose[0] - eye_center[0]) / face.shape[1]
|
58 |
+
|
59 |
+
return abs(eye_angle) < 10 and nose_deviation < 0.1
|
60 |
+
|
61 |
def process_frames(frames_folder, faces_folder, frame_count, progress):
|
62 |
embeddings_by_frame = {}
|
63 |
posture_scores_by_frame = {}
|
|
|
75 |
posture_scores_by_frame[frame_num] = posture_score
|
76 |
posture_landmarks_by_frame[frame_num] = posture_landmarks
|
77 |
|
78 |
+
boxes, probs, landmarks = mtcnn.detect(frame, landmarks=True)
|
79 |
|
80 |
if boxes is not None and len(boxes) > 0 and probs[0] >= 0.99:
|
81 |
x1, y1, x2, y2 = [int(b) for b in boxes[0]]
|
82 |
face = frame[y1:y2, x1:x2]
|
83 |
+
|
84 |
+
if face.size > 0 and is_frontal_face(face, landmarks[0]):
|
85 |
face_resized = cv2.resize(face, (160, 160))
|
86 |
output_path = os.path.join(faces_folder, f"frame_{frame_num}_face.jpg")
|
|
|
87 |
cv2.imwrite(output_path, cv2.cvtColor(face_resized, cv2.COLOR_RGB2BGR))
|
88 |
face_paths.append(output_path)
|
89 |
embedding = get_face_embedding(face_resized)
|