Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -315,10 +315,10 @@ def plot_anomaly_scores(df, anomaly_scores, top_indices, title):
|
|
| 315 |
plt.tight_layout()
|
| 316 |
return fig
|
| 317 |
|
| 318 |
-
def plot_emotion(df, emotion, num_anomalies):
|
| 319 |
fig, ax = plt.subplots(figsize=(16, 8))
|
| 320 |
values = df[emotion].values
|
| 321 |
-
bars = ax.bar(range(len(df)), values, width=0.8, color=
|
| 322 |
top_indices = np.argsort(values)[-num_anomalies:][::-1]
|
| 323 |
for i in top_indices:
|
| 324 |
bars[i].set_color('red')
|
|
@@ -331,17 +331,14 @@ def plot_emotion(df, emotion, num_anomalies):
|
|
| 331 |
plt.tight_layout()
|
| 332 |
return fig
|
| 333 |
|
| 334 |
-
def
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
ax.legend()
|
| 343 |
-
plt.tight_layout()
|
| 344 |
-
return fig
|
| 345 |
|
| 346 |
def process_video(video_path, num_anomalies, num_components, desired_fps, batch_size, progress=gr.Progress()):
|
| 347 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
@@ -390,11 +387,17 @@ def process_video(video_path, num_anomalies, num_components, desired_fps, batch_
|
|
| 390 |
try:
|
| 391 |
anomaly_plot_all = plot_anomaly_scores(df, anomaly_scores_all, top_indices_all, "All Features")
|
| 392 |
anomaly_plot_comp = plot_anomaly_scores(df, anomaly_scores_comp, top_indices_comp, "Components Only")
|
| 393 |
-
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
| 395 |
except Exception as e:
|
| 396 |
return f"Error generating plots: {str(e)}", None, None, None, None, None, None
|
| 397 |
|
|
|
|
|
|
|
|
|
|
| 398 |
progress(1.0, "Preparing results")
|
| 399 |
results = f"Top {num_anomalies} anomalies (All Features):\n"
|
| 400 |
results += "\n".join([f"{score:.4f} at {timecode}" for score, timecode in
|
|
@@ -408,8 +411,8 @@ def process_video(video_path, num_anomalies, num_components, desired_fps, batch_
|
|
| 408 |
results += f"\n\nTop {num_anomalies} {emotion.capitalize()} Scores:\n"
|
| 409 |
results += "\n".join([f"{df[emotion].iloc[i]:.4f} at {df['Timecode'].iloc[i]}" for i in top_indices])
|
| 410 |
|
| 411 |
-
return results, anomaly_plot_all, anomaly_plot_comp,
|
| 412 |
-
|
| 413 |
# Gradio interface
|
| 414 |
iface = gr.Interface(
|
| 415 |
fn=process_video,
|
|
@@ -422,9 +425,9 @@ iface = gr.Interface(
|
|
| 422 |
],
|
| 423 |
outputs=[
|
| 424 |
gr.Textbox(label="Anomaly Detection Results"),
|
|
|
|
| 425 |
gr.Plot(label="Anomaly Scores (All Features)"),
|
| 426 |
gr.Plot(label="Anomaly Scores (Components Only)"),
|
| 427 |
-
gr.Plot(label="UMAP Components"),
|
| 428 |
gr.Plot(label="Fear Scores"),
|
| 429 |
gr.Plot(label="Sad Scores"),
|
| 430 |
gr.Plot(label="Angry Scores")
|
|
|
|
| 315 |
plt.tight_layout()
|
| 316 |
return fig
|
| 317 |
|
| 318 |
+
def plot_emotion(df, emotion, num_anomalies, color):
|
| 319 |
fig, ax = plt.subplots(figsize=(16, 8))
|
| 320 |
values = df[emotion].values
|
| 321 |
+
bars = ax.bar(range(len(df)), values, width=0.8, color=color)
|
| 322 |
top_indices = np.argsort(values)[-num_anomalies:][::-1]
|
| 323 |
for i in top_indices:
|
| 324 |
bars[i].set_color('red')
|
|
|
|
| 331 |
plt.tight_layout()
|
| 332 |
return fig
|
| 333 |
|
| 334 |
+
def get_random_face_sample(organized_faces_folder, largest_cluster):
|
| 335 |
+
person_folder = os.path.join(organized_faces_folder, f"person_{largest_cluster}")
|
| 336 |
+
face_files = [f for f in os.listdir(person_folder) if f.endswith('.jpg')]
|
| 337 |
+
if face_files:
|
| 338 |
+
random_face = np.random.choice(face_files)
|
| 339 |
+
face_path = os.path.join(person_folder, random_face)
|
| 340 |
+
return face_path
|
| 341 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
def process_video(video_path, num_anomalies, num_components, desired_fps, batch_size, progress=gr.Progress()):
|
| 344 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
| 387 |
try:
|
| 388 |
anomaly_plot_all = plot_anomaly_scores(df, anomaly_scores_all, top_indices_all, "All Features")
|
| 389 |
anomaly_plot_comp = plot_anomaly_scores(df, anomaly_scores_comp, top_indices_comp, "Components Only")
|
| 390 |
+
emotion_plots = [
|
| 391 |
+
plot_emotion(df, 'fear', num_anomalies, 'lightblue'),
|
| 392 |
+
plot_emotion(df, 'sad', num_anomalies, 'lightgreen'),
|
| 393 |
+
plot_emotion(df, 'angry', num_anomalies, 'salmon')
|
| 394 |
+
]
|
| 395 |
except Exception as e:
|
| 396 |
return f"Error generating plots: {str(e)}", None, None, None, None, None, None
|
| 397 |
|
| 398 |
+
# Get a random face sample
|
| 399 |
+
face_sample = get_random_face_sample(organized_faces_folder, largest_cluster)
|
| 400 |
+
|
| 401 |
progress(1.0, "Preparing results")
|
| 402 |
results = f"Top {num_anomalies} anomalies (All Features):\n"
|
| 403 |
results += "\n".join([f"{score:.4f} at {timecode}" for score, timecode in
|
|
|
|
| 411 |
results += f"\n\nTop {num_anomalies} {emotion.capitalize()} Scores:\n"
|
| 412 |
results += "\n".join([f"{df[emotion].iloc[i]:.4f} at {df['Timecode'].iloc[i]}" for i in top_indices])
|
| 413 |
|
| 414 |
+
return results, face_sample, anomaly_plot_all, anomaly_plot_comp, *emotion_plots
|
| 415 |
+
|
| 416 |
# Gradio interface
|
| 417 |
iface = gr.Interface(
|
| 418 |
fn=process_video,
|
|
|
|
| 425 |
],
|
| 426 |
outputs=[
|
| 427 |
gr.Textbox(label="Anomaly Detection Results"),
|
| 428 |
+
gr.Image(label="Random Face Sample of Most Frequent Person"),
|
| 429 |
gr.Plot(label="Anomaly Scores (All Features)"),
|
| 430 |
gr.Plot(label="Anomaly Scores (Components Only)"),
|
|
|
|
| 431 |
gr.Plot(label="Fear Scores"),
|
| 432 |
gr.Plot(label="Sad Scores"),
|
| 433 |
gr.Plot(label="Angry Scores")
|