Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -318,7 +318,7 @@ def plot_anomaly_scores(df, anomaly_scores, top_indices, title):
|
|
| 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.
|
| 322 |
top_indices = np.argsort(values)[-num_anomalies:][::-1]
|
| 323 |
for i in top_indices:
|
| 324 |
bars[i].set_color('red')
|
|
@@ -343,7 +343,7 @@ def get_random_face_sample(organized_faces_folder, largest_cluster, output_folde
|
|
| 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, (
|
| 347 |
cv2.imwrite(output_path, small_face)
|
| 348 |
|
| 349 |
return output_path
|
|
@@ -402,10 +402,14 @@ def process_video(video_path, num_anomalies, num_components, desired_fps, batch_
|
|
| 402 |
emotion_plots = [
|
| 403 |
plot_emotion(df, 'fear', num_anomalies, 'purple'),
|
| 404 |
plot_emotion(df, 'sad', num_anomalies, 'green'),
|
| 405 |
-
plot_emotion(df, 'angry', num_anomalies, 'orange')
|
|
|
|
|
|
|
|
|
|
| 406 |
]
|
| 407 |
except Exception as e:
|
| 408 |
-
return f"Error generating plots: {str(e)}", None, None, None, None, None, None
|
|
|
|
| 409 |
|
| 410 |
# Get a random face sample
|
| 411 |
face_sample = get_random_face_sample(organized_faces_folder, largest_cluster, output_folder)
|
|
@@ -418,11 +422,11 @@ def process_video(video_path, num_anomalies, num_components, desired_fps, batch_
|
|
| 418 |
results += "\n".join([f"{score:.4f} at {timecode}" for score, timecode in
|
| 419 |
zip(anomaly_scores_comp[top_indices_comp], df['Timecode'].iloc[top_indices_comp].values)])
|
| 420 |
|
| 421 |
-
for emotion in ['fear', 'sad', 'angry']:
|
| 422 |
top_indices = np.argsort(df[emotion].values)[-num_anomalies:][::-1]
|
| 423 |
results += f"\n\nTop {num_anomalies} {emotion.capitalize()} Scores:\n"
|
| 424 |
results += "\n".join([f"{df[emotion].iloc[i]:.4f} at {df['Timecode'].iloc[i]}" for i in top_indices])
|
| 425 |
-
|
| 426 |
return results, face_sample, anomaly_plot_all, anomaly_plot_comp, *emotion_plots
|
| 427 |
|
| 428 |
# Gradio interface
|
|
@@ -430,7 +434,7 @@ iface = gr.Interface(
|
|
| 430 |
fn=process_video,
|
| 431 |
inputs=[
|
| 432 |
gr.Video(),
|
| 433 |
-
gr.Slider(minimum=1, maximum=20, step=1, value=
|
| 434 |
gr.Slider(minimum=2, maximum=5, step=1, value=3, label="Number of Components"),
|
| 435 |
gr.Slider(minimum=1, maximum=30, step=1, value=20, label="Desired FPS"),
|
| 436 |
gr.Slider(minimum=1, maximum=64, step=1, value=16, label="Batch Size")
|
|
@@ -442,7 +446,10 @@ iface = gr.Interface(
|
|
| 442 |
gr.Plot(label="Anomaly Scores (Components Only)"),
|
| 443 |
gr.Plot(label="Fear Anomalies"),
|
| 444 |
gr.Plot(label="Sad Anomalies"),
|
| 445 |
-
gr.Plot(label="Angry Anomalies")
|
|
|
|
|
|
|
|
|
|
| 446 |
],
|
| 447 |
title="Facial Expressions Anomaly Detection",
|
| 448 |
description="""
|
|
|
|
| 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.9, color=color)
|
| 322 |
top_indices = np.argsort(values)[-num_anomalies:][::-1]
|
| 323 |
for i in top_indices:
|
| 324 |
bars[i].set_color('red')
|
|
|
|
| 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, (10, 10)) # Resize to NxN pixels
|
| 347 |
cv2.imwrite(output_path, small_face)
|
| 348 |
|
| 349 |
return output_path
|
|
|
|
| 402 |
emotion_plots = [
|
| 403 |
plot_emotion(df, 'fear', num_anomalies, 'purple'),
|
| 404 |
plot_emotion(df, 'sad', num_anomalies, 'green'),
|
| 405 |
+
plot_emotion(df, 'angry', num_anomalies, 'orange'),
|
| 406 |
+
plot_emotion(df, 'happy', num_anomalies, 'darkblue'),
|
| 407 |
+
plot_emotion(df, 'surprise', num_anomalies, 'gold'),
|
| 408 |
+
plot_emotion(df, 'neutral', num_anomalies, 'grey')
|
| 409 |
]
|
| 410 |
except Exception as e:
|
| 411 |
+
return f"Error generating plots: {str(e)}", None, None, None, None, None, None, None, None, None
|
| 412 |
+
|
| 413 |
|
| 414 |
# Get a random face sample
|
| 415 |
face_sample = get_random_face_sample(organized_faces_folder, largest_cluster, output_folder)
|
|
|
|
| 422 |
results += "\n".join([f"{score:.4f} at {timecode}" for score, timecode in
|
| 423 |
zip(anomaly_scores_comp[top_indices_comp], df['Timecode'].iloc[top_indices_comp].values)])
|
| 424 |
|
| 425 |
+
for emotion in ['fear', 'sad', 'angry', 'happy', 'surprise', 'neutral']:
|
| 426 |
top_indices = np.argsort(df[emotion].values)[-num_anomalies:][::-1]
|
| 427 |
results += f"\n\nTop {num_anomalies} {emotion.capitalize()} Scores:\n"
|
| 428 |
results += "\n".join([f"{df[emotion].iloc[i]:.4f} at {df['Timecode'].iloc[i]}" for i in top_indices])
|
| 429 |
+
|
| 430 |
return results, face_sample, anomaly_plot_all, anomaly_plot_comp, *emotion_plots
|
| 431 |
|
| 432 |
# Gradio interface
|
|
|
|
| 434 |
fn=process_video,
|
| 435 |
inputs=[
|
| 436 |
gr.Video(),
|
| 437 |
+
gr.Slider(minimum=1, maximum=20, step=1, value=5, label="Number of Anomalies"),
|
| 438 |
gr.Slider(minimum=2, maximum=5, step=1, value=3, label="Number of Components"),
|
| 439 |
gr.Slider(minimum=1, maximum=30, step=1, value=20, label="Desired FPS"),
|
| 440 |
gr.Slider(minimum=1, maximum=64, step=1, value=16, label="Batch Size")
|
|
|
|
| 446 |
gr.Plot(label="Anomaly Scores (Components Only)"),
|
| 447 |
gr.Plot(label="Fear Anomalies"),
|
| 448 |
gr.Plot(label="Sad Anomalies"),
|
| 449 |
+
gr.Plot(label="Angry Anomalies"),
|
| 450 |
+
gr.Plot(label="Happy Anomalies"),
|
| 451 |
+
gr.Plot(label="Surprise Anomalies"),
|
| 452 |
+
gr.Plot(label="Neutral Anomalies")
|
| 453 |
],
|
| 454 |
title="Facial Expressions Anomaly Detection",
|
| 455 |
description="""
|