File size: 28,517 Bytes
ba069c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e03307
b9341c8
 
 
 
 
 
 
 
 
 
 
ba069c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16698ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba069c3
 
 
 
16698ae
 
 
 
 
ba069c3
 
 
16698ae
ba069c3
 
 
 
7e03307
 
 
 
 
ba069c3
 
7e03307
 
ba069c3
 
7e03307
 
 
 
 
ba069c3
7e03307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba069c3
 
 
 
7e03307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba069c3
 
 
 
16698ae
ba069c3
 
7e03307
 
16698ae
 
ba069c3
16698ae
b9341c8
 
16698ae
b9341c8
 
 
 
 
 
 
 
16698ae
b9341c8
 
 
 
 
 
 
 
 
16698ae
 
b9341c8
 
ba069c3
 
 
 
7e03307
ba069c3
 
 
 
 
 
7e03307
ba069c3
7e03307
 
 
 
ba069c3
 
 
7e03307
 
 
 
 
 
ba069c3
 
 
 
 
 
7e03307
 
ba069c3
7e03307
 
 
ba069c3
 
7e03307
 
 
 
ba069c3
 
 
7e03307
ba069c3
 
 
7e03307
 
 
 
 
ba069c3
 
 
 
 
 
7e03307
 
ba069c3
 
 
 
 
 
 
7e03307
 
ba069c3
 
7e03307
ba069c3
 
 
b9341c8
 
ba069c3
7e03307
 
 
 
 
 
 
 
ba069c3
 
 
b9341c8
7e03307
 
b9341c8
7e03307
b9341c8
 
 
 
7e03307
ba069c3
 
7e03307
b9341c8
7e03307
 
ba069c3
1732572
b9341c8
 
 
 
 
 
 
 
 
 
7e03307
b9341c8
 
 
7e03307
b9341c8
 
7e03307
 
 
 
 
b9341c8
1732572
 
 
 
ba069c3
 
7e03307
 
ba069c3
1732572
 
ba069c3
1732572
 
ba069c3
 
 
1732572
 
 
 
ba069c3
1732572
 
 
7e03307
 
1732572
 
7e03307
1732572
 
 
 
 
 
 
 
 
 
 
7e03307
1732572
7e03307
1732572
 
7e03307
ba069c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e03307
 
ba069c3
 
7e03307
ba069c3
 
 
7e03307
ba069c3
7e03307
ba069c3
 
 
 
 
 
7e03307
 
 
 
 
 
 
 
ba069c3
 
 
 
 
 
 
 
 
7e03307
ba069c3
7e03307
 
 
 
 
ba069c3
7e03307
 
 
 
 
ba069c3
 
7e03307
 
 
 
 
 
 
 
 
 
 
 
 
 
ba069c3
 
 
 
7e03307
ba069c3
7e03307
ba069c3
 
 
 
 
 
 
7e03307
 
 
ba069c3
 
 
 
7e03307
ba069c3
 
 
 
 
 
 
7e03307
ba069c3
7e03307
ba069c3
7e03307
 
ba069c3
 
 
 
7e03307
ba069c3
 
7e03307
ba069c3
 
 
 
1732572
 
7e03307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9341c8
7e03307
1732572
 
7e03307
 
1732572
 
7e03307
 
 
 
1732572
ba069c3
 
 
 
 
7e03307
ba069c3
 
 
7e03307
ba069c3
 
 
7e03307
ba069c3
 
 
 
 
 
 
 
 
 
7e03307
ba069c3
 
7e03307
ba069c3
 
 
7e03307
ba069c3
 
7e03307
ba069c3
7e03307
 
 
 
 
ba069c3
7e03307
 
 
 
ba069c3
7e03307
 
 
 
ba069c3
7e03307
ba069c3
7e03307
 
 
 
 
 
 
ba069c3
7e03307
ba069c3
7e03307
 
 
 
 
ba069c3
7e03307
ba069c3
7e03307
 
 
 
ba069c3
 
 
7e03307
ba069c3
 
 
 
7e03307
 
 
 
 
1732572
 
ba069c3
 
 
 
7e03307
 
 
 
 
 
 
 
ba069c3
 
7e03307
ba069c3
 
7e03307
 
 
ba069c3
7e03307
 
ba069c3
 
 
 
 
 
7e03307
ba069c3
 
 
 
 
 
 
 
 
 
 
 
 
 
1732572
 
 
 
 
 
7e03307
1732572
7e03307
 
 
1732572
 
 
 
 
 
 
 
 
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import gradio as gr
import torch
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from transformers import pipeline
import logging
import time
from typing import Tuple, List, Dict, Optional

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Model configuration
MODEL_NAME = "abhilash88/face-emotion-detection"

# Emotion labels mapping
EMOTION_LABELS = {
    'LABEL_0': 'angry',
    'LABEL_1': 'disgust', 
    'LABEL_2': 'fear',
    'LABEL_3': 'happy',
    'LABEL_4': 'sad',
    'LABEL_5': 'surprise',
    'LABEL_6': 'neutral'
}

# Emotion colors for visualization
EMOTION_COLORS = {
    'angry': '#FF4444',
    'disgust': '#AA4444', 
    'fear': '#4444FF',
    'happy': '#44FF44',
    'sad': '#4444AA',
    'surprise': '#FFAA44',
    'neutral': '#AAAAAA'
}

# Global variables for model
emotion_classifier = None
face_cascade = None

def load_models():
    """Load the emotion detection model and face cascade"""
    global emotion_classifier, face_cascade
    
    try:
        logger.info(f"Loading emotion detection model: {MODEL_NAME}")
        
        # Try loading with different configurations
        try:
            emotion_classifier = pipeline(
                "image-classification",
                model=MODEL_NAME,
                top_k=None
            )
        except Exception as e1:
            logger.warning(f"Failed with top_k=None, trying without: {e1}")
            try:
                emotion_classifier = pipeline(
                    "image-classification",
                    model=MODEL_NAME
                )
            except Exception as e2:
                logger.warning(f"Failed with default config, trying basic setup: {e2}")
                # Fallback to manual model loading
                from transformers import AutoImageProcessor, AutoModelForImageClassification
                processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
                model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
                emotion_classifier = pipeline(
                    "image-classification",
                    model=model,
                    image_processor=processor
                )
        
        logger.info("Emotion detection model loaded successfully")
        
        # Load OpenCV face cascade
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
        
        if face_cascade.empty():
            logger.error("Failed to load face cascade classifier")
            return False
            
        logger.info("Face detection cascade loaded successfully")
        
        return True
        
    except Exception as e:
        logger.error(f"Error loading models: {e}")
        return False

def detect_faces_improved(image: np.ndarray, min_face_size: int = 80) -> List[Tuple[int, int, int, int]]:
    """
    Improved face detection with better parameters to reduce false positives
    and merge overlapping detections
    """
    try:
        gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        
        # Use more strict parameters to reduce false positives
        faces = face_cascade.detectMultiScale(
            gray, 
            scaleFactor=1.05,        # Smaller scale factor for more careful detection
            minNeighbors=8,          # Higher min neighbors to be more strict
            minSize=(min_face_size, min_face_size),  # Larger minimum size
            maxSize=(int(min(image.shape[:2]) * 0.8), int(min(image.shape[:2]) * 0.8)),  # Maximum size
            flags=cv2.CASCADE_SCALE_IMAGE | cv2.CASCADE_DO_CANNY_PRUNING
        )
        
        if len(faces) == 0:
            return []
        
        # Convert to list and merge overlapping detections
        faces_list = faces.tolist()
        merged_faces = merge_overlapping_faces(faces_list)
        
        # Filter faces that are too small relative to image size
        image_area = image.shape[0] * image.shape[1]
        filtered_faces = []
        
        for (x, y, w, h) in merged_faces:
            face_area = w * h
            # Face should be at least 0.5% of image area but not more than 80%
            if 0.005 < (face_area / image_area) < 0.8:
                # Additional validation: check aspect ratio (faces are roughly square)
                aspect_ratio = w / h
                if 0.7 <= aspect_ratio <= 1.4:  # Allow some variance but not extreme rectangles
                    filtered_faces.append((x, y, w, h))
        
        return filtered_faces
        
    except Exception as e:
        logger.error(f"Error detecting faces: {e}")
        return []

def merge_overlapping_faces(faces: List[Tuple[int, int, int, int]], overlap_threshold: float = 0.3) -> List[Tuple[int, int, int, int]]:
    """
    Merge overlapping face detections to avoid duplicates
    """
    if len(faces) <= 1:
        return faces
    
    # Calculate IoU (Intersection over Union) for all pairs
    merged = []
    used = [False] * len(faces)
    
    for i in range(len(faces)):
        if used[i]:
            continue
            
        current_face = faces[i]
        merged_face = list(current_face)
        count = 1
        used[i] = True
        
        for j in range(i + 1, len(faces)):
            if used[j]:
                continue
                
            if calculate_iou(current_face, faces[j]) > overlap_threshold:
                # Merge by averaging coordinates
                merged_face[0] = (merged_face[0] * count + faces[j][0]) // (count + 1)
                merged_face[1] = (merged_face[1] * count + faces[j][1]) // (count + 1)
                merged_face[2] = (merged_face[2] * count + faces[j][2]) // (count + 1)
                merged_face[3] = (merged_face[3] * count + faces[j][3]) // (count + 1)
                count += 1
                used[j] = True
        
        merged.append(tuple(merged_face))
    
    return merged

def calculate_iou(box1: Tuple[int, int, int, int], box2: Tuple[int, int, int, int]) -> float:
    """Calculate Intersection over Union of two bounding boxes"""
    x1, y1, w1, h1 = box1
    x2, y2, w2, h2 = box2
    
    # Calculate intersection
    x_left = max(x1, x2)
    y_top = max(y1, y2)
    x_right = min(x1 + w1, x2 + w2)
    y_bottom = min(y1 + h1, y2 + h2)
    
    if x_right < x_left or y_bottom < y_top:
        return 0.0
    
    intersection = (x_right - x_left) * (y_bottom - y_top)
    
    # Calculate union
    area1 = w1 * h1
    area2 = w2 * h2
    union = area1 + area2 - intersection
    
    return intersection / union if union > 0 else 0.0

def predict_emotion(face_image: Image.Image) -> List[Dict]:
    """Predict emotion for a single face"""
    try:
        if emotion_classifier is None:
            logger.warning("Emotion classifier not loaded, returning neutral")
            return [{"label": "neutral", "score": 1.0}]
        
        # Resize image for better performance and consistency
        face_image = face_image.resize((224, 224))
        
        # The pipeline returns results in different formats depending on configuration
        results = emotion_classifier(face_image)
        
        # Handle different return formats and map labels to emotion names
        processed_results = []
        if isinstance(results, list):
            for result in results:
                if isinstance(result, dict) and 'label' in result and 'score' in result:
                    # Map LABEL_X to actual emotion name
                    emotion_name = EMOTION_LABELS.get(result['label'], result['label'])
                    processed_results.append({
                        'label': emotion_name,
                        'score': result['score']
                    })
        elif isinstance(results, dict):
            # Single prediction
            emotion_name = EMOTION_LABELS.get(results['label'], results['label'])
            processed_results = [{
                'label': emotion_name,
                'score': results['score']
            }]
        
        if not processed_results:
            logger.warning("No valid results, returning neutral")
            return [{"label": "neutral", "score": 1.0}]
            
        return processed_results
            
    except Exception as e:
        logger.error(f"Error predicting emotion: {e}")
        return [{"label": "neutral", "score": 1.0}]

def draw_emotion_results(image: Image.Image, faces: List, emotions: List, confidence_threshold: float = 0.5) -> Image.Image:
    """Draw bounding boxes and emotion labels on the image"""
    try:
        draw = ImageDraw.Draw(image)
        
        # Try to load a font, fallback to default if not available
        try:
            font = ImageFont.truetype("arial.ttf", 20)
        except:
            try:
                font = ImageFont.truetype("DejaVuSans.ttf", 20)
            except:
                font = ImageFont.load_default()
        
        for i, (x, y, w, h) in enumerate(faces):
            if i < len(emotions):
                # Get top emotion above threshold
                valid_emotions = [e for e in emotions[i] if e['score'] >= confidence_threshold]
                if not valid_emotions:
                    continue
                    
                top_emotion = max(valid_emotions, key=lambda x: x['score'])
                emotion_label = top_emotion['label']
                confidence = top_emotion['score']
                
                # Get color for this emotion
                color = EMOTION_COLORS.get(emotion_label, '#FFFFFF')
                
                # Draw bounding box with thicker line
                draw.rectangle([(x, y), (x + w, y + h)], outline=color, width=4)
                
                # Draw emotion label with better formatting
                label_text = f"{emotion_label.upper()}"
                confidence_text = f"{confidence:.1%}"
                
                # Calculate text size for background
                bbox1 = draw.textbbox((0, 0), label_text, font=font)
                bbox2 = draw.textbbox((0, 0), confidence_text, font=font)
                text_width = max(bbox1[2] - bbox1[0], bbox2[2] - bbox2[0]) + 20
                text_height = (bbox1[3] - bbox1[1]) + (bbox2[3] - bbox2[1]) + 15
                
                # Draw background for text
                draw.rectangle(
                    [(x, y - text_height - 10), (x + text_width, y)],
                    fill=color
                )
                
                # Draw emotion label
                draw.text((x + 10, y - text_height - 5), label_text, fill='white', font=font)
                
                # Draw confidence
                draw.text((x + 10, y - text_height + 20), confidence_text, fill='white', font=font)
        
        return image
    except Exception as e:
        logger.error(f"Error drawing results: {e}")
        return image

def process_image(image: Image.Image, confidence_threshold: float = 0.5, min_face_size: int = 80) -> Tuple[Image.Image, str]:
    """Process an image for emotion detection with improved face detection"""
    try:
        if image is None:
            return None, "No image provided"
        
        # Convert PIL to numpy array
        image_np = np.array(image)
        
        # Detect faces with improved method
        faces = detect_faces_improved(image_np, min_face_size)
        
        if not faces:
            return image, "❌ No faces detected in the image. Try adjusting the minimum face size or use an image with clearer faces."
        
        # Process each face
        emotions_list = []
        valid_faces = []
        
        for (x, y, w, h) in faces:
            # Extract face region with some padding
            padding = max(10, min(w, h) // 10)
            x_pad = max(0, x - padding)
            y_pad = max(0, y - padding)
            w_pad = min(image.width - x_pad, w + 2 * padding)
            h_pad = min(image.height - y_pad, h + 2 * padding)
            
            face_region = image.crop((x_pad, y_pad, x_pad + w_pad, y_pad + h_pad))
            
            # Predict emotion
            emotions = predict_emotion(face_region)
            
            # Check if any emotion meets the confidence threshold
            valid_emotions = [e for e in emotions if e['score'] >= confidence_threshold]
            
            if valid_emotions:
                emotions_list.append(emotions)
                valid_faces.append((x, y, w, h))
        
        if not valid_faces:
            return image, f"⚠️ {len(faces)} face(s) detected but no emotions above {confidence_threshold:.1f} confidence threshold. Try lowering the threshold."
        
        # Draw results
        result_image = draw_emotion_results(image.copy(), valid_faces, emotions_list, confidence_threshold)
        
        # Create summary text
        summary_lines = [f"βœ… **Successfully detected {len(valid_faces)} face(s) with confident emotion predictions:**\n"]
        
        for i, emotions in enumerate(emotions_list):
            # Sort emotions by confidence
            sorted_emotions = sorted(emotions, key=lambda x: x['score'], reverse=True)
            top_emotion = sorted_emotions[0]
            
            # Add emoji for emotion
            emotion_emoji = {
                'angry': '😠', 'disgust': '🀒', 'fear': '😨', 
                'happy': '😊', 'sad': '😒', 'surprise': '😲', 'neutral': '😐'
            }.get(top_emotion['label'], '😐')
            
            summary_lines.append(f"**Face {i+1}:** {emotion_emoji} **{top_emotion['label'].title()}** ({top_emotion['score']:.1%} confidence)")
            
            # Add top 3 emotions for detailed analysis
            if len(sorted_emotions) > 1:
                summary_lines.append("  πŸ“Š Other detected emotions:")
                for emotion in sorted_emotions[1:4]:  # Top 3 others
                    if emotion['score'] >= confidence_threshold:
                        emoji = {
                            'angry': '😠', 'disgust': '🀒', 'fear': '😨', 
                            'happy': '😊', 'sad': '😒', 'surprise': '😲', 'neutral': '😐'
                        }.get(emotion['label'], '😐')
                        summary_lines.append(f"     β€’ {emoji} {emotion['label'].title()}: {emotion['score']:.1%}")
            summary_lines.append("")
        
        summary = "\n".join(summary_lines)
        
        return result_image, summary
        
    except Exception as e:
        logger.error(f"Error processing image: {e}")
        return image, f"❌ Error processing image: {str(e)}"

def analyze_emotions_batch(files) -> str:
    """Analyze emotions in multiple uploaded files"""
    try:
        if not files:
            return "No files provided"
        
        all_results = []
        
        for idx, file in enumerate(files):
            try:
                # Open the image file
                image = Image.open(file.name)
                
                # Convert PIL to numpy array
                image_np = np.array(image)
                
                # Detect faces with improved method
                faces = detect_faces_improved(image_np)
                
                if not faces:
                    all_results.append(f"πŸ“ File {idx+1} ({file.name}): No faces detected")
                    continue
                
                # Process each face
                image_emotions = []
                for (x, y, w, h) in faces:
                    # Extract face region
                    face_region = image.crop((x, y, x + w, y + h))
                    
                    # Predict emotion
                    emotions = predict_emotion(face_region)
                    top_emotion = max(emotions, key=lambda x: x['score'])
                    image_emotions.append(f"{top_emotion['label']} ({top_emotion['score']:.1%})")
                
                all_results.append(f"πŸ“ File {idx+1} ({file.name}): {len(faces)} face(s) - {', '.join(image_emotions)}")
                
            except Exception as e:
                all_results.append(f"πŸ“ File {idx+1}: Error processing - {str(e)}")
        
        return "\n".join(all_results)
        
    except Exception as e:
        logger.error(f"Error in batch analysis: {e}")
        return f"Error in batch analysis: {str(e)}"

def get_emotion_statistics(image: Image.Image) -> str:
    """Get detailed emotion statistics for an image"""
    try:
        if image is None:
            return "No image provided"
        
        # Convert PIL to numpy array
        image_np = np.array(image)
        
        # Detect faces with improved method
        faces = detect_faces_improved(image_np)
        
        if not faces:
            return "❌ No faces detected in the image"
        
        # Collect all emotions
        all_emotions = {}
        face_details = []
        
        for i, (x, y, w, h) in enumerate(faces):
            # Extract face region
            face_region = image.crop((x, y, x + w, y + h))
            
            # Predict emotion
            emotions = predict_emotion(face_region)
            
            # Store face details
            sorted_emotions = sorted(emotions, key=lambda x: x['score'], reverse=True)
            face_details.append({
                'face_num': i + 1,
                'position': (x, y, w, h),
                'emotions': sorted_emotions
            })
            
            for emotion_data in emotions:
                emotion = emotion_data['label']
                score = emotion_data['score']
                
                if emotion not in all_emotions:
                    all_emotions[emotion] = []
                all_emotions[emotion].append(score)
        
        # Calculate statistics
        stats_lines = [f"πŸ“Š **Detailed Emotion Analysis for {len(faces)} face(s):**\n"]
        
        # Per-face breakdown
        for face_detail in face_details:
            stats_lines.append(f"### πŸ‘€ Face {face_detail['face_num']}:")
            top_emotion = face_detail['emotions'][0]
            stats_lines.append(f"**Primary emotion:** {top_emotion['label'].title()} ({top_emotion['score']:.1%})")
            
            stats_lines.append("**All emotions detected:**")
            for emotion in face_detail['emotions']:
                bar_length = int(emotion['score'] * 20)  # Scale to 20 chars
                bar = "β–ˆ" * bar_length + "β–‘" * (20 - bar_length)
                stats_lines.append(f"  {emotion['label'].title()}: {bar} {emotion['score']:.1%}")
            stats_lines.append("")
        
        # Overall statistics
        if len(faces) > 1:
            stats_lines.append("### πŸ“ˆ Overall Statistics:")
            for emotion, scores in all_emotions.items():
                avg_score = np.mean(scores)
                max_score = np.max(scores)
                count = len(scores)
                
                stats_lines.append(f"**{emotion.title()}:**")
                stats_lines.append(f"  - Average confidence: {avg_score:.1%}")
                stats_lines.append(f"  - Maximum confidence: {max_score:.1%}")
                stats_lines.append(f"  - Faces showing this emotion: {count}/{len(faces)}")
                stats_lines.append("")
        
        return "\n".join(stats_lines)
        
    except Exception as e:
        logger.error(f"Error calculating statistics: {e}")
        return f"❌ Error calculating statistics: {str(e)}"

# Create simplified Gradio interface
def create_interface():
    custom_css = """
    .main-header {
        text-align: center;
        color: #2563eb;
        margin-bottom: 2rem;
    }
    .gradio-container {
        max-width: 1200px;
        margin: auto;
    }
    """
    
    with gr.Blocks(
        title="Face Emotion Detection - Improved",
        theme=gr.themes.Soft(),
        css=custom_css
    ) as iface:
        
        # Header
        gr.Markdown(
            """
            # 😊 Face Emotion Detection (Improved)
            
            ### Accurate emotion recognition with enhanced face detection
            
            This improved version includes better face detection algorithms to reduce false positives 
            and provides more accurate emotion classification for detected faces.
            """,
            elem_classes=["main-header"]
        )
        
        with gr.Tab("πŸ–ΌοΈ Single Image Analysis"):
            with gr.Row():
                with gr.Column(scale=1):
                    image_input = gr.Image(
                        label="Upload Image",
                        type="pil",
                        height=400
                    )
                    
                    with gr.Row():
                        confidence_slider = gr.Slider(
                            minimum=0.1,
                            maximum=1.0,
                            value=0.5,
                            step=0.1,
                            label="🎯 Confidence Threshold",
                            info="Minimum confidence to display emotions"
                        )
                        
                        face_size_slider = gr.Slider(
                            minimum=30,
                            maximum=200,
                            value=80,
                            step=10,
                            label="πŸ‘€ Minimum Face Size",
                            info="Minimum face size (pixels) to detect"
                        )
                    
                    analyze_btn = gr.Button("πŸ” Analyze Emotions", variant="primary", size="lg")
                
                with gr.Column(scale=1):
                    output_image = gr.Image(
                        label="Emotion Detection Results",
                        height=400
                    )
                    result_text = gr.Textbox(
                        label="Detection Results",
                        lines=8,
                        show_copy_button=True
                    )
        
        with gr.Tab("πŸ“Š Detailed Statistics"):
            with gr.Row():
                with gr.Column(scale=1):
                    stats_image_input = gr.Image(
                        label="Upload Image for Statistical Analysis",
                        type="pil",
                        height=400
                    )
                    analyze_stats_btn = gr.Button("πŸ“ˆ Generate Detailed Statistics", variant="primary", size="lg")
                
                with gr.Column(scale=1):
                    stats_output = gr.Markdown(
                        value="Upload an image and click 'Generate Detailed Statistics' to see comprehensive emotion analysis...",
                        label="Emotion Statistics"
                    )
        
        with gr.Tab("πŸ”„ Batch Processing"):
            with gr.Column():
                batch_images_input = gr.File(
                    label="Upload Multiple Images",
                    file_count="multiple",
                    file_types=["image"]
                )
                batch_process_btn = gr.Button("⚑ Process All Images", variant="primary", size="lg")
                batch_results_output = gr.Textbox(
                    label="Batch Processing Results",
                    lines=15,
                    show_copy_button=True
                )
        
        with gr.Tab("ℹ️ About & Tips"):
            gr.Markdown(
                """
                ## πŸ”§ Improvements Made
                
                ### βœ… Enhanced Face Detection
                - **Stricter parameters** to reduce false positives
                - **Overlap detection** to merge duplicate face detections
                - **Size filtering** to ignore unrealistic face sizes
                - **Aspect ratio validation** to filter non-face rectangles
                
                ### 🎯 Better Accuracy
                - **Confidence thresholds** to filter uncertain predictions
                - **Improved preprocessing** for better emotion recognition
                - **Face padding** for better context in emotion detection
                
                ### πŸš€ Performance Optimizations
                - **Removed problematic live camera** feature
                - **Streamlined interface** for better user experience
                - **Better error handling** and user feedback
                
                ## πŸ“š Supported Emotions
                
                - 😠 **Angry** - Expressions of anger, frustration
                - 🀒 **Disgust** - Expressions of revulsion or distaste  
                - 😨 **Fear** - Expressions of fear, anxiety
                - 😊 **Happy** - Expressions of joy, contentment
                - 😒 **Sad** - Expressions of sadness, sorrow
                - 😲 **Surprise** - Expressions of surprise, amazement
                - 😐 **Neutral** - Calm, neutral expressions
                
                ## πŸ’‘ Tips for Best Results
                
                1. **Use clear, well-lit images** with visible faces
                2. **Adjust confidence threshold** if you get too many/few results
                3. **Modify minimum face size** based on your image resolution
                4. **Frontal face views** work better than profile shots
                5. **Avoid heavily shadowed or blurry faces**
                
                ## πŸ”§ Troubleshooting
                
                - **No faces detected?** Try lowering the minimum face size
                - **Too many false detections?** Increase the minimum face size or confidence threshold
                - **Missing obvious faces?** Lower the confidence threshold
                - **Multiple boxes on same face?** The system should automatically merge them now
                
                ---
                
                **Model:** [abhilash88/face-emotion-detection](https://huggingface.co/abhilash88/face-emotion-detection)
                """
            )
        
        # Event handlers
        analyze_btn.click(
            fn=process_image,
            inputs=[image_input, confidence_slider, face_size_slider],
            outputs=[output_image, result_text],
            api_name="analyze_image"
        )
        
        analyze_stats_btn.click(
            fn=get_emotion_statistics,
            inputs=stats_image_input,
            outputs=stats_output,
            api_name="get_statistics"
        )
        
        batch_process_btn.click(
            fn=analyze_emotions_batch,
            inputs=batch_images_input,
            outputs=batch_results_output,
            api_name="batch_process"
        )
        
        # Example images
        gr.Examples(
            examples=[
                "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=400&h=400&fit=crop&crop=face",
                "https://images.unsplash.com/photo-1554151228-14d9def656e4?w=400&h=400&fit=crop&crop=face",
                "https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=400&h=400&fit=crop&crop=face",
            ],
            inputs=image_input,
            label="πŸ–ΌοΈ Try these example images"
        )
    
    return iface

# Initialize and launch
if __name__ == "__main__":
    logger.info("Initializing Improved Face Emotion Detection System...")
    
    if load_models():
        logger.info("Models loaded successfully!")
        
        iface = create_interface()
        
        iface.launch(
            share=False,
            show_error=True,
            server_name="0.0.0.0",
            server_port=7860,
            show_api=True
        )
    else:
        logger.error("Failed to load models. Please check your model configuration.")
        with gr.Blocks() as error_iface:
            gr.Markdown(
                """
                # ⚠️ Model Loading Error
                
                The emotion detection model failed to load. Please check:
                
                1. Network connectivity
                2. Model dependencies
                3. System logs for details
                """
            )
        
        error_iface.launch(
            share=False,
            show_error=True,
            server_name="0.0.0.0",
            server_port=7860
        )