Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -324,7 +324,7 @@ def plot_emotion(df, emotion, num_anomalies, color):
|
|
| 324 |
bars[i].set_color('red')
|
| 325 |
ax.set_xlabel('Timecode')
|
| 326 |
ax.set_ylabel(f'{emotion.capitalize()} Score')
|
| 327 |
-
ax.set_title(f'{emotion.capitalize()}
|
| 328 |
ax.xaxis.set_major_locator(MaxNLocator(nbins=100))
|
| 329 |
ticks = ax.get_xticks()
|
| 330 |
ax.set_xticklabels([df['Timecode'].iloc[int(tick)] if tick >= 0 and tick < len(df) else '' for tick in ticks], rotation=90, ha='right')
|
|
@@ -340,7 +340,12 @@ def get_random_face_sample(organized_faces_folder, largest_cluster, output_folde
|
|
| 340 |
random_face = np.random.choice(face_files)
|
| 341 |
face_path = os.path.join(person_folder, random_face)
|
| 342 |
output_path = os.path.join(output_folder, "random_face_sample.jpg")
|
| 343 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
return output_path
|
| 345 |
return None
|
| 346 |
|
|
@@ -377,18 +382,17 @@ def process_video(video_path, num_anomalies, num_components, desired_fps, batch_
|
|
| 377 |
progress(0.8, "Saving person data")
|
| 378 |
df, largest_cluster = save_person_data_to_csv(embeddings_by_frame, emotions_by_frame, clusters, desired_fps, original_fps, temp_dir, num_components)
|
| 379 |
|
| 380 |
-
progress(0.
|
| 381 |
-
feature_columns = [col for col in df.columns if col not in ['Frame', 'Timecode', 'Time (Minutes)', 'Embedding_Index']]
|
| 382 |
-
X = df[feature_columns].values
|
| 383 |
-
print(f"Shape of input data: {X.shape}")
|
| 384 |
-
print(f"Feature columns: {feature_columns}")
|
| 385 |
try:
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
except Exception as e:
|
| 388 |
-
|
| 389 |
-
print(f"X shape: {X.shape}")
|
| 390 |
-
print(f"X dtype: {X.dtype}")
|
| 391 |
-
return f"Error in anomaly detection: {str(e)}", None, None, None, None, None, None
|
| 392 |
|
| 393 |
progress(0.95, "Generating plots")
|
| 394 |
try:
|
|
@@ -435,9 +439,9 @@ iface = gr.Interface(
|
|
| 435 |
gr.Image(type="filepath", label="Random Face Sample of Most Frequent Person"),
|
| 436 |
gr.Plot(label="Anomaly Scores (All Features)"),
|
| 437 |
gr.Plot(label="Anomaly Scores (Components Only)"),
|
| 438 |
-
gr.Plot(label="Fear
|
| 439 |
-
gr.Plot(label="Sad
|
| 440 |
-
gr.Plot(label="Angry
|
| 441 |
],
|
| 442 |
title="Facial Expressions Anomaly Detection",
|
| 443 |
description="""
|
|
|
|
| 324 |
bars[i].set_color('red')
|
| 325 |
ax.set_xlabel('Timecode')
|
| 326 |
ax.set_ylabel(f'{emotion.capitalize()} Score')
|
| 327 |
+
ax.set_title(f'{emotion.capitalize()} Anomalies Over Time (Top {num_anomalies} in Red)')
|
| 328 |
ax.xaxis.set_major_locator(MaxNLocator(nbins=100))
|
| 329 |
ticks = ax.get_xticks()
|
| 330 |
ax.set_xticklabels([df['Timecode'].iloc[int(tick)] if tick >= 0 and tick < len(df) else '' for tick in ticks], rotation=90, ha='right')
|
|
|
|
| 340 |
random_face = np.random.choice(face_files)
|
| 341 |
face_path = os.path.join(person_folder, random_face)
|
| 342 |
output_path = os.path.join(output_folder, "random_face_sample.jpg")
|
| 343 |
+
|
| 344 |
+
# Read the image and resize it to be smaller
|
| 345 |
+
face_img = cv2.imread(face_path)
|
| 346 |
+
small_face = cv2.resize(face_img, (80, 80)) # Resize to 80x80 pixels
|
| 347 |
+
cv2.imwrite(output_path, small_face)
|
| 348 |
+
|
| 349 |
return output_path
|
| 350 |
return None
|
| 351 |
|
|
|
|
| 382 |
progress(0.8, "Saving person data")
|
| 383 |
df, largest_cluster = save_person_data_to_csv(embeddings_by_frame, emotions_by_frame, clusters, desired_fps, original_fps, temp_dir, num_components)
|
| 384 |
|
| 385 |
+
progress(0.95, "Generating plots")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
try:
|
| 387 |
+
anomaly_plot_all = plot_anomaly_scores(df, anomaly_scores_all, top_indices_all, "All Features")
|
| 388 |
+
anomaly_plot_comp = plot_anomaly_scores(df, anomaly_scores_comp, top_indices_comp, "Components Only")
|
| 389 |
+
emotion_plots = [
|
| 390 |
+
plot_emotion(df, 'fear', num_anomalies, 'purple'),
|
| 391 |
+
plot_emotion(df, 'sad', num_anomalies, 'green'),
|
| 392 |
+
plot_emotion(df, 'angry', num_anomalies, 'orange')
|
| 393 |
+
]
|
| 394 |
except Exception as e:
|
| 395 |
+
return f"Error generating plots: {str(e)}", None, None, None, None, None, None
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
progress(0.95, "Generating plots")
|
| 398 |
try:
|
|
|
|
| 439 |
gr.Image(type="filepath", label="Random Face Sample of Most Frequent Person"),
|
| 440 |
gr.Plot(label="Anomaly Scores (All Features)"),
|
| 441 |
gr.Plot(label="Anomaly Scores (Components Only)"),
|
| 442 |
+
gr.Plot(label="Fear Anomalies"),
|
| 443 |
+
gr.Plot(label="Sad Anomalies"),
|
| 444 |
+
gr.Plot(label="Angry Anomalies")
|
| 445 |
],
|
| 446 |
title="Facial Expressions Anomaly Detection",
|
| 447 |
description="""
|