Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,39 @@
|
|
| 1 |
-
import
|
| 2 |
from deepface import DeepFace
|
| 3 |
-
import
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
if
|
| 8 |
-
return "β No video file
|
| 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 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
emotion = frame.get("dominant_emotion")
|
| 28 |
-
if emotion:
|
| 29 |
-
emotion_counts[emotion] = emotion_counts.get(emotion, 0) + 1
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 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 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
from deepface import DeepFace
|
| 3 |
+
import gradio as gr
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
def analyze_emotion_from_video(video_path):
|
| 7 |
+
if not video_path or not os.path.exists(video_path):
|
| 8 |
+
return "β Error: No video file found or path is invalid."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
cap = cv2.VideoCapture(video_path)
|
| 11 |
+
if not cap.isOpened():
|
| 12 |
+
return "β Error: Could not open the video file."
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 15 |
+
middle_frame_index = frame_count // 2
|
| 16 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame_index)
|
| 17 |
+
success, frame = cap.read()
|
| 18 |
|
| 19 |
+
if not success or frame is None:
|
| 20 |
+
return "β Error: Could not read a valid frame from the video."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
analysis = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)
|
| 24 |
+
dominant_emotion = analysis[0]['dominant_emotion']
|
| 25 |
+
return f"β
Dominant Emotion: {dominant_emotion}"
|
| 26 |
except Exception as e:
|
| 27 |
return f"β Error during analysis: {str(e)}"
|
| 28 |
+
finally:
|
| 29 |
+
cap.release()
|
| 30 |
+
|
| 31 |
+
interface = gr.Interface(
|
| 32 |
+
fn=analyze_emotion_from_video,
|
| 33 |
+
inputs=gr.Video(label="Upload a Video File"),
|
| 34 |
+
outputs=gr.Textbox(label="Emotion Analysis Result"),
|
| 35 |
+
title="π Video Emotion Detection",
|
| 36 |
+
description="Upload a short video with a visible face to detect the dominant emotion using DeepFace."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
interface.launch()
|