File size: 11,843 Bytes
24c8903
 
b5583ea
24c8903
b5583ea
 
24c8903
 
b5583ea
 
 
24c8903
b5583ea
 
 
 
 
 
24c8903
b5583ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24c8903
b5583ea
 
 
24c8903
b5583ea
 
 
 
 
24c8903
b5583ea
 
 
 
 
 
 
 
 
 
 
 
 
 
24c8903
b5583ea
 
24c8903
 
b5583ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24c8903
b5583ea
24c8903
 
b5583ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24c8903
b5583ea
 
24c8903
b5583ea
24c8903
b5583ea
 
 
 
 
 
 
 
 
 
24c8903
b5583ea
 
 
 
 
 
 
 
 
 
24c8903
b5583ea
 
 
 
 
 
 
 
24c8903
b5583ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24c8903
b5583ea
 
 
 
 
24c8903
b5583ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24c8903
 
b5583ea
24c8903
b5583ea
24c8903
b5583ea
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import gradio as gr
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from deepface import DeepFace
import os
import tempfile
from PIL import Image
import io
import base64

class EmotionDetector:
    def __init__(self):
        self.emotions = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
        
    def detect_emotions_image(self, image):
        """Detect emotions in a single image"""
        try:
            if image is None:
                return None, "No image provided"
            
            # Convert PIL Image to numpy array if needed
            if isinstance(image, Image.Image):
                image = np.array(image)
            
            # Convert RGB to BGR for OpenCV
            if len(image.shape) == 3 and image.shape[2] == 3:
                image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            else:
                image_bgr = image
            
            # Save temporary image for DeepFace
            with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp_file:
                cv2.imwrite(tmp_file.name, image_bgr)
                temp_path = tmp_file.name
            
            try:
                # Analyze emotions using DeepFace
                result = DeepFace.analyze(
                    img_path=temp_path,
                    actions=['emotion'],
                    enforce_detection=False,
                    detector_backend='opencv'
                )
                
                # Handle both single face and multiple faces results
                if isinstance(result, list):
                    emotions_data = result[0]['emotion']
                else:
                    emotions_data = result['emotion']
                
                # Create emotion chart
                emotion_df = pd.DataFrame(list(emotions_data.items()), 
                                        columns=['Emotion', 'Confidence'])
                emotion_df = emotion_df.sort_values('Confidence', ascending=True)
                
                # Create matplotlib plot
                plt.figure(figsize=(10, 6))
                bars = plt.barh(emotion_df['Emotion'], emotion_df['Confidence'])
                plt.xlabel('Confidence (%)')
                plt.title('Emotion Detection Results')
                plt.grid(axis='x', alpha=0.3)
                
                # Color bars based on emotion
                colors = {
                    'happy': '#FFD700',
                    'sad': '#4169E1', 
                    'angry': '#DC143C',
                    'fear': '#800080',
                    'surprise': '#FF8C00',
                    'disgust': '#228B22',
                    'neutral': '#708090'
                }
                
                for bar, emotion in zip(bars, emotion_df['Emotion']):
                    bar.set_color(colors.get(emotion, '#708090'))
                
                plt.tight_layout()
                
                # Save plot to bytes
                img_buffer = io.BytesIO()
                plt.savefig(img_buffer, format='png', dpi=150, bbox_inches='tight')
                img_buffer.seek(0)
                plt.close()
                
                # Convert to PIL Image
                chart_image = Image.open(img_buffer)
                
                # Get dominant emotion
                dominant_emotion = max(emotions_data, key=emotions_data.get)
                confidence = emotions_data[dominant_emotion]
                
                result_text = f"**Dominant Emotion:** {dominant_emotion.title()}\n"
                result_text += f"**Confidence:** {confidence:.1f}%\n\n"
                result_text += "**All Emotions:**\n"
                
                for emotion, conf in sorted(emotions_data.items(), key=lambda x: x[1], reverse=True):
                    result_text += f"β€’ {emotion.title()}: {conf:.1f}%\n"
                
                return chart_image, result_text
                
            finally:
                # Clean up temporary file
                if os.path.exists(temp_path):
                    os.unlink(temp_path)
                    
        except Exception as e:
            error_msg = f"Error analyzing image: {str(e)}"
            print(error_msg)  # For debugging
            return None, error_msg
    
    def detect_emotions_video(self, video_path, sample_rate=30):
        """Detect emotions in video by sampling frames"""
        try:
            if video_path is None:
                return None, "No video provided"
            
            cap = cv2.VideoCapture(video_path)
            frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
            fps = int(cap.get(cv2.CAP_PROP_FPS))
            
            if frame_count == 0:
                return None, "Invalid video file"
            
            # Sample frames every 'sample_rate' frames
            frame_indices = range(0, frame_count, sample_rate)
            emotions_over_time = []
            
            for frame_idx in frame_indices:
                cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
                ret, frame = cap.read()
                
                if not ret:
                    continue
                
                try:
                    # Save frame temporarily
                    with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp_file:
                        cv2.imwrite(tmp_file.name, frame)
                        temp_path = tmp_file.name
                    
                    # Analyze frame
                    result = DeepFace.analyze(
                        img_path=temp_path,
                        actions=['emotion'],
                        enforce_detection=False,
                        detector_backend='opencv'
                    )
                    
                    if isinstance(result, list):
                        emotions_data = result[0]['emotion']
                    else:
                        emotions_data = result['emotion']
                    
                    # Add timestamp
                    timestamp = frame_idx / fps
                    emotions_data['timestamp'] = timestamp
                    emotions_over_time.append(emotions_data)
                    
                    # Clean up
                    os.unlink(temp_path)
                    
                except Exception as e:
                    print(f"Error processing frame {frame_idx}: {e}")
                    continue
            
            cap.release()
            
            if not emotions_over_time:
                return None, "No emotions detected in video"
            
            # Create DataFrame for plotting
            df = pd.DataFrame(emotions_over_time)
            
            # Plot emotions over time
            plt.figure(figsize=(12, 8))
            
            for emotion in self.emotions:
                if emotion in df.columns:
                    plt.plot(df['timestamp'], df[emotion], label=emotion.title(), linewidth=2)
            
            plt.xlabel('Time (seconds)')
            plt.ylabel('Confidence (%)')
            plt.title('Emotions Over Time')
            plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
            plt.grid(True, alpha=0.3)
            plt.tight_layout()
            
            # Save plot
            img_buffer = io.BytesIO()
            plt.savefig(img_buffer, format='png', dpi=150, bbox_inches='tight')
            img_buffer.seek(0)
            plt.close()
            
            chart_image = Image.open(img_buffer)
            
            # Calculate average emotions
            avg_emotions = df[self.emotions].mean().sort_values(ascending=False)
            
            result_text = f"**Video Analysis Complete**\n"
            result_text += f"**Frames Analyzed:** {len(emotions_over_time)}\n"
            result_text += f"**Duration:** {df['timestamp'].max():.1f} seconds\n\n"
            result_text += "**Average Emotions:**\n"
            
            for emotion, confidence in avg_emotions.items():
                result_text += f"β€’ {emotion.title()}: {confidence:.1f}%\n"
            
            return chart_image, result_text
            
        except Exception as e:
            return None, f"Error processing video: {str(e)}"

# Initialize detector
detector = EmotionDetector()

# Create Gradio interface
def create_interface():
    with gr.Blocks(title="Emotion Detection App", theme=gr.themes.Soft()) as demo:
        gr.Markdown(
            """
            # 🎭 Emotion Detection App
            
            Upload an image or video to detect emotions using AI. This app uses DeepFace for accurate emotion recognition.
            
            **Supported emotions:** Happy, Sad, Angry, Fear, Surprise, Disgust, Neutral
            """
        )
        
        with gr.Tabs():
            # Image Analysis Tab
            with gr.Tab("πŸ“Έ Image Analysis"):
                with gr.Row():
                    with gr.Column():
                        image_input = gr.Image(
                            label="Upload Image",
                            type="pil"
                        )
                        image_button = gr.Button("Analyze Emotions", variant="primary")
                    
                    with gr.Column():
                        image_chart = gr.Image(label="Emotion Chart")
                        image_results = gr.Markdown(label="Results")
                
                image_button.click(
                    fn=detector.detect_emotions_image,
                    inputs=[image_input],
                    outputs=[image_chart, image_results]
                )
            
            # Video Analysis Tab
            with gr.Tab("πŸŽ₯ Video Analysis"):
                with gr.Row():
                    with gr.Column():
                        video_input = gr.Video(label="Upload Video")
                        with gr.Row():
                            sample_rate = gr.Slider(
                                minimum=10,
                                maximum=60,
                                value=30,
                                step=5,
                                label="Frame Sampling Rate"
                            )
                        video_button = gr.Button("Analyze Video", variant="primary")
                    
                    with gr.Column():
                        video_chart = gr.Image(label="Emotions Over Time")
                        video_results = gr.Markdown(label="Results")
                
                video_button.click(
                    fn=detector.detect_emotions_video,
                    inputs=[video_input, sample_rate],
                    outputs=[video_chart, video_results]
                )
        
        # Examples
        gr.Markdown("### πŸ“‹ Instructions")
        gr.Markdown(
            """
            **For Images:**
            - Upload any image with visible faces
            - The app will detect and analyze emotions
            - Results show confidence percentages for each emotion
            
            **For Videos:**
            - Upload video files (MP4, AVI, MOV, etc.)
            - Adjust frame sampling rate to balance speed vs accuracy
            - Lower values = more frames analyzed = more accurate but slower
            - Higher values = fewer frames analyzed = faster but less detailed
            
            **Tips:**
            - Ensure faces are clearly visible and well-lit
            - The app works best with front-facing faces
            - Multiple faces in one image/video are supported
            """
        )
    
    return demo

# Launch the app
if __name__ == "__main__":
    demo = create_interface()
    demo.launch()