Spaces:
Runtime error
Runtime error
Update video_processing.py
Browse files- video_processing.py +21 -47
video_processing.py
CHANGED
|
@@ -46,49 +46,6 @@ 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_frames(frames_folder, aligned_faces_folder, frame_count, progress):
|
| 50 |
-
embeddings_by_frame = {}
|
| 51 |
-
posture_scores_by_frame = {}
|
| 52 |
-
posture_landmarks_by_frame = {}
|
| 53 |
-
facial_landmarks_by_frame = {}
|
| 54 |
-
aligned_face_paths = []
|
| 55 |
-
frame_files = sorted([f for f in os.listdir(frames_folder) if f.endswith('.jpg')])
|
| 56 |
-
|
| 57 |
-
for i, frame_file in enumerate(frame_files):
|
| 58 |
-
frame_num = int(frame_file.split('_')[1].split('.')[0])
|
| 59 |
-
frame_path = os.path.join(frames_folder, frame_file)
|
| 60 |
-
frame = cv2.imread(frame_path)
|
| 61 |
-
|
| 62 |
-
if frame is not None:
|
| 63 |
-
posture_score, posture_landmarks = calculate_posture_score(frame)
|
| 64 |
-
posture_scores_by_frame[frame_num] = posture_score
|
| 65 |
-
posture_landmarks_by_frame[frame_num] = posture_landmarks
|
| 66 |
-
|
| 67 |
-
boxes, probs = mtcnn.detect(frame)
|
| 68 |
-
|
| 69 |
-
if boxes is not None and len(boxes) > 0 and probs[0] >= 0.99:
|
| 70 |
-
x1, y1, x2, y2 = [int(b) for b in boxes[0]]
|
| 71 |
-
face = frame[y1:y2, x1:x2]
|
| 72 |
-
if face.size > 0:
|
| 73 |
-
face_rgb = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
|
| 74 |
-
results = face_mesh.process(face_rgb)
|
| 75 |
-
if results.multi_face_landmarks:
|
| 76 |
-
facial_landmarks_by_frame[frame_num] = results.multi_face_landmarks[0]
|
| 77 |
-
if is_frontal_face(results.multi_face_landmarks[0].landmark):
|
| 78 |
-
aligned_face = face
|
| 79 |
-
|
| 80 |
-
if aligned_face is not None:
|
| 81 |
-
aligned_face_resized = cv2.resize(aligned_face, (160, 160))
|
| 82 |
-
output_path = os.path.join(aligned_faces_folder, f"frame_{frame_num}_face.jpg")
|
| 83 |
-
cv2.imwrite(output_path, aligned_face_resized)
|
| 84 |
-
aligned_face_paths.append(output_path)
|
| 85 |
-
embedding = get_face_embedding(aligned_face_resized)
|
| 86 |
-
embeddings_by_frame[frame_num] = embedding
|
| 87 |
-
|
| 88 |
-
progress((i + 1) / len(frame_files), f"Processing frame {i + 1} of {len(frame_files)}")
|
| 89 |
-
|
| 90 |
-
return embeddings_by_frame, posture_scores_by_frame, posture_landmarks_by_frame, aligned_face_paths, facial_landmarks_by_frame
|
| 91 |
-
|
| 92 |
def process_video(video_path, anomaly_threshold, desired_fps, progress=None):
|
| 93 |
start_time = time.time()
|
| 94 |
output_folder = "output"
|
|
@@ -199,11 +156,28 @@ def process_video(video_path, anomaly_threshold, desired_fps, progress=None):
|
|
| 199 |
correlation_heatmap = plot_correlation_heatmap(mse_embeddings, mse_posture, mse_voice)
|
| 200 |
scatter_plot_3d = plot_3d_scatter(mse_embeddings, mse_posture, mse_voice)
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
except Exception as e:
|
| 209 |
print(f"Error details: {str(e)}")
|
|
|
|
| 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"
|
|
|
|
| 156 |
correlation_heatmap = plot_correlation_heatmap(mse_embeddings, mse_posture, mse_voice)
|
| 157 |
scatter_plot_3d = plot_3d_scatter(mse_embeddings, mse_posture, mse_voice)
|
| 158 |
|
| 159 |
+
try:
|
| 160 |
+
if progress is not None:
|
| 161 |
+
progress(0.95, desc="Generating video with heatmap")
|
| 162 |
+
|
| 163 |
+
heatmap_video_path = create_video_with_heatmap(
|
| 164 |
+
video_path, df, mse_embeddings, mse_posture, mse_voice,
|
| 165 |
+
output_folder, original_fps, largest_cluster
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
if heatmap_video_path is None:
|
| 169 |
+
print("Failed to create heatmap video")
|
| 170 |
+
else:
|
| 171 |
+
print(f"Heatmap video path from create_video_with_heatmap: {heatmap_video_path}")
|
| 172 |
+
|
| 173 |
+
if progress is not None:
|
| 174 |
+
progress(1.0, "Video processing complete")
|
| 175 |
|
| 176 |
+
except Exception as e:
|
| 177 |
+
print(f"Error in create_video_with_heatmap: {str(e)}")
|
| 178 |
+
import traceback
|
| 179 |
+
traceback.print_exc()
|
| 180 |
+
heatmap_video_path = None
|
| 181 |
|
| 182 |
except Exception as e:
|
| 183 |
print(f"Error details: {str(e)}")
|