Spaces:
Sleeping
Sleeping
app.py update
Browse files
app.py
CHANGED
@@ -1,108 +1,79 @@
|
|
1 |
import os
|
2 |
import cv2
|
3 |
-
import io
|
4 |
import tempfile
|
5 |
import pandas as pd
|
6 |
import matplotlib.pyplot as plt
|
7 |
-
from
|
8 |
-
from deepface import DeepFace
|
9 |
import gradio as gr
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
summary += f"**Duration:** {df['timestamp'].max():.1f} seconds\n\n"
|
80 |
-
summary += "**Average Emotions:**\n"
|
81 |
-
for e, v in avg.items():
|
82 |
-
summary += f"• {e.title()}: {v:.1f}%\n"
|
83 |
-
|
84 |
-
return chart, summary
|
85 |
-
|
86 |
-
except Exception as e:
|
87 |
-
return None, f"Error: {str(e)}"
|
88 |
-
|
89 |
-
def create_interface():
|
90 |
-
detector = EmotionDetector()
|
91 |
-
def process(video, rate): return detector.detect_emotions_video(video, rate)
|
92 |
-
|
93 |
-
return gr.Interface(
|
94 |
-
fn=process,
|
95 |
-
inputs=[
|
96 |
-
gr.Video(label="Upload Video"),
|
97 |
-
gr.Slider(minimum=1, maximum=60, step=1, value=30, label="Sample Rate")
|
98 |
-
],
|
99 |
-
outputs=[
|
100 |
-
gr.Image(type="pil", label="Emotion Chart"),
|
101 |
-
gr.Textbox(label="Summary")
|
102 |
-
],
|
103 |
-
title="Emotion Detection from Video",
|
104 |
-
description="Upload a video to analyze facial emotions using DeepFace."
|
105 |
-
)
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
-
|
|
|
1 |
import os
|
2 |
import cv2
|
|
|
3 |
import tempfile
|
4 |
import pandas as pd
|
5 |
import matplotlib.pyplot as plt
|
6 |
+
from deepface import DeepFace
|
|
|
7 |
import gradio as gr
|
8 |
|
9 |
+
def analyze_video(video_path):
|
10 |
+
cap = cv2.VideoCapture(video_path)
|
11 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
12 |
+
frames = []
|
13 |
+
count = 0
|
14 |
+
|
15 |
+
while True:
|
16 |
+
ret, frame = cap.read()
|
17 |
+
if not ret:
|
18 |
+
break
|
19 |
+
if count % int(fps * 2) == 0: # sample every 2 seconds
|
20 |
+
frames.append(frame)
|
21 |
+
count += 1
|
22 |
+
cap.release()
|
23 |
+
|
24 |
+
emotions_summary = []
|
25 |
+
for i, frame in enumerate(frames):
|
26 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmpfile:
|
27 |
+
cv2.imwrite(tmpfile.name, frame)
|
28 |
+
try:
|
29 |
+
result = DeepFace.analyze(
|
30 |
+
img_path=tmpfile.name,
|
31 |
+
actions=['emotion'],
|
32 |
+
enforce_detection=False,
|
33 |
+
detector_backend='opencv',
|
34 |
+
prog_backend='pytorch'
|
35 |
+
)
|
36 |
+
if isinstance(result, list):
|
37 |
+
emotions_summary.append(result[0]['emotion'])
|
38 |
+
else:
|
39 |
+
emotions_summary.append(result['emotion'])
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Frame {i} skipped: {e}")
|
42 |
+
finally:
|
43 |
+
os.unlink(tmpfile.name)
|
44 |
+
|
45 |
+
df = pd.DataFrame(emotions_summary)
|
46 |
+
emotion_means = df.mean().sort_values(ascending=False)
|
47 |
+
|
48 |
+
# Plot
|
49 |
+
plt.figure(figsize=(10, 5))
|
50 |
+
emotion_means.plot(kind='bar', color='skyblue')
|
51 |
+
plt.title("Average Emotions in Video")
|
52 |
+
plt.ylabel("Probability")
|
53 |
+
plt.xticks(rotation=45)
|
54 |
+
plt.tight_layout()
|
55 |
+
plt.savefig("emotion_chart.png")
|
56 |
+
plt.close()
|
57 |
+
|
58 |
+
summary = "**Video Analysis Complete**\n"
|
59 |
+
summary += f"**Frames Analyzed:** {len(frames)}\n"
|
60 |
+
summary += f"**Duration:** {round(len(frames) * 2.0, 1)} seconds\n\n"
|
61 |
+
summary += "**Average Emotions:**\n"
|
62 |
+
for emotion, value in emotion_means.items():
|
63 |
+
summary += f"• {emotion.capitalize()}: {value:.1f}%\n"
|
64 |
+
|
65 |
+
return "emotion_chart.png", summary
|
66 |
+
|
67 |
+
demo = gr.Interface(
|
68 |
+
fn=analyze_video,
|
69 |
+
inputs=gr.Video(label="Upload a Video"),
|
70 |
+
outputs=[
|
71 |
+
gr.Image(label="Emotion Chart"),
|
72 |
+
gr.Markdown(label="Emotion Summary")
|
73 |
+
],
|
74 |
+
title="Emotion Recognition from Video",
|
75 |
+
description="Upload a short video. The app analyzes emotions every 2 seconds using DeepFace and PyTorch."
|
76 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
if __name__ == "__main__":
|
79 |
+
demo.launch()
|