Spaces:
Runtime error
Runtime error
Update video_processing.py
Browse files- video_processing.py +45 -0
video_processing.py
CHANGED
|
@@ -46,6 +46,51 @@ def extract_frames(video_path, output_folder, desired_fps, progress_callback=Non
|
|
| 46 |
clip.close()
|
| 47 |
return frame_count, original_fps
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def process_video(video_path, anomaly_threshold, desired_fps, progress=None):
|
| 50 |
start_time = time.time()
|
| 51 |
output_folder = "output"
|
|
|
|
| 46 |
clip.close()
|
| 47 |
return frame_count, original_fps
|
| 48 |
|
| 49 |
+
|
| 50 |
+
def process_frames(frames_folder, aligned_faces_folder, frame_count, progress):
|
| 51 |
+
embeddings_by_frame = {}
|
| 52 |
+
posture_scores_by_frame = {}
|
| 53 |
+
posture_landmarks_by_frame = {}
|
| 54 |
+
facial_landmarks_by_frame = {}
|
| 55 |
+
aligned_face_paths = []
|
| 56 |
+
frame_files = sorted([f for f in os.listdir(frames_folder) if f.endswith('.jpg')])
|
| 57 |
+
|
| 58 |
+
for i, frame_file in enumerate(frame_files):
|
| 59 |
+
frame_num = int(frame_file.split('_')[1].split('.')[0])
|
| 60 |
+
frame_path = os.path.join(frames_folder, frame_file)
|
| 61 |
+
frame = cv2.imread(frame_path)
|
| 62 |
+
|
| 63 |
+
if frame is not None:
|
| 64 |
+
posture_score, posture_landmarks = calculate_posture_score(frame)
|
| 65 |
+
posture_scores_by_frame[frame_num] = posture_score
|
| 66 |
+
posture_landmarks_by_frame[frame_num] = posture_landmarks
|
| 67 |
+
|
| 68 |
+
boxes, probs = mtcnn.detect(frame)
|
| 69 |
+
|
| 70 |
+
if boxes is not None and len(boxes) > 0 and probs[0] >= 0.99:
|
| 71 |
+
x1, y1, x2, y2 = [int(b) for b in boxes[0]]
|
| 72 |
+
face = frame[y1:y2, x1:x2]
|
| 73 |
+
if face.size > 0:
|
| 74 |
+
face_rgb = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
|
| 75 |
+
results = face_mesh.process(face_rgb)
|
| 76 |
+
if results.multi_face_landmarks:
|
| 77 |
+
facial_landmarks_by_frame[frame_num] = results.multi_face_landmarks[0]
|
| 78 |
+
if is_frontal_face(results.multi_face_landmarks[0].landmark):
|
| 79 |
+
aligned_face = face
|
| 80 |
+
|
| 81 |
+
if aligned_face is not None:
|
| 82 |
+
aligned_face_resized = cv2.resize(aligned_face, (160, 160))
|
| 83 |
+
output_path = os.path.join(aligned_faces_folder, f"frame_{frame_num}_face.jpg")
|
| 84 |
+
cv2.imwrite(output_path, aligned_face_resized)
|
| 85 |
+
aligned_face_paths.append(output_path)
|
| 86 |
+
embedding = get_face_embedding(aligned_face_resized)
|
| 87 |
+
embeddings_by_frame[frame_num] = embedding
|
| 88 |
+
|
| 89 |
+
progress((i + 1) / len(frame_files), f"Processing frame {i + 1} of {len(frame_files)}")
|
| 90 |
+
|
| 91 |
+
return embeddings_by_frame, posture_scores_by_frame, posture_landmarks_by_frame, aligned_face_paths, facial_landmarks_by_frame
|
| 92 |
+
|
| 93 |
+
|
| 94 |
def process_video(video_path, anomaly_threshold, desired_fps, progress=None):
|
| 95 |
start_time = time.time()
|
| 96 |
output_folder = "output"
|