File size: 31,534 Bytes
9de3cd4
 
 
 
 
 
 
 
 
 
 
 
 
80e0ebd
9de3cd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d491c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f65f330
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
"""
Quality analysis and metrics for BackgroundFX Pro.
Provides REAL metrics instead of fake 100% values.
"""

import numpy as np
import cv2
import torch
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass, field
from collections import deque
import logging
from scipy import signal, ndimage
# from skimage import metrics as skmetrics
import json
from pathlib import Path
from datetime import datetime

logger = logging.getLogger(__name__)


@dataclass
class QualityMetrics:
    """Real quality metrics container."""
    # Edge Quality
    edge_accuracy: float = 0.0
    edge_smoothness: float = 0.0
    edge_completeness: float = 0.0
    
    # Temporal Quality
    temporal_stability: float = 0.0
    temporal_consistency: float = 0.0
    flicker_score: float = 0.0
    
    # Mask Quality
    mask_coverage: float = 0.0
    mask_accuracy: float = 0.0
    mask_confidence: float = 0.0
    hole_ratio: float = 0.0
    
    # Detail Preservation
    detail_preservation: float = 0.0
    hair_detail_score: float = 0.0
    texture_quality: float = 0.0
    
    # Overall Scores
    overall_quality: float = 0.0
    processing_confidence: float = 0.0
    
    # Detailed breakdown
    breakdown: Dict[str, float] = field(default_factory=dict)
    warnings: List[str] = field(default_factory=list)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            'edge_accuracy': round(self.edge_accuracy, 3),
            'edge_smoothness': round(self.edge_smoothness, 3),
            'edge_completeness': round(self.edge_completeness, 3),
            'temporal_stability': round(self.temporal_stability, 3),
            'temporal_consistency': round(self.temporal_consistency, 3),
            'flicker_score': round(self.flicker_score, 3),
            'mask_coverage': round(self.mask_coverage, 3),
            'mask_accuracy': round(self.mask_accuracy, 3),
            'mask_confidence': round(self.mask_confidence, 3),
            'hole_ratio': round(self.hole_ratio, 3),
            'detail_preservation': round(self.detail_preservation, 3),
            'hair_detail_score': round(self.hair_detail_score, 3),
            'texture_quality': round(self.texture_quality, 3),
            'overall_quality': round(self.overall_quality, 3),
            'processing_confidence': round(self.processing_confidence, 3),
            'breakdown': self.breakdown,
            'warnings': self.warnings
        }
    
    def get_summary(self) -> str:
        """Get human-readable summary."""
        status = "Excellent" if self.overall_quality > 0.9 else \
                 "Good" if self.overall_quality > 0.75 else \
                 "Fair" if self.overall_quality > 0.6 else "Poor"
        
        return (f"Quality: {status} ({self.overall_quality:.1%})\n"
                f"Edge: {self.edge_accuracy:.1%} | "
                f"Temporal: {self.temporal_stability:.1%} | "
                f"Detail: {self.detail_preservation:.1%}")


@dataclass
class QualityConfig:
    """Configuration for quality analysis."""
    enable_deep_analysis: bool = True
    temporal_window: int = 5
    edge_threshold: float = 0.1
    min_confidence: float = 0.6
    detect_artifacts: bool = True
    compute_ssim: bool = True
    compute_psnr: bool = True
    save_reports: bool = True
    report_dir: str = "LOGS/quality_reports"
    warning_thresholds: Dict[str, float] = field(default_factory=lambda: {
        'edge_accuracy': 0.7,
        'temporal_stability': 0.75,
        'mask_accuracy': 0.8,
        'detail_preservation': 0.7
    })


class QualityAnalyzer:
    """Comprehensive quality analysis system."""
    
    def __init__(self, config: Optional[QualityConfig] = None):
        self.config = config or QualityConfig()
        self.frame_buffer = deque(maxlen=self.config.temporal_window)
        self.mask_buffer = deque(maxlen=self.config.temporal_window)
        self.metrics_history = deque(maxlen=100)
        self.frame_count = 0
        
        # Initialize analyzers
        self.edge_analyzer = EdgeQualityAnalyzer()
        self.temporal_analyzer = TemporalQualityAnalyzer()
        self.detail_analyzer = DetailPreservationAnalyzer()
        self.artifact_detector = ArtifactDetector()
        
        # Create report directory
        if self.config.save_reports:
            Path(self.config.report_dir).mkdir(parents=True, exist_ok=True)
    
    def analyze_frame(self, 
                     original_frame: np.ndarray,
                     processed_frame: np.ndarray,
                     mask: np.ndarray,
                     alpha: Optional[np.ndarray] = None) -> QualityMetrics:
        """Analyze frame quality with REAL metrics."""
        self.frame_count += 1
        metrics = QualityMetrics()
        
        # Add to buffers
        self.frame_buffer.append(processed_frame)
        self.mask_buffer.append(mask)
        
        # 1. Edge Quality Analysis
        edge_metrics = self.edge_analyzer.analyze(original_frame, mask, alpha)
        metrics.edge_accuracy = edge_metrics['accuracy']
        metrics.edge_smoothness = edge_metrics['smoothness']
        metrics.edge_completeness = edge_metrics['completeness']
        
        # 2. Temporal Quality (if we have history)
        if len(self.mask_buffer) >= 2:
            temporal_metrics = self.temporal_analyzer.analyze(
                self.mask_buffer, self.frame_buffer
            )
            metrics.temporal_stability = temporal_metrics['stability']
            metrics.temporal_consistency = temporal_metrics['consistency']
            metrics.flicker_score = temporal_metrics['flicker']
        else:
            # First frame defaults
            metrics.temporal_stability = 1.0
            metrics.temporal_consistency = 1.0
            metrics.flicker_score = 0.0
        
        # 3. Mask Quality Analysis
        mask_metrics = self._analyze_mask_quality(mask, alpha)
        metrics.mask_coverage = mask_metrics['coverage']
        metrics.mask_accuracy = mask_metrics['accuracy']
        metrics.mask_confidence = mask_metrics['confidence']
        metrics.hole_ratio = mask_metrics['hole_ratio']
        
        # 4. Detail Preservation
        detail_metrics = self.detail_analyzer.analyze(
            original_frame, processed_frame, mask
        )
        metrics.detail_preservation = detail_metrics['overall']
        metrics.hair_detail_score = detail_metrics['hair_detail']
        metrics.texture_quality = detail_metrics['texture']
        
        # 5. Artifact Detection
        if self.config.detect_artifacts:
            artifacts = self.artifact_detector.detect(processed_frame, mask)
            if artifacts['found']:
                for artifact in artifacts['types']:
                    metrics.warnings.append(f"Artifact detected: {artifact}")
        
        # 6. Compute Overall Quality (weighted average)
        metrics.overall_quality = self._compute_overall_quality(metrics)
        metrics.processing_confidence = self._compute_confidence(metrics)
        
        # 7. Generate warnings based on thresholds
        self._generate_warnings(metrics)
        
        # 8. Store in history
        self.metrics_history.append(metrics)
        
        # 9. Save report if configured
        if self.config.save_reports and self.frame_count % 30 == 0:
            self._save_report(metrics)
        
        return metrics
    
    def _analyze_mask_quality(self, mask: np.ndarray,
                             alpha: Optional[np.ndarray] = None) -> Dict[str, float]:
        """Analyze mask quality metrics."""
        h, w = mask.shape[:2]
        total_pixels = h * w
        
        # Coverage ratio
        coverage = np.sum(mask > 0.5) / total_pixels
        
        # Hole detection
        mask_binary = (mask > 0.5).astype(np.uint8)
        
        # Find contours
        contours, _ = cv2.findContours(
            mask_binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
        )
        
        # Find holes (internal contours)
        hole_area = 0
        if len(contours) > 0:
            # Create filled mask
            filled = np.zeros_like(mask_binary)
            cv2.drawContours(filled, contours, -1, 1, -1)
            
            # Holes are the difference
            holes = filled - mask_binary
            hole_area = np.sum(holes) / np.sum(filled) if np.sum(filled) > 0 else 0
        
        # Accuracy (based on gradient consistency)
        gradient_x = cv2.Sobel(mask, cv2.CV_64F, 1, 0, ksize=3)
        gradient_y = cv2.Sobel(mask, cv2.CV_64F, 0, 1, ksize=3)
        gradient_mag = np.sqrt(gradient_x**2 + gradient_y**2)
        
        # Good masks have smooth gradients
        gradient_smoothness = 1.0 - np.std(gradient_mag) / (np.mean(gradient_mag) + 1e-6)
        accuracy = np.clip(gradient_smoothness, 0, 1)
        
        # Confidence (alpha vs mask consistency if alpha provided)
        if alpha is not None:
            diff = np.abs(alpha - mask)
            confidence = 1.0 - np.mean(diff)
        else:
            # Use mask value distribution as confidence
            hist, _ = np.histogram(mask.flatten(), bins=10, range=(0, 1))
            hist = hist / hist.sum()
            # High confidence = values clustered near 0 or 1
            confidence = (hist[0] + hist[-1]) / 2.0
        
        return {
            'coverage': coverage,
            'accuracy': accuracy,
            'confidence': confidence,
            'hole_ratio': hole_area
        }
    
    def _compute_overall_quality(self, metrics: QualityMetrics) -> float:
        """Compute weighted overall quality score."""
        weights = {
            'edge': 0.25,
            'temporal': 0.25,
            'mask': 0.25,
            'detail': 0.25
        }
        
        # Component scores
        edge_score = np.mean([
            metrics.edge_accuracy,
            metrics.edge_smoothness,
            metrics.edge_completeness
        ])
        
        temporal_score = np.mean([
            metrics.temporal_stability,
            metrics.temporal_consistency,
            1.0 - metrics.flicker_score  # Invert flicker
        ])
        
        mask_score = np.mean([
            metrics.mask_accuracy,
            metrics.mask_confidence,
            1.0 - metrics.hole_ratio  # Invert hole ratio
        ])
        
        detail_score = np.mean([
            metrics.detail_preservation,
            metrics.hair_detail_score,
            metrics.texture_quality
        ])
        
        # Weighted average
        overall = (
            weights['edge'] * edge_score +
            weights['temporal'] * temporal_score +
            weights['mask'] * mask_score +
            weights['detail'] * detail_score
        )
        
        # Apply penalties for warnings
        penalty = len(metrics.warnings) * 0.05
        overall = max(0, overall - penalty)
        
        return np.clip(overall, 0, 1)
    
    def _compute_confidence(self, metrics: QualityMetrics) -> float:
        """Compute processing confidence."""
        # Factors that affect confidence
        factors = []
        
        # High edge accuracy increases confidence
        factors.append(metrics.edge_accuracy)
        
        # Good temporal stability increases confidence
        factors.append(metrics.temporal_stability)
        
        # Low hole ratio increases confidence
        factors.append(1.0 - metrics.hole_ratio)
        
        # Mask confidence directly affects overall confidence
        factors.append(metrics.mask_confidence)
        
        # No warnings increases confidence
        warning_factor = 1.0 if len(metrics.warnings) == 0 else 0.8
        factors.append(warning_factor)
        
        return np.mean(factors)
    
    def _generate_warnings(self, metrics: QualityMetrics):
        """Generate warnings based on quality thresholds."""
        for metric_name, threshold in self.config.warning_thresholds.items():
            if hasattr(metrics, metric_name):
                value = getattr(metrics, metric_name)
                if value < threshold:
                    metrics.warnings.append(
                        f"Low {metric_name.replace('_', ' ')}: {value:.1%} < {threshold:.1%}"
                    )
    
    def _save_report(self, metrics: QualityMetrics):
        """Save quality report to file."""
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        report_path = Path(self.config.report_dir) / f"quality_report_{timestamp}.json"
        
        report = {
            'timestamp': timestamp,
            'frame_count': self.frame_count,
            'metrics': metrics.to_dict(),
            'config': {
                'temporal_window': self.config.temporal_window,
                'edge_threshold': self.config.edge_threshold,
                'min_confidence': self.config.min_confidence
            }
        }
        
        with open(report_path, 'w') as f:
            json.dump(report, f, indent=2)
        
        logger.info(f"Quality report saved to {report_path}")
    
    def get_statistics(self) -> Dict[str, Any]:
        """Get quality statistics over time."""
        if not self.metrics_history:
            return {}
        
        # Compute statistics
        all_metrics = list(self.metrics_history)
        
        stats = {
            'average_quality': np.mean([m.overall_quality for m in all_metrics]),
            'min_quality': np.min([m.overall_quality for m in all_metrics]),
            'max_quality': np.max([m.overall_quality for m in all_metrics]),
            'std_quality': np.std([m.overall_quality for m in all_metrics]),
            'total_warnings': sum(len(m.warnings) for m in all_metrics),
            'frames_analyzed': len(all_metrics)
        }
        
        return stats


class EdgeQualityAnalyzer:
    """Analyzes edge quality in masks."""
    
    def analyze(self, image: np.ndarray, mask: np.ndarray,
               alpha: Optional[np.ndarray] = None) -> Dict[str, float]:
        """Analyze edge quality metrics."""
        # Convert to grayscale if needed
        if len(image.shape) == 3:
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        else:
            gray = image
        
        # Detect edges in image
        image_edges = cv2.Canny(gray, 50, 150) / 255.0
        
        # Detect edges in mask
        mask_uint8 = (mask * 255).astype(np.uint8)
        mask_edges = cv2.Canny(mask_uint8, 50, 150) / 255.0
        
        # Edge accuracy: how well mask edges align with image edges
        overlap = np.logical_and(image_edges > 0, mask_edges > 0)
        accuracy = np.sum(overlap) / (np.sum(mask_edges) + 1e-6)
        
        # Edge smoothness: measure edge roughness
        contours, _ = cv2.findContours(
            mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
        )
        
        smoothness = 1.0
        if len(contours) > 0:
            # Approximate contours and measure approximation quality
            for contour in contours:
                perimeter = cv2.arcLength(contour, True)
                if perimeter > 0:
                    # Approximate polygon
                    epsilon = 0.02 * perimeter
                    approx = cv2.approxPolyDP(contour, epsilon, True)
                    
                    # Smoothness based on approximation ratio
                    complexity = len(approx) / (perimeter / 10 + 1)
                    smoothness = min(smoothness, 1.0 / (1.0 + complexity))
        
        # Edge completeness: how much of image edges are covered
        if np.sum(image_edges) > 0:
            # Dilate mask edges to allow some tolerance
            kernel = np.ones((5, 5), np.uint8)
            mask_edges_dilated = cv2.dilate(mask_edges, kernel, iterations=1)
            
            covered = np.logical_and(image_edges > 0, mask_edges_dilated > 0)
            completeness = np.sum(covered) / np.sum(image_edges)
        else:
            completeness = 1.0
        
        return {
            'accuracy': np.clip(accuracy, 0, 1),
            'smoothness': np.clip(smoothness, 0, 1),
            'completeness': np.clip(completeness, 0, 1)
        }


class TemporalQualityAnalyzer:
    """Analyzes temporal consistency and stability."""
    
    def analyze(self, mask_buffer: deque, frame_buffer: deque) -> Dict[str, float]:
        """Analyze temporal quality metrics."""
        if len(mask_buffer) < 2:
            return {'stability': 1.0, 'consistency': 1.0, 'flicker': 0.0}
        
        masks = list(mask_buffer)
        
        # Temporal stability: measure change between consecutive frames
        differences = []
        for i in range(1, len(masks)):
            diff = np.abs(masks[i] - masks[i-1])
            differences.append(np.mean(diff))
        
        # Lower difference = higher stability
        avg_diff = np.mean(differences)
        stability = 1.0 - min(avg_diff * 2, 1.0)  # Scale and invert
        
        # Temporal consistency: measure variance across window
        mask_stack = np.stack(masks, axis=0)
        variance = np.var(mask_stack, axis=0)
        consistency = 1.0 - np.mean(variance)
        
        # Flicker detection: look for alternating patterns
        flicker = 0.0
        if len(differences) >= 3:
            # Check for alternating high-low-high pattern
            for i in range(1, len(differences) - 1):
                if differences[i] < differences[i-1] * 0.5 and differences[i] < differences[i+1] * 0.5:
                    flicker += 0.1
                elif differences[i] > differences[i-1] * 2 and differences[i] > differences[i+1] * 2:
                    flicker += 0.1
        
        flicker = min(flicker, 1.0)
        
        return {
            'stability': np.clip(stability, 0, 1),
            'consistency': np.clip(consistency, 0, 1),
            'flicker': np.clip(flicker, 0, 1)
        }


class DetailPreservationAnalyzer:
    """Analyzes how well details are preserved."""
    
    def analyze(self, original: np.ndarray, processed: np.ndarray,
               mask: np.ndarray) -> Dict[str, float]:
        """Analyze detail preservation metrics."""
        # Convert to grayscale for analysis
        if len(original.shape) == 3:
            orig_gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
            proc_gray = cv2.cvtColor(processed, cv2.COLOR_BGR2GRAY)
        else:
            orig_gray = original
            proc_gray = processed
        
        # Focus on masked region
        mask_binary = mask > 0.5
        
        # Overall detail preservation using SSIM
        overall = 1.0
        if np.any(mask_binary):
            # Compute SSIM on masked region
            orig_masked = orig_gray * mask_binary
            proc_masked = proc_gray * mask_binary
            
            try:
                overall = skmetrics.structural_similarity(
                    orig_masked, proc_masked,
                    data_range=255
                )
            except:
                overall = 0.8  # Default if SSIM fails
        
        # Hair detail score (high-frequency preservation)
        hair_detail = self._analyze_hair_details(orig_gray, proc_gray, mask)
        
        # Texture quality (local variance preservation)
        texture = self._analyze_texture_quality(orig_gray, proc_gray, mask_binary)
        
        return {
            'overall': np.clip(overall, 0, 1),
            'hair_detail': np.clip(hair_detail, 0, 1),
            'texture': np.clip(texture, 0, 1)
        }
    
    def _analyze_hair_details(self, orig: np.ndarray, proc: np.ndarray,
                             mask: np.ndarray) -> float:
        """Analyze hair detail preservation."""
        # Use high-pass filter to extract fine details
        kernel = np.array([[-1, -1, -1],
                          [-1,  8, -1],
                          [-1, -1, -1]], dtype=np.float32)
        
        orig_details = cv2.filter2D(orig, -1, kernel)
        proc_details = cv2.filter2D(proc, -1, kernel)
        
        # Focus on edge regions (likely hair)
        edges = cv2.Canny((mask * 255).astype(np.uint8), 50, 150)
        edge_mask = edges > 0
        
        if np.any(edge_mask):
            # Compare detail preservation in edge regions
            orig_edge_details = np.abs(orig_details[edge_mask])
            proc_edge_details = np.abs(proc_details[edge_mask])
            
            # Compute correlation
            if len(orig_edge_details) > 0 and len(proc_edge_details) > 0:
                correlation = np.corrcoef(
                    orig_edge_details.flatten(),
                    proc_edge_details.flatten()
                )[0, 1]
                
                return (correlation + 1) / 2  # Normalize to [0, 1]
        
        return 0.8  # Default score
    
    def _analyze_texture_quality(self, orig: np.ndarray, proc: np.ndarray,
                                mask: np.ndarray) -> float:
        """Analyze texture preservation quality."""
        # Compute local variance (texture measure)
        window_size = 5
        
        def local_variance(img):
            mean = cv2.blur(img, (window_size, window_size))
            sqr_mean = cv2.blur(img**2, (window_size, window_size))
            variance = sqr_mean - mean**2
            return np.sqrt(np.maximum(variance, 0))
        
        orig_texture = local_variance(orig.astype(np.float32))
        proc_texture = local_variance(proc.astype(np.float32))
        
        # Compare texture in masked region
        if np.any(mask):
            orig_masked_texture = orig_texture[mask]
            proc_masked_texture = proc_texture[mask]
            
            if len(orig_masked_texture) > 0:
                # Compute texture similarity
                texture_diff = np.abs(orig_masked_texture - proc_masked_texture)
                max_texture = np.maximum(orig_masked_texture, proc_masked_texture) + 1e-6
                
                similarity = 1.0 - np.mean(texture_diff / max_texture)
                return similarity
        
        return 0.8  # Default score


class ArtifactDetector:
    """Detects various artifacts in processed frames."""
    
    def detect(self, frame: np.ndarray, mask: np.ndarray) -> Dict[str, Any]:
        """Detect artifacts in frame and mask."""
        artifacts = {
            'found': False,
            'types': [],
            'locations': []
        }
        
        # Check for halo artifacts
        if self._detect_halo(frame, mask):
            artifacts['found'] = True
            artifacts['types'].append('halo')
        
        # Check for color bleeding
        if self._detect_color_bleeding(frame, mask):
            artifacts['found'] = True
            artifacts['types'].append('color_bleeding')
        
        # Check for blocky artifacts
        if self._detect_blockiness(mask):
            artifacts['found'] = True
            artifacts['types'].append('blockiness')
        
        # Check for noise artifacts
        if self._detect_noise(mask):
            artifacts['found'] = True
            artifacts['types'].append('noise')
        
        return artifacts
    
    def _detect_halo(self, frame: np.ndarray, mask: np.ndarray) -> bool:
        """Detect halo artifacts around edges."""
        # Dilate mask to get outer region
        kernel = np.ones((5, 5), np.uint8)
        dilated = cv2.dilate((mask > 0.5).astype(np.uint8), kernel, iterations=2)
        
        # Get halo region (dilated - original)
        halo_region = dilated - (mask > 0.5).astype(np.uint8)
        
        if np.any(halo_region):
            # Check for unusual brightness in halo region
            halo_pixels = frame[halo_region > 0]
            if len(halo_pixels) > 0:
                mean_brightness = np.mean(halo_pixels)
                
                # Compare with overall image brightness
                overall_brightness = np.mean(frame)
                
                # Halo detected if halo region is significantly brighter/darker
                if abs(mean_brightness - overall_brightness) > 30:
                    return True
        
        return False
    
    def _detect_color_bleeding(self, frame: np.ndarray, mask: np.ndarray) -> bool:
        """Detect color bleeding at edges."""
        # Get edge region
        edges = cv2.Canny((mask * 255).astype(np.uint8), 50, 150)
        kernel = np.ones((3, 3), np.uint8)
        edge_region = cv2.dilate(edges, kernel, iterations=1) > 0
        
        if np.any(edge_region) and len(frame.shape) == 3:
            # Analyze color variance in edge region
            edge_pixels = frame[edge_region]
            
            if len(edge_pixels) > 0:
                # High color variance at edges might indicate bleeding
                color_std = np.std(edge_pixels, axis=0)
                
                if np.max(color_std) > 50:  # High variance threshold
                    return True
        
        return False
    
    def _detect_blockiness(self, mask: np.ndarray) -> bool:
        """Detect blocky artifacts in mask."""
        # Compute gradient
        grad_x = np.abs(np.diff(mask, axis=1))
        grad_y = np.abs(np.diff(mask, axis=0))
        
        # Look for regular patterns (blockiness)
        if grad_x.size > 0 and grad_y.size > 0:
            # FFT to detect regular patterns
            fft_x = np.fft.fft2(grad_x)
            fft_y = np.fft.fft2(grad_y)
            
            # Check for peaks at regular intervals (block boundaries)
            spectrum_x = np.abs(fft_x)
            spectrum_y = np.abs(fft_y)
            
            # Simple blockiness detection: high energy at specific frequencies
            blockiness_score = (np.max(spectrum_x) + np.max(spectrum_y)) / (spectrum_x.size + spectrum_y.size)
            
            if blockiness_score > 0.1:  # Threshold for blockiness
                return True
        
        return False
    
    def _detect_noise(self, mask: np.ndarray) -> bool:
        """Detect noise artifacts in mask."""
        # Compute local variance as noise measure
        mean = cv2.blur(mask, (3, 3))
        sqr_mean = cv2.blur(mask**2, (3, 3))
        variance = sqr_mean - mean**2
        
        # High variance in smooth regions indicates noise
        smooth_regions = (mask > 0.3) & (mask < 0.7)
        
        if np.any(smooth_regions):
            noise_level = np.mean(variance[smooth_regions])
            
            if noise_level > 0.05:  # Noise threshold
                return True
        
        return False


class MetricsTracker:
    """Tracks metrics over time for reporting."""
    
    def __init__(self, window_size: int = 100):
        self.window_size = window_size
        self.metrics_history = deque(maxlen=window_size)
        self.frame_times = deque(maxlen=window_size)
        
    def add(self, metrics: QualityMetrics, frame_time: float):
        """Add metrics to tracker."""
        self.metrics_history.append(metrics)
        self.frame_times.append(frame_time)
    
    def get_trends(self) -> Dict[str, List[float]]:
        """Get metric trends over time."""
        if not self.metrics_history:
            return {}
        
        trends = {
            'overall_quality': [],
            'edge_accuracy': [],
            'temporal_stability': [],
            'detail_preservation': []
        }
        
        for metrics in self.metrics_history:
            trends['overall_quality'].append(metrics.overall_quality)
            trends['edge_accuracy'].append(metrics.edge_accuracy)
            trends['temporal_stability'].append(metrics.temporal_stability)
            trends['detail_preservation'].append(metrics.detail_preservation)
        
        return trends
    
    def get_average_fps(self) -> float:
        """Get average FPS from frame times."""
        if len(self.frame_times) < 2:
            return 0.0
        
        time_diffs = [self.frame_times[i] - self.frame_times[i-1] 
                     for i in range(1, len(self.frame_times))]
        
        avg_time = np.mean(time_diffs)
        return 1.0 / avg_time if avg_time > 0 else 0.0


class QualityReport:
    """Generates quality reports."""
    
    @staticmethod
    def generate(metrics: QualityMetrics, 
                statistics: Dict[str, Any],
                output_path: Optional[str] = None) -> str:
        """Generate comprehensive quality report."""
        report = []
        report.append("=" * 60)
        report.append("BACKGROUNDFX PRO - QUALITY REPORT")
        report.append("=" * 60)
        report.append("")
        
        # Overall summary
        report.append(f"Overall Quality: {metrics.overall_quality:.1%}")
        report.append(f"Processing Confidence: {metrics.processing_confidence:.1%}")
        report.append("")
        
        # Detailed metrics
        report.append("DETAILED METRICS:")
        report.append("-" * 40)
        report.append(f"Edge Accuracy:        {metrics.edge_accuracy:.1%}")
        report.append(f"Edge Smoothness:      {metrics.edge_smoothness:.1%}")
        report.append(f"Edge Completeness:    {metrics.edge_completeness:.1%}")
        report.append("")
        report.append(f"Temporal Stability:   {metrics.temporal_stability:.1%}")
        report.append(f"Temporal Consistency: {metrics.temporal_consistency:.1%}")
        report.append(f"Flicker Score:        {metrics.flicker_score:.1%}")
        report.append("")
        report.append(f"Mask Coverage:        {metrics.mask_coverage:.1%}")
        report.append(f"Mask Accuracy:        {metrics.mask_accuracy:.1%}")
        report.append(f"Hole Ratio:          {metrics.hole_ratio:.1%}")
        report.append("")
        report.append(f"Detail Preservation:  {metrics.detail_preservation:.1%}")
        report.append(f"Hair Detail Score:    {metrics.hair_detail_score:.1%}")
        report.append(f"Texture Quality:      {metrics.texture_quality:.1%}")
        report.append("")
        
        # Warnings
        if metrics.warnings:
            report.append("WARNINGS:")
            report.append("-" * 40)
            for warning in metrics.warnings:
                report.append(f"⚠️  {warning}")
            report.append("")
        
        # Statistics
        if statistics:
            report.append("STATISTICS:")
            report.append("-" * 40)
            report.append(f"Average Quality: {statistics.get('average_quality', 0):.1%}")
            report.append(f"Min Quality:     {statistics.get('min_quality', 0):.1%}")
            report.append(f"Max Quality:     {statistics.get('max_quality', 0):.1%}")
            report.append(f"Std Deviation:   {statistics.get('std_quality', 0):.3f}")
            report.append(f"Total Warnings:  {statistics.get('total_warnings', 0)}")
            report.append(f"Frames Analyzed: {statistics.get('frames_analyzed', 0)}")
        
        report.append("")
        report.append("=" * 60)
        
        report_text = "\n".join(report)
        
        # Save if path provided
        if output_path:
            with open(output_path, 'w') as f:
                f.write(report_text)
        
        return report_text


# Export classes
__all__ = [
    'QualityAnalyzer',
    'QualityMetrics',
    'QualityConfig',
    'MetricsTracker',
    'QualityReport',
    'EdgeQualityAnalyzer',
    'TemporalQualityAnalyzer',
    'DetailPreservationAnalyzer',
    'ArtifactDetector'
]