File size: 1,895 Bytes
f0f3558
2804ec4
cdd93c7
d5a7986
 
cdd93c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b77d03b
1
2
3
4
5
6
7
8
9
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
import gradio as gr
from deepface import DeepFace
from transformers import pipeline
import tempfile
import cv2
import moviepy.editor as mp
def analyze_text(text):
    classifier = pipeline("sentiment-analysis")
    return classifier(text)[0]['label']
def analyze_video_emotion(video_file):
    try:
        # Save the uploaded video to a temp file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
            tmp.write(video_file.read())
            tmp_path = tmp.name

        # Extract frames using MoviePy (more reliable than OpenCV alone)
        video = mp.VideoFileClip(tmp_path)
        frames = list(video.iter_frames())
        
        emotions = []
        for frame in frames[:60]:  # Limit to first 60 frames
            try:
                # Use DeepFace for emotion detection
                result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)
                emotions.append(result[0]['dominant_emotion'])
            except Exception as e:
                print("Error analyzing frame:", e)

        if emotions:
            # Return the most common emotion
            return max(set(emotions), key=emotions.count)
        else:
            return "No face detected"

    except Exception as e:
        print("Error processing video:", e)
        return "Error processing video file"

def process_all(text_input, video_input):
    text_result = analyze_text(text_input)
    video_result = analyze_video_emotion(video_input)
    return f"Text Sentiment: {text_result}\nFacial Emotion: {video_result}"

iface = gr.Interface(
    fn=process_all,
    inputs=[
        gr.Textbox(label="Enter Social Media Text"),
        gr.Video(label="Upload a Video Clip")
    ],
    outputs="text",
    title="Emotion & Sentiment Decoder",
    description="Analyzes social media text & facial expressions from video."
)
iface.launch()