Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,8 @@ import matplotlib.pyplot as plt
|
|
| 12 |
input_labels_X = "Happy Face, Sad Face, Angry Face, Fear Face, Disgust Face, Contempt Face, Nervous Face, Curious Face, Flirtatious Face, Ashamed Face, Bored Face, Confused Face, Calm Face, Proud Face, Guilty Face, Annoyed Face, Desperate Face, Jealous Face, Embarrassed Face, Uncomfortable Face, Helpless Face, Shy Face, Infatuated Face, Apathetic Face, Neutral Face"
|
| 13 |
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
-
model, preprocess = clip.load("ViT-
|
| 16 |
-
current_model_name = "ViT-
|
| 17 |
|
| 18 |
# Initialize MTCNN for face detection
|
| 19 |
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.95, 0.95, 0.95], min_face_size=80)
|
|
@@ -69,13 +69,21 @@ def process_frame(frame, selected_model):
|
|
| 69 |
top_five_labels_probs = combined_labels_probs[:5]
|
| 70 |
|
| 71 |
# Create a bar graph
|
| 72 |
-
fig, ax = plt.subplots(figsize=(
|
|
|
|
|
|
|
| 73 |
labels, probs = zip(*top_five_labels_probs)
|
| 74 |
bars = ax.barh(labels, probs, color=plt.cm.tab20.colors)
|
| 75 |
ax.set_xlabel('Probability')
|
| 76 |
ax.set_title('Top 5 Emotions')
|
| 77 |
ax.invert_yaxis() # Invert y-axis to have the highest probability at the top
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
# Convert plot to image
|
| 80 |
fig.canvas.draw()
|
| 81 |
plot_img = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8)
|
|
@@ -115,7 +123,7 @@ with gr.Blocks() as app:
|
|
| 115 |
with gr.Row():
|
| 116 |
with gr.Column():
|
| 117 |
with gr.Tab("Video"):
|
| 118 |
-
model_dropdown_video = gr.Dropdown(choices=["ViT-B/32", "ViT-B/16", "ViT-L/14"], label="Model", value="ViT-
|
| 119 |
gr.Markdown("Upload a video to detect faces and recognize emotions.")
|
| 120 |
video_input = gr.Video()
|
| 121 |
frame_slider = gr.Slider(minimum=0, maximum=100, step=1, label="Frame Index", value=0)
|
|
|
|
| 12 |
input_labels_X = "Happy Face, Sad Face, Angry Face, Fear Face, Disgust Face, Contempt Face, Nervous Face, Curious Face, Flirtatious Face, Ashamed Face, Bored Face, Confused Face, Calm Face, Proud Face, Guilty Face, Annoyed Face, Desperate Face, Jealous Face, Embarrassed Face, Uncomfortable Face, Helpless Face, Shy Face, Infatuated Face, Apathetic Face, Neutral Face"
|
| 13 |
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
model, preprocess = clip.load("ViT-B/16", device=device)
|
| 16 |
+
current_model_name = "ViT-B/16"
|
| 17 |
|
| 18 |
# Initialize MTCNN for face detection
|
| 19 |
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.95, 0.95, 0.95], min_face_size=80)
|
|
|
|
| 69 |
top_five_labels_probs = combined_labels_probs[:5]
|
| 70 |
|
| 71 |
# Create a bar graph
|
| 72 |
+
fig, ax = plt.subplots(figsize=(10, 5), dpi=300) # Increased figure size
|
| 73 |
+
plt.subplots_adjust(left=0.3) # Adjust left margin
|
| 74 |
+
|
| 75 |
labels, probs = zip(*top_five_labels_probs)
|
| 76 |
bars = ax.barh(labels, probs, color=plt.cm.tab20.colors)
|
| 77 |
ax.set_xlabel('Probability')
|
| 78 |
ax.set_title('Top 5 Emotions')
|
| 79 |
ax.invert_yaxis() # Invert y-axis to have the highest probability at the top
|
| 80 |
|
| 81 |
+
# Adjust x-axis labels
|
| 82 |
+
ax.set_xticklabels(ax.get_xticks(), rotation=0, ha='center')
|
| 83 |
+
|
| 84 |
+
# Ensure all labels are fully visible
|
| 85 |
+
plt.tight_layout()
|
| 86 |
+
|
| 87 |
# Convert plot to image
|
| 88 |
fig.canvas.draw()
|
| 89 |
plot_img = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8)
|
|
|
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Column():
|
| 125 |
with gr.Tab("Video"):
|
| 126 |
+
model_dropdown_video = gr.Dropdown(choices=["ViT-B/32", "ViT-B/16", "ViT-L/14"], label="Model", value="ViT-B/16")
|
| 127 |
gr.Markdown("Upload a video to detect faces and recognize emotions.")
|
| 128 |
video_input = gr.Video()
|
| 129 |
frame_slider = gr.Slider(minimum=0, maximum=100, step=1, label="Frame Index", value=0)
|