File size: 5,053 Bytes
9e629a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import cv2

class ThreatLabeler:
    def __init__(self):
        """
        Initialize the threat labeling system for deepfake detection
        """
        # Define threat level thresholds
        self.threat_levels = {
            'low': (0, 30),      # Low threat: 0-30% difference
            'medium': (30, 60),  # Medium threat: 30-60% difference
            'high': (60, 100)    # High threat: 60-100% difference
        }
        
        # Define colors for each threat level (BGR format for OpenCV)
        self.threat_colors = {
            'low': (0, 255, 0),      # Green
            'medium': (0, 165, 255), # Orange
            'high': (0, 0, 255)      # Red
        }
    
    def calculate_threat_level(self, diff_value):
        """
        Calculate threat level based on difference value
        
        Args:
            diff_value: Normalized difference value (0-100)
            
        Returns:
            Threat level string ('low', 'medium', or 'high')
        """
        for level, (min_val, max_val) in self.threat_levels.items():
            if min_val <= diff_value < max_val:
                return level
        return 'high'  # Default to high if outside ranges
    
    def calculate_region_threat(self, diff_image, bbox):
        """
        Calculate the threat level for a specific region
        
        Args:
            diff_image: Difference image (normalized 0-255)
            bbox: Bounding box (x, y, w, h)
            
        Returns:
            Threat level string and average difference value
        """
        x, y, w, h = bbox
        region = diff_image[y:y+h, x:x+w]
        
        # Calculate average difference in the region (normalized to 0-100)
        avg_diff = np.mean(region) / 255 * 100
        
        # Determine threat level
        threat_level = self.calculate_threat_level(avg_diff)
        
        return threat_level, avg_diff
    
    def label_regions(self, image, diff_image, bounding_boxes):
        """
        Label regions with threat levels and colors
        
        Args:
            image: Original image to label
            diff_image: Difference image
            bounding_boxes: List of bounding boxes (x, y, w, h)
            
        Returns:
            Labeled image and list of regions with threat levels
        """
        # Make a copy of the image to avoid modifying the original
        labeled_image = image.copy()
        labeled_regions = []
        
        # Process each bounding box
        for bbox in bounding_boxes:
            x, y, w, h = bbox
            
            # Calculate threat level for this region
            threat_level, avg_diff = self.calculate_region_threat(diff_image, bbox)
            
            # Get color for this threat level
            color = self.threat_colors[threat_level]
            
            # Draw colored rectangle based on threat level
            cv2.rectangle(labeled_image, (x, y), (x + w, y + h), color, 2)
            
            # Add label text with threat level and percentage
            label_text = f"{threat_level.upper()}: {avg_diff:.1f}%"
            cv2.putText(labeled_image, label_text, (x, y - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
            
            # Store labeled region info
            labeled_regions.append({
                'bbox': bbox,
                'threat_level': threat_level,
                'difference_percentage': avg_diff
            })
        
        return labeled_image, labeled_regions
    
    def get_threat_summary(self, labeled_regions):
        """
        Generate a summary of threat levels in the image
        
        Args:
            labeled_regions: List of labeled regions with threat levels
            
        Returns:
            Dictionary with threat summary statistics
        """
        if not labeled_regions:
            return {
                'total_regions': 0,
                'threat_counts': {'low': 0, 'medium': 0, 'high': 0},
                'max_threat': None,
                'average_difference': 0
            }
        
        # Count regions by threat level
        threat_counts = {'low': 0, 'medium': 0, 'high': 0}
        total_diff = 0
        max_threat = {'level': 'low', 'percentage': 0}
        
        for region in labeled_regions:
            level = region['threat_level']
            diff = region['difference_percentage']
            
            # Update counts
            threat_counts[level] += 1
            total_diff += diff
            
            # Track maximum threat
            if diff > max_threat['percentage']:
                max_threat = {'level': level, 'percentage': diff}
        
        # Calculate average difference
        avg_diff = total_diff / len(labeled_regions) if labeled_regions else 0
        
        return {
            'total_regions': len(labeled_regions),
            'threat_counts': threat_counts,
            'max_threat': max_threat,
            'average_difference': avg_diff
        }