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767ca
 
fd12b0a
 
 
24767ca
 
fd12b0a
24767ca
 
 
fd12b0a
24767ca
fd12b0a
 
 
 
 
24767ca
fd12b0a
 
 
 
 
 
 
24767ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f1cfda
24767ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd12b0a
24767ca
 
 
 
 
 
 
 
 
 
 
 
fd12b0a
 
 
 
e6997b2
 
fd12b0a
e6997b2
fd12b0a
 
 
e6997b2
fd12b0a
 
 
 
 
 
 
24767ca
 
 
 
 
 
fd12b0a
 
24767ca
 
 
 
 
 
1f1cfda
24767ca
 
 
 
 
 
 
 
1f1cfda
24767ca
 
1f1cfda
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
import io
import os
import zipfile
import numpy as np
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
import gradio as gr
from ultralytics import YOLO
import torch
from typing import List, Dict, Tuple
import json


# ==========================================================
# CONFIG (FLEXIBLE PATHS FOR LOCAL & HUGGING FACE)
# ==========================================================
CLASSES_PATH = Path("model/classes.txt")
MODEL_PATH = Path("model/best.pt")

# Verify files exist
if not MODEL_PATH.exists():
    raise FileNotFoundError(
        f"❌ Model not found at {MODEL_PATH}.\n"
        f"Please ensure your directory structure is:\n"
        f"  model/\n"
        f"    β”œβ”€β”€ best.pt\n"
        f"    └── classes.txt"
    )
if not CLASSES_PATH.exists():
    raise FileNotFoundError(
        f"❌ Classes file not found at {CLASSES_PATH}.\n"
        f"Please ensure 'classes.txt' exists in the model/ directory."
    )


# ==========================================================
# LOAD CLASSES
# ==========================================================
def load_classes(path):
    with open(path, "r", encoding="utf-8") as f:
        return [line.strip() for line in f.readlines()]


CLASS_NAMES = load_classes(CLASSES_PATH)
print(f"βœ… Loaded {len(CLASS_NAMES)} classes: {', '.join(CLASS_NAMES)}")


# ==========================================================
# FONT (CROSS-PLATFORM SAFE)
# ==========================================================
def get_font(size=20):
    """Try multiple font sources for cross-platform compatibility"""
    font_options = [
        "arial.ttf",
        "Arial.ttf",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
        "/System/Library/Fonts/Helvetica.ttc",
        "C:\\Windows\\Fonts\\arial.ttf",
        "C:\\Windows\\Fonts\\arialbd.ttf",
    ]

    for font_path in font_options:
        try:
            return ImageFont.truetype(font_path, size)
        except:
            continue

    return ImageFont.load_default()


FONT = get_font(24)
FONT_SMALL = get_font(18)


# ==========================================================
# LOAD YOLO MODEL WITH OPTIMIZATIONS
# ==========================================================
print(f"πŸ”„ Loading model from {MODEL_PATH}...")
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f"πŸ–₯️  Using device: {device}")

model = YOLO(str(MODEL_PATH))
model.model.eval()
if device == 'cuda':
    model.model.half()  # FP16 for faster inference on GPU
print(f"βœ… Model loaded successfully!")


# ==========================================================
# COLOR PALETTE FOR CONSISTENT COLORS
# ==========================================================
def get_color_palette(num_classes):
    """Generate distinct colors for each class"""
    np.random.seed(42)
    colors = []
    for i in range(num_classes):
        # Use HSV for better color distribution
        hue = int(360 * i / num_classes)
        saturation = 200 + np.random.randint(0, 55)
        value = 180 + np.random.randint(0, 75)

        # Convert HSV to RGB (simplified)
        import colorsys
        r, g, b = colorsys.hsv_to_rgb(hue / 360, saturation / 255, value / 255)
        colors.append((int(r * 255), int(g * 255), int(b * 255)))

    return colors


COLOR_PALETTE = get_color_palette(len(CLASS_NAMES))


# ==========================================================
# IMAGE PREPROCESSING FOR BETTER DETECTION
# ==========================================================
def preprocess_image(image: Image.Image, enhance: bool = True) -> Image.Image:
    """
    Enhance image quality for better detection
    """
    if not enhance:
        return image

    # Convert to RGB if needed
    if image.mode != 'RGB':
        image = image.convert('RGB')

    # Enhance contrast
    enhancer = ImageEnhance.Contrast(image)
    image = enhancer.enhance(1.15)

    # Enhance sharpness
    enhancer = ImageEnhance.Sharpness(image)
    image = enhancer.enhance(1.2)

    # Enhance brightness slightly
    enhancer = ImageEnhance.Brightness(image)
    image = enhancer.enhance(1.05)

    return image


# ==========================================================
# IMPROVED BOUNDING BOX UTILITIES
# ==========================================================
def compute_iou(box1: List[int], box2: List[int]) -> float:
    """Compute Intersection over Union between two boxes"""
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    x2 = min(box1[2], box2[2])
    y2 = min(box1[3], box2[3])

    intersection = max(0, x2 - x1) * max(0, y2 - y1)

    area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
    area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
    union = area1 + area2 - intersection

    return intersection / union if union > 0 else 0


def compute_box_area(box: List[int]) -> int:
    """Compute area of a box"""
    return (box[2] - box[0]) * (box[3] - box[1])


def is_box_inside(box1: List[int], box2: List[int], threshold: float = 0.95) -> bool:
    """Check if box1 is inside box2"""
    x1, y1, x2, y2 = box1
    bx1, by1, bx2, by2 = box2

    # Check if box1 is completely inside box2
    if x1 >= bx1 and y1 >= by1 and x2 <= bx2 and y2 <= by2:
        area1 = compute_box_area(box1)
        area2 = compute_box_area(box2)

        # If box1 is very small compared to box2, it's nested
        if area1 < area2 * threshold:
            return True

    return False


# ==========================================================
# ADVANCED NMS WITH NESTED BOX REMOVAL
# ==========================================================
def remove_nested_boxes(boxes: List[Dict], containment_threshold: float = 0.85) -> List[Dict]:
    """
    Remove boxes that are nested inside other boxes of different classes
    Keeps the higher confidence detection
    """
    if len(boxes) <= 1:
        return boxes

    # Sort by confidence descending
    boxes = sorted(boxes, key=lambda x: x['conf'], reverse=True)
    keep = []

    for box1 in boxes:
        is_nested = False

        # Check against all higher-confidence boxes already kept
        for box2 in keep:
            # Only check for nesting between different classes
            if box1['cls'] != box2['cls']:
                # Check if box1 is inside box2
                if is_box_inside(box1['xyxy'], box2['xyxy'], containment_threshold):
                    is_nested = True
                    break

                # Also check very high IoU between different classes
                iou = compute_iou(box1['xyxy'], box2['xyxy'])
                if iou > 0.85:  # Very high overlap
                    is_nested = True
                    break

        if not is_nested:
            keep.append(box1)

    return keep


def non_max_suppression_custom(boxes: List[Dict], iou_threshold: float) -> List[Dict]:
    """
    Improved NMS with better handling of overlapping detections
    """
    if not boxes:
        return []

    # Sort by confidence descending
    boxes = sorted(boxes, key=lambda x: x['conf'], reverse=True)
    keep = []

    while boxes:
        best = boxes[0]
        keep.append(best)
        boxes = boxes[1:]

        filtered = []
        for box in boxes:
            iou = compute_iou(best['xyxy'], box['xyxy'])

            # Same class: use standard NMS
            if best['cls'] == box['cls']:
                if iou < iou_threshold:
                    filtered.append(box)
            # Different class: only suppress if extremely high overlap
            else:
                if iou < 0.80:  # Higher threshold for different classes
                    filtered.append(box)

        boxes = filtered

    return keep


# ==========================================================
# ADVANCED INFERENCE WITH IMPROVED MERGING
# ==========================================================
def advanced_inference(
    img: Image.Image,
    conf: float,
    iou: float,
    img_size: int,
    use_tta: bool,
    use_ensemble: bool,
    enhance_img: bool
) -> List[Dict]:
    """
    Enhanced inference with better false positive suppression
    """
    # Preprocess image
    img = preprocess_image(img, enhance=enhance_img)

    all_predictions = []

    # Strategy 1: Standard prediction with optimal settings
    results = model.predict(
        img,
        conf=conf,
        iou=iou,
        verbose=False,
        augment=use_tta,
        imgsz=img_size,
        half=(device == 'cuda'),
        device=device,
        max_det=150,  # Reduced from 300
        agnostic_nms=True  # Better for reducing false positives
    )[0]
    all_predictions.append(results)

    # Strategy 2: Multi-scale inference (ensemble mode)
    if use_ensemble:
        scales = [img_size - 64, img_size +
                  64] if img_size >= 704 else [img_size]
        for scale in scales:
            results_scaled = model.predict(
                img,
                conf=conf * 1.1,  # Slightly higher confidence
                iou=iou,
                verbose=False,
                augment=False,
                imgsz=scale,
                half=(device == 'cuda'),
                device=device,
                max_det=150,
                agnostic_nms=True
            )[0]
            all_predictions.append(results_scaled)

    # Merge predictions
    merged_boxes = merge_predictions(
        all_predictions, iou_threshold=iou, conf_threshold=conf)

    # Remove nested boxes (critical for reducing false positives)
    merged_boxes = remove_nested_boxes(
        merged_boxes, containment_threshold=0.85)

    return merged_boxes


def merge_predictions(predictions: List, iou_threshold: float, conf_threshold: float) -> List[Dict]:
    """
    Merge multiple predictions using improved NMS
    """
    if len(predictions) == 1:
        boxes = yolo_to_boxes(predictions[0])
        return [b for b in boxes if b['conf'] >= conf_threshold]

    all_boxes = []
    for pred in predictions:
        boxes = yolo_to_boxes(pred)
        all_boxes.extend(boxes)

    if not all_boxes:
        return []

    # Group by class
    class_boxes = {}
    for box in all_boxes:
        cls = box['cls']
        if cls not in class_boxes:
            class_boxes[cls] = []
        class_boxes[cls].append(box)

    # Apply NMS per class
    final_boxes = []
    for cls, boxes in class_boxes.items():
        nms_boxes = non_max_suppression_custom(boxes, iou_threshold)
        final_boxes.extend(
            [b for b in nms_boxes if b['conf'] >= conf_threshold])

    return final_boxes


# ==========================================================
# CONVERT YOLO RESULTS
# ==========================================================
def yolo_to_boxes(res):
    boxes = []
    for r in res.boxes:
        x1, y1, x2, y2 = r.xyxy[0].tolist()
        boxes.append({
            "cls": int(r.cls[0]),
            "conf": float(r.conf[0]),
            "xyxy": [int(x1), int(y1), int(x2), int(y2)]
        })
    return boxes


# ==========================================================
# ENHANCED BOX DRAWING
# ==========================================================
def draw_boxes(image, boxes, show_conf=True, box_thickness=3):
    """Enhanced visualization with better styling"""
    img = image.convert("RGBA")
    overlay = Image.new("RGBA", img.size, (0, 0, 0, 0))
    d = ImageDraw.Draw(overlay)

    for b in boxes:
        cls_idx = b["cls"]
        cls = CLASS_NAMES[cls_idx]
        conf = b['conf']
        x1, y1, x2, y2 = b["xyxy"]
        color = COLOR_PALETTE[cls_idx]

        # Adaptive box thickness based on confidence
        thickness = max(2, int(box_thickness * (0.6 + conf * 0.4)))

        # Draw main bounding box
        d.rectangle([x1, y1, x2, y2], outline=color + (255,), width=thickness)

        # Draw corner markers for better visibility
        corner_len = 20
        d.line([x1, y1, x1 + corner_len, y1],
               fill=color + (255,), width=thickness + 1)
        d.line([x1, y1, x1, y1 + corner_len],
               fill=color + (255,), width=thickness + 1)
        d.line([x2, y1, x2 - corner_len, y1],
               fill=color + (255,), width=thickness + 1)
        d.line([x2, y1, x2, y1 + corner_len],
               fill=color + (255,), width=thickness + 1)

        # Text label with confidence
        if show_conf:
            label = f"{cls} {conf:.0%}"
        else:
            label = cls

        # Get text dimensions
        bbox = FONT.getbbox(label)
        text_w = bbox[2] - bbox[0]
        text_h = bbox[3] - bbox[1]

        # Position label above box, or below if at top edge
        padding = 8
        if y1 - text_h - padding * 2 >= 0:
            label_y = y1 - text_h - padding * 2
            label_pos = "top"
        else:
            label_y = y2
            label_pos = "bottom"

        # Background rectangle with rounded appearance
        bg_coords = [x1, label_y, x1 + text_w +
                     padding * 2, label_y + text_h + padding * 2]
        d.rectangle(bg_coords, fill=color + (240,))

        # Add subtle border to label
        d.rectangle(bg_coords, outline=color + (255,), width=2)

        # Draw text with shadow for better readability
        shadow_offset = 2
        d.text(
            (x1 + padding + shadow_offset, label_y + padding + shadow_offset),
            label,
            fill=(0, 0, 0, 120),
            font=FONT
        )
        d.text(
            (x1 + padding, label_y + padding),
            label,
            fill="white",
            font=FONT
        )

    return Image.alpha_composite(img, overlay).convert("RGB")


# ==========================================================
# SINGLE IMAGE PREDICTION (IMPROVED)
# ==========================================================
def predict_single(
    input_image,
    conf,
    iou,
    use_tta,
    img_size,
    use_ensemble,
    enhance_img,
    show_conf,
    box_thickness
):
    if input_image is None:
        return None, [], "⚠️ Please upload an image first"

    img = Image.fromarray(input_image).convert("RGB")

    # Advanced inference
    boxes = advanced_inference(
        img,
        conf=conf,
        iou=iou,
        img_size=img_size,
        use_tta=use_tta,
        use_ensemble=use_ensemble,
        enhance_img=enhance_img
    )

    if not boxes:
        return img, [], f"ℹ️ No objects detected with confidence β‰₯ {conf:.0%}. Try lowering the confidence threshold or enabling TTA/Ensemble modes."

    out_img = draw_boxes(img, boxes, show_conf=show_conf,
                         box_thickness=box_thickness)

    # Create detailed detection table
    det_table = [
        [
            CLASS_NAMES[b["cls"]],
            f"{b['conf']:.2%}",
            f"({b['xyxy'][0]}, {b['xyxy'][1]})",
            f"({b['xyxy'][2]}, {b['xyxy'][3]})",
            f"{compute_box_area(b['xyxy']):,}"
        ]
        for b in sorted(boxes, key=lambda x: x['conf'], reverse=True)
    ]

    # Count detections by class
    counts = {}
    conf_by_class = {}
    for b in boxes:
        cls = CLASS_NAMES[b["cls"]]
        counts[cls] = counts.get(cls, 0) + 1
        if cls not in conf_by_class:
            conf_by_class[cls] = []
        conf_by_class[cls].append(b['conf'])

    # Create enhanced summary with quality indicators
    avg_conf = np.mean([b['conf'] for b in boxes])
    summary = f"### 🎯 Detection Summary\n\n"
    summary += f"**Total Objects Detected:** {len(boxes)}\n"
    summary += f"**Average Confidence:** {avg_conf:.1%}"

    # Add confidence quality indicator
    if avg_conf >= 0.70:
        summary += " βœ… (High Quality)\n"
    elif avg_conf >= 0.50:
        summary += " ⚠️ (Medium Quality - verify results)\n"
    else:
        summary += " ⚠️ (Low Quality - may contain false positives)\n"

    summary += "\n**Breakdown by Class:**\n"
    for cls, count in sorted(counts.items(), key=lambda x: x[1], reverse=True):
        avg_cls_conf = np.mean(conf_by_class[cls])
        quality_icon = "βœ…" if avg_cls_conf >= 0.60 else "⚠️" if avg_cls_conf >= 0.40 else "❌"
        summary += f"- {quality_icon} **{cls}**: {count} object{'s' if count > 1 else ''} (avg conf: {avg_cls_conf:.1%})\n"

    # Add warnings and recommendations
    warnings = []
    if avg_conf < 0.35:
        warnings.append(
            f"⚠️ Very low average confidence. Consider increasing threshold to {min(0.50, conf + 0.15):.2f}")
    if len(boxes) > 15:
        warnings.append(
            "⚠️ Many detections found. Consider increasing confidence threshold to reduce false positives")

    # Check for potential false positives (many low-confidence different-class detections)
    low_conf_count = sum(1 for b in boxes if b['conf'] < 0.40)
    if low_conf_count > len(boxes) * 0.5 and len(set(b['cls'] for b in boxes)) > 3:
        warnings.append(
            "⚠️ Multiple low-confidence detections across different classes detected")
        warnings.append(
            f"πŸ’‘ Recommended: Increase confidence to {min(0.50, conf + 0.20):.2f}")

    if warnings:
        summary += "\n**⚠️ Recommendations:**\n"
        for warning in warnings:
            summary += f"- {warning}\n"

    # Add inference settings info
    summary += f"\n**Inference Configuration:**\n"
    summary += f"- Test-Time Augmentation: {'βœ… Enabled' if use_tta else '❌ Disabled'}\n"
    summary += f"- Multi-Scale Ensemble: {'βœ… Enabled' if use_ensemble else '❌ Disabled'}\n"
    summary += f"- Image Enhancement: {'βœ… Enabled' if enhance_img else '❌ Disabled'}\n"
    summary += f"- Input Image Size: {img_size}px\n"
    summary += f"- Confidence Threshold: {conf:.1%}\n"
    summary += f"- IoU Threshold: {iou:.1%}\n"

    return out_img, det_table, summary


# ==========================================================
# BATCH PREDICTION (OPTIMIZED)
# ==========================================================
def predict_batch(files, conf, iou, use_tta, img_size, use_ensemble, enhance_img):
    if not files:
        return {"message": "⚠️ No files uploaded"}, None

    tmp = Path("pred_tmp")
    tmp.mkdir(exist_ok=True)

    meta = []
    output_paths = []
    total_detections = 0
    all_class_counts = {}
    failed_images = []

    for idx, f in enumerate(files, 1):
        try:
            img = Image.open(f).convert("RGB")

            boxes = advanced_inference(
                img,
                conf=conf,
                iou=iou,
                img_size=img_size,
                use_tta=use_tta,
                use_ensemble=use_ensemble,
                enhance_img=enhance_img
            )

            out_img = draw_boxes(img, boxes)

            out_path = tmp / f"pred_{Path(f).name}"
            out_img.save(out_path, quality=95, optimize=True)
            output_paths.append(out_path)

            counts = {}
            for b in boxes:
                cls = CLASS_NAMES[b["cls"]]
                counts[cls] = counts.get(cls, 0) + 1
                all_class_counts[cls] = all_class_counts.get(cls, 0) + 1

            total_detections += len(boxes)

            meta.append({
                "image": Path(f).name,
                "detections": len(boxes),
                "avg_confidence": f"{np.mean([b['conf'] for b in boxes]):.1%}" if boxes else "N/A",
                "objects": counts,
                "status": "βœ… Success"
            })

            print(
                f"βœ… [{idx}/{len(files)}] {Path(f).name} - {len(boxes)} objects detected")

        except Exception as e:
            error_msg = str(e)
            print(
                f"❌ [{idx}/{len(files)}] Error processing {Path(f).name}: {error_msg}")
            failed_images.append(Path(f).name)
            meta.append({
                "image": Path(f).name,
                "status": "❌ Failed",
                "error": error_msg
            })

    # Create ZIP only if there are successful predictions
    zip_path = None
    if output_paths:
        zip_path = tmp / "predictions.zip"
        with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED, compresslevel=6) as z:
            for p in output_paths:
                z.write(p, arcname=p.name)

    # Enhanced summary
    summary = {
        "πŸ“Š Processing Summary": {
            "Total Images": len(files),
            "βœ… Successful": len(output_paths),
            "❌ Failed": len(failed_images),
            "Success Rate": f"{(len(output_paths) / len(files) * 100):.1f}%"
        },
        "🎯 Detection Summary": {
            "Total Detections": total_detections,
            "Avg Detections/Image": f"{total_detections / len(output_paths):.1f}" if output_paths else "0",
            "Images with Detections": sum(1 for m in meta if m.get('detections', 0) > 0)
        },
        "πŸ“¦ Class Distribution": all_class_counts,
        "πŸ–ΌοΈ Detailed Results": meta
    }

    if failed_images:
        summary["❌ Failed Images"] = failed_images

    return summary, str(zip_path) if zip_path else None


# ==========================================================
# GRADIO UI (PREMIUM DESIGN WITH TECHXNINJAS BRANDING)
# ==========================================================
css = """
.gradio-container {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    max-width: 1600px;
    margin: auto;
}
.primary-btn {
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%) !important;
    border: none !important;
    color: white !important;
    font-weight: 600 !important;
    transition: all 0.3s ease !important;
    padding: 12px 24px !important;
}
.primary-btn:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 12px 24px rgba(255, 107, 107, 0.4) !important;
}
.stats-box {
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    padding: 20px;
    border-radius: 12px;
    margin: 10px 0;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.accuracy-badge {
    display: inline-block;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: white;
    padding: 6px 16px;
    border-radius: 20px;
    font-weight: bold;
    font-size: 14px;
    box-shadow: 0 2px 4px rgba(16, 185, 129, 0.3);
}
.hackathon-badge {
    display: inline-block;
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%);
    color: white;
    padding: 8px 20px;
    border-radius: 25px;
    font-weight: bold;
    font-size: 16px;
    margin: 10px 5px;
    box-shadow: 0 4px 8px rgba(255, 107, 107, 0.3);
}
.header-gradient {
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.paranox-banner {
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
    color: white;
    padding: 25px;
    border-radius: 15px;
    text-align: center;
    margin-bottom: 20px;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
}
"""

with gr.Blocks(theme=gr.themes.Soft(), css=css, title="TechXNinjas | PARANOX 2.0 - Safety Detector") as demo:

    gr.HTML("""
    <div class="paranox-banner">
        <h1 style="font-size: 3em; margin: 0; font-weight: bold; color: white;">⚑ TechXNinjas</h1>
        <div style="margin: 15px 0;">
            <span class="hackathon-badge">PARANOX 2.0</span>
        </div>
        <p style="font-size: 1.3em; margin: 10px 0; opacity: 0.9; color: #f0f0f0;">
            24-Hour National Innovation Hackathon | 3-Month Journey: Build β†’ Pitch β†’ Prototype
        </p>
        <p style="font-size: 1em; margin: 5px 0; opacity: 0.7; color: #f0f0f0;">
            πŸš€ Where Students Transform Ideas Into Reality
        </p>
    </div>
    """)

    gr.Markdown("""
    <div style="text-align: center; padding: 30px 20px; background: linear-gradient(135deg, #ff6b6b15 0%, #ee5a6f15 100%); border-radius: 15px; margin-bottom: 20px;">
        <h1 style="font-size: 2.5em; margin-bottom: 10px;">πŸ›‘οΈ AI Safety Object Detector</h1>
        <p style="font-size: 1.2em; color: #555; margin: 10px 0;">
            <span class="accuracy-badge">MAXIMUM ACCURACY MODE</span><br>
            <span style="margin-top: 10px; display: inline-block;">Advanced YOLOv8 with Enhanced NMS & False Positive Suppression</span>
        </p>
    </div>
    """)

    with gr.Row():
        # ===== LEFT PANEL: INPUT & CONTROLS =====
        with gr.Column(scale=2):
            gr.Markdown("### πŸ“Έ Single Image Detection")
            img_input = gr.Image(
                type="numpy",
                label="Upload Image for Detection",
                height=400,
                interactive=True
            )

            with gr.Accordion("βš™οΈ Detection Settings", open=True):
                gr.Markdown("**Core Parameters** - Adjust for optimal results")
                with gr.Row():
                    conf = gr.Slider(
                        0.05, 0.95, 0.25, step=0.05,
                        label="🎯 Confidence Threshold",
                        info="Higher = fewer but more accurate detections (recommended: 0.25-0.45)"
                    )
                    iou = gr.Slider(
                        0.10, 0.95, 0.45, step=0.05,
                        label="πŸ“¦ IoU Threshold",
                        info="Higher = less overlap filtering (recommended: 0.45-0.55)"
                    )

            with gr.Accordion("πŸ”¬ Advanced Accuracy Boosters", open=True):
                gr.Markdown(
                    "**Performance Enhancers** - Enable for maximum accuracy")
                with gr.Row():
                    use_tta = gr.Checkbox(
                        value=True,
                        label="✨ Test-Time Augmentation (TTA)",
                        info="Multiple augmented predictions (+3-7% mAP, slower)"
                    )
                    use_ensemble = gr.Checkbox(
                        value=False,
                        label="🎭 Multi-Scale Ensemble",
                        info="Multiple image sizes (+2-5% mAP, much slower)"
                    )

                with gr.Row():
                    enhance_img = gr.Checkbox(
                        value=True,
                        label="🎨 Image Enhancement",
                        info="Auto contrast, sharpness & brightness boost"
                    )
                    img_size = gr.Dropdown(
                        choices=[640, 800, 1024, 1280],
                        value=640,
                        label="πŸ“ Input Image Size",
                        info="Higher = better for small objects (slower)"
                    )

            with gr.Accordion("🎨 Visualization Options", open=False):
                with gr.Row():
                    show_conf = gr.Checkbox(
                        value=True,
                        label="πŸ“Š Show Confidence Scores",
                        info="Display confidence percentages in labels"
                    )
                    box_thickness = gr.Slider(
                        1, 8, 3, step=1,
                        label="πŸ“ Bounding Box Thickness",
                        info="Visual thickness of detection boxes"
                    )

            detect_btn = gr.Button(
                "πŸ” Detect Objects (High Accuracy Mode)",
                variant="primary",
                size="lg",
                elem_classes="primary-btn"
            )

            gr.Markdown("---")
            gr.Markdown("### πŸ“ Batch Processing Mode")
            batch_input = gr.File(
                file_count="multiple",
                label="Upload Multiple Images (JPG, PNG)",
                file_types=["image"],
                height=120
            )
            gr.Markdown(
                "*πŸ’‘ Tip: Upload multiple images to process them all at once and download as ZIP*")

        # ===== RIGHT PANEL: RESULTS & OUTPUT =====
        with gr.Column(scale=3):
            gr.Markdown("### 🎨 Detection Results")
            out_img = gr.Image(
                type="pil",
                label="Annotated Image with Detections",
                height=400,
                show_label=True
            )

            with gr.Row():
                out_counts = gr.Markdown(
                    value="πŸ“€ Upload an image to start detecting objects",
                    elem_classes="stats-box"
                )

            with gr.Accordion("πŸ“Š Detailed Detection Table", open=True):
                out_table = gr.Dataframe(
                    headers=["Class", "Confidence",
                             "Top-Left (x,y)", "Bottom-Right (x,y)", "Area (pxΒ²)"],
                    label="All Detections Sorted by Confidence",
                    row_count=10,
                    wrap=True
                )

            gr.Markdown("---")
            gr.Markdown("### πŸ“¦ Batch Processing Results")
            with gr.Row():
                with gr.Column():
                    batch_meta = gr.JSON(
                        label="πŸ“Š Batch Statistics & Details", show_label=True)
                with gr.Column():
                    batch_zip = gr.File(
                        label="πŸ“₯ Download All Predictions (ZIP)", show_label=True)

    # ===== TIPS & CONFIGURATION GUIDE =====
    with gr.Accordion("πŸ’‘ Configuration Guide - Get Best Results", open=False):
        gr.Markdown("""
        ## 🎯 Recommended Settings by Use Case

        ### πŸ† MAXIMUM ACCURACY (Best for Critical Applications)
        Perfect for: Safety inspections, compliance checks, detailed analysis

        | Parameter | Value | Why? |
        |-----------|-------|------|
        | Confidence | `0.35-0.45` | Filters out most false positives while keeping real objects |
        | IoU | `0.45-0.55` | Good balance for overlapping objects |
        | TTA | βœ… **Enabled** | +3-7% accuracy through augmentation |
        | Ensemble | βœ… **Enabled** | +2-5% accuracy through multi-scale detection |
        | Enhancement | βœ… **Enabled** | Improves detection on low-quality images |
        | Image Size | `800-1024px` | Better for small and distant objects |

        **Expected Performance:** Best accuracy, ~5-10 seconds per image

        ---

        ### ⚑ BALANCED MODE (Speed + Accuracy)
        Perfect for: General use, moderate batch processing

        | Parameter | Value | Why? |
        |-----------|-------|------|
        | Confidence | `0.30-0.40` | Good detection rate with acceptable false positives |
        | IoU | `0.45-0.50` | Standard NMS threshold |
        | TTA | βœ… **Enabled** | Worth the small speed cost |
        | Ensemble | ❌ **Disabled** | Too slow for marginal gains |
        | Enhancement | βœ… **Enabled** | Fast and helpful |
        | Image Size | `640px` | Fast and sufficient for most cases |

        **Expected Performance:** Good accuracy, ~2-3 seconds per image

        ---

        ### πŸš€ SPEED MODE (Real-time/Batch)
        Perfect for: Large batches, real-time monitoring, quick scans

        | Parameter | Value | Why? |
        |-----------|-------|------|
        | Confidence | `0.40-0.55` | Higher threshold = fewer detections but faster |
        | IoU | `0.50-0.60` | Standard NMS, less computation |
        | TTA | ❌ **Disabled** | Too slow for speed mode |
        | Ensemble | ❌ **Disabled** | Significantly slower |
        | Enhancement | ❌ **Disabled** | Save preprocessing time |
        | Image Size | `640px` | Fastest inference size |

        **Expected Performance:** Fast, ~0.5-1 second per image

        ---

        ## πŸ” Understanding Each Parameter

        ### Confidence Threshold (0.05-0.95)
        - **What it does:** Minimum probability score for a detection to be kept
        - **Lower (0.15-0.25):** More detections, more false positives
        - **Higher (0.40-0.60):** Fewer detections, fewer false positives
        - **Sweet spot:** 0.30-0.40 for most use cases

        ### IoU Threshold (0.10-0.95)
        - **What it does:** Controls how much boxes can overlap before one is removed (Non-Maximum Suppression)
        - **Lower (0.30-0.40):** More aggressive overlap removal, fewer boxes kept
        - **Higher (0.50-0.70):** Keeps more overlapping boxes (good for crowded scenes)
        - **Sweet spot:** 0.45-0.55 for most use cases
        """)

    # ===== MODEL INFORMATION =====
    with gr.Accordion("πŸ“Š Model & System Information", open=False):
        gr.Markdown(f"""
        ## πŸ€– Model Details
        
        **Architecture:** YOLOv8s (Small)
        - Parameters: 11.2M
        - FLOPs: 28.6G
        - Size: ~22MB
        
        **Trained Classes ({len(CLASS_NAMES)}):**
        ```
        {' β€’ '.join(CLASS_NAMES)}
        ```
        
        ## πŸ–₯️ Runtime Configuration
        
        **Device:** {device.upper()}
        **Precision:** {"FP16 (Half-precision)" if device == "cuda" else "FP32 (Full-precision)"}
        **CUDA Available:** {"βœ… Yes" if torch.cuda.is_available() else "❌ No (using CPU)"}
        
        ## ✨ Advanced Features Enabled
        
        βœ… **Test-Time Augmentation (TTA)**
        - Horizontal flips, brightness adjustments, scale variations
        - Predictions averaged across augmentations
        
        βœ… **Multi-Scale Ensemble Inference**
        - Multiple input resolutions (Β±64px from base size)
        - Weighted Box Fusion (WBF) for merging predictions
        
        βœ… **Image Preprocessing & Enhancement**
        - Contrast enhancement (+15%)
        - Sharpness boost (+20%)
        - Brightness normalization (+5%)
        
        βœ… **Improved Non-Maximum Suppression (NMS)**
        - Class-agnostic NMS for better cross-class handling
        - Nested box removal algorithm
        - Confidence-weighted box merging
        
        βœ… **False Positive Suppression**
        - Containment-based filtering (boxes inside other boxes)
        - High-overlap cross-class suppression
        - Confidence-based quality assessment
        """)

    # ===== EVENT BINDINGS =====

    # Single image detection
    detect_btn.click(
        fn=predict_single,
        inputs=[
            img_input,
            conf,
            iou,
            use_tta,
            img_size,
            use_ensemble,
            enhance_img,
            show_conf,
            box_thickness
        ],
        outputs=[out_img, out_table, out_counts]
    )

    # Batch processing
    batch_input.change(
        fn=predict_batch,
        inputs=[
            batch_input,
            conf,
            iou,
            use_tta,
            img_size,
            use_ensemble,
            enhance_img
        ],
        outputs=[batch_meta, batch_zip]
    )

    # ===== EXAMPLES =====
    gr.Markdown("---")
    gr.Markdown("### πŸ“š Quick Start Examples")
    gr.Markdown("""
    **Try these configurations for common scenarios:**
    
    1. **Single clear object (like fire extinguisher):**
       - Confidence: 0.40, IoU: 0.50, TTA: βœ…, Ensemble: ❌, Size: 640px
    
    2. **Multiple small objects:**
       - Confidence: 0.25, IoU: 0.45, TTA: βœ…, Ensemble: βœ…, Size: 1024px
    
    3. **Fast batch processing:**
       - Confidence: 0.45, IoU: 0.55, TTA: ❌, Ensemble: ❌, Size: 640px
    
    4. **Low quality/dark images:**
       - Confidence: 0.30, IoU: 0.50, TTA: βœ…, Enhancement: βœ…, Size: 800px
    """)

    # ===== FOOTER =====
    gr.HTML("""
    <div style="text-align: center; padding: 30px 20px; margin-top: 40px; background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); border-radius: 15px; color: white;">
        <h3 style="color: #ff6b6b; margin-bottom: 15px;">πŸš€ Built with Innovation & Passion</h3>
        <p style="font-size: 1.1em; margin: 10px 0; color: white;">
            Powered by TechXNinjas | PARANOX 2.0 Hackathon Project
        </p>
        <p style="opacity: 0.8; margin: 10px 0; color: white;">
            24-Hour National Hackathon β€’ 3-Month Innovation Journey β€’ Student-Led Excellence
        </p>
        <div style="margin-top: 20px; padding-top: 20px; border-top: 1px solid rgba(255,255,255,0.2);">
            <p style="opacity: 0.7; font-size: 0.9em; color: white;">
                ⚠️ AI-Powered Tool β€’ Always verify critical detections manually<br>
                Made with ❀️ for safety and security applications
            </p>
        </div>
    </div>
    """)


# ==========================================================
# LAUNCH APPLICATION
# ==========================================================
if __name__ == "__main__":
    print("\n" + "="*60)
    print("⚑ TechXNinjas - PARANOX 2.0")
    print("πŸš€ Starting AI Safety Object Detector")
    print("="*60)
    print(f"πŸ“¦ Model: {MODEL_PATH}")
    print(f"🏷️  Classes: {len(CLASS_NAMES)}")
    print(f"πŸ–₯️  Device: {device.upper()}")
    print(f"⚑ Precision: {'FP16' if device == 'cuda' else 'FP32'}")
    print("="*60 + "\n")

    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True,
        share=False,
        show_api=False,
        favicon_path=None
    )

    print("\nβœ… Application started successfully!")
    print("🌐 Open your browser and navigate to the URL shown above")
    print("⚠️  Press Ctrl+C to stop the server\n")