Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from deepface import DeepFace
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
def analyze_video(
|
| 6 |
-
if
|
| 7 |
-
return "β
|
| 8 |
-
|
| 9 |
-
# Gradio provides a tempfile.NamedTemporaryFile, use .name to get the actual path
|
| 10 |
-
video_path = video_file.name
|
| 11 |
|
| 12 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
result = DeepFace.analyze(
|
| 14 |
video_path,
|
| 15 |
actions=["emotion"],
|
| 16 |
-
|
| 17 |
-
|
| 18 |
)
|
| 19 |
|
|
|
|
| 20 |
emotion_counts = {}
|
| 21 |
-
for
|
| 22 |
-
|
| 23 |
-
if
|
| 24 |
-
emotion_counts[
|
| 25 |
|
| 26 |
total_frames = sum(emotion_counts.values())
|
| 27 |
-
|
| 28 |
-
return "β οΈ No faces or emotions detected in the video."
|
| 29 |
-
|
| 30 |
-
output = "π **Detected Emotions Summary:**\n"
|
| 31 |
for emotion, count in sorted(emotion_counts.items(), key=lambda x: x[1], reverse=True):
|
| 32 |
-
|
| 33 |
-
output += f"- {emotion}: {count} frames ({percentage:.2f}%)\n"
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
else:
|
| 38 |
-
output += "\nπ’ **No
|
| 39 |
|
| 40 |
return output
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
-
return f"β Error
|
| 44 |
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
gr.Markdown("
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
result_output = gr.Textbox(label="π Analysis Results", lines=12)
|
| 50 |
analyze_btn = gr.Button("π Analyze Video")
|
| 51 |
|
| 52 |
-
analyze_btn.click(analyze_video, inputs=video_input, outputs=
|
| 53 |
|
| 54 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from deepface import DeepFace
|
| 3 |
+
import tempfile
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
def analyze_video(video):
|
| 7 |
+
if video is None:
|
| 8 |
+
return "β No video file uploaded."
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
try:
|
| 11 |
+
# Save video to a temporary path
|
| 12 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
|
| 13 |
+
tmp.write(video.read())
|
| 14 |
+
video_path = tmp.name
|
| 15 |
+
|
| 16 |
+
# Analyze video for emotion (can add other actions like age, gender, race)
|
| 17 |
result = DeepFace.analyze(
|
| 18 |
video_path,
|
| 19 |
actions=["emotion"],
|
| 20 |
+
detector_backend="retinaface",
|
| 21 |
+
enforce_detection=False
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Summarize emotion frequency
|
| 25 |
emotion_counts = {}
|
| 26 |
+
for frame in result:
|
| 27 |
+
emotion = frame.get("dominant_emotion")
|
| 28 |
+
if emotion:
|
| 29 |
+
emotion_counts[emotion] = emotion_counts.get(emotion, 0) + 1
|
| 30 |
|
| 31 |
total_frames = sum(emotion_counts.values())
|
| 32 |
+
output = "π **Emotion Analysis:**\n"
|
|
|
|
|
|
|
|
|
|
| 33 |
for emotion, count in sorted(emotion_counts.items(), key=lambda x: x[1], reverse=True):
|
| 34 |
+
output += f"- {emotion}: {count} frames ({(count/total_frames)*100:.1f}%)\n"
|
|
|
|
| 35 |
|
| 36 |
+
# Basic lie detection logic (can be customized)
|
| 37 |
+
if emotion_counts.get("fear", 0)/total_frames > 0.2 or emotion_counts.get("surprise", 0)/total_frames > 0.2:
|
| 38 |
+
output += "\nπ΄ **Lie Detected: High fear/surprise levels**"
|
| 39 |
else:
|
| 40 |
+
output += "\nπ’ **No lie patterns detected**"
|
| 41 |
|
| 42 |
return output
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
+
return f"β Error during analysis: {str(e)}"
|
| 46 |
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("## π΅οΈ Lie Detection from Video\nUpload a short face-visible video to detect emotional indicators.")
|
| 49 |
+
video_input = gr.Video(label="π₯ Upload MP4 Video")
|
| 50 |
+
output_box = gr.Textbox(label="π Results", lines=10)
|
|
|
|
| 51 |
analyze_btn = gr.Button("π Analyze Video")
|
| 52 |
|
| 53 |
+
analyze_btn.click(fn=analyze_video, inputs=video_input, outputs=output_box)
|
| 54 |
|
| 55 |
demo.launch()
|