File size: 10,641 Bytes
347d1a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Confidence scoring utilities.

This module handles:
- Card detection confidence
- Finger detection confidence
- Measurement stability confidence
- Edge quality confidence (v1)
- Aggregate confidence calculation

All thresholds and weights are imported from confidence_constants.py.
"""

import logging
import numpy as np
from typing import Dict, Any, Optional, Literal

from .confidence_constants import (
    # Card confidence constants
    CARD_IDEAL_ASPECT_RATIO,
    CARD_MAX_ASPECT_DEVIATION,
    CARD_WEIGHT_DETECTION,
    CARD_WEIGHT_ASPECT,
    CARD_WEIGHT_SCALE,
    # Finger confidence constants
    FINGER_IDEAL_MIN_AREA_FRACTION,
    FINGER_IDEAL_MAX_AREA_FRACTION,
    FINGER_WEIGHT_HAND_DETECTION,
    FINGER_WEIGHT_MASK_VALIDITY,
    # Measurement confidence constants
    MEASUREMENT_CV_POOR,
    MEASUREMENT_CONSISTENCY_THRESHOLD,
    MEASUREMENT_OUTLIER_STD_MULTIPLIER,
    MEASUREMENT_WIDTH_TYPICAL_MIN,
    MEASUREMENT_WIDTH_TYPICAL_MAX,
    MEASUREMENT_WIDTH_ABSOLUTE_MIN,
    MEASUREMENT_WIDTH_ABSOLUTE_MAX,
    MEASUREMENT_WEIGHT_VARIANCE,
    MEASUREMENT_WEIGHT_CONSISTENCY,
    MEASUREMENT_WEIGHT_OUTLIERS,
    MEASUREMENT_WEIGHT_RANGE,
    MEASUREMENT_RANGE_SCORE_IDEAL,
    MEASUREMENT_RANGE_SCORE_BORDERLINE,
    MEASUREMENT_RANGE_SCORE_OUTSIDE,
    # Overall confidence constants
    V0_WEIGHT_CARD,
    V0_WEIGHT_FINGER,
    V0_WEIGHT_MEASUREMENT,
    V1_WEIGHT_CARD,
    V1_WEIGHT_FINGER,
    V1_WEIGHT_EDGE_QUALITY,
    V1_WEIGHT_MEASUREMENT,
    CONFIDENCE_LEVEL_HIGH_THRESHOLD,
    CONFIDENCE_LEVEL_MEDIUM_THRESHOLD,
)

logger = logging.getLogger(__name__)

EdgeMethod = Literal["contour", "sobel", "sobel_fallback"]


def compute_card_confidence(
    card_result: Dict[str, Any],
    scale_confidence: float,
) -> float:
    """
    Compute confidence score from card detection.

    Uses constants:
    - CARD_IDEAL_ASPECT_RATIO: ISO/IEC 7810 ID-1 aspect ratio
    - CARD_MAX_ASPECT_DEVIATION: Maximum acceptable deviation (0.15)
    - CARD_WEIGHT_*: Component weights (detection: 50%, aspect: 25%, scale: 25%)

    Args:
        card_result: Output from detect_credit_card()
        scale_confidence: Scale calibration confidence

    Returns:
        Card confidence score [0, 1]
    """
    # Base confidence from card detection
    detection_conf = card_result.get("confidence", 0.0)

    # Aspect ratio deviation penalty
    aspect_ratio = card_result.get("aspect_ratio", 0.0)
    aspect_deviation = abs(aspect_ratio - CARD_IDEAL_ASPECT_RATIO) / CARD_IDEAL_ASPECT_RATIO

    # Penalize deviation beyond threshold
    aspect_score = max(0, 1.0 - (aspect_deviation / CARD_MAX_ASPECT_DEVIATION))

    # Combine components with weights
    card_conf = (
        CARD_WEIGHT_DETECTION * detection_conf +
        CARD_WEIGHT_ASPECT * aspect_score +
        CARD_WEIGHT_SCALE * scale_confidence
    )

    return float(np.clip(card_conf, 0, 1))


def compute_finger_confidence(
    hand_data: Dict[str, Any],
    finger_data: Dict[str, Any],
    mask_area: int,
    image_area: int,
) -> float:
    """
    Compute confidence score from finger detection.

    Uses constants:
    - FINGER_IDEAL_MIN_AREA_FRACTION: Minimum ideal mask area (0.5% of image)
    - FINGER_IDEAL_MAX_AREA_FRACTION: Maximum ideal mask area (5% of image)
    - FINGER_WEIGHT_*: Component weights (hand: 70%, mask: 30%)

    Args:
        hand_data: Output from segment_hand()
        finger_data: Output from isolate_finger()
        mask_area: Area of cleaned finger mask in pixels
        image_area: Total image area in pixels

    Returns:
        Finger confidence score [0, 1]
    """
    # Hand landmark detection confidence from MediaPipe
    hand_conf = hand_data.get("confidence", 0.0)

    # Mask area validity (should be reasonable fraction of image)
    mask_fraction = mask_area / image_area
    # Ideal range: FINGER_IDEAL_MIN_AREA_FRACTION to FINGER_IDEAL_MAX_AREA_FRACTION
    if mask_fraction < FINGER_IDEAL_MIN_AREA_FRACTION:
        area_score = mask_fraction / FINGER_IDEAL_MIN_AREA_FRACTION
    elif mask_fraction > FINGER_IDEAL_MAX_AREA_FRACTION:
        area_score = max(0, 1.0 - (mask_fraction - FINGER_IDEAL_MAX_AREA_FRACTION) / FINGER_IDEAL_MAX_AREA_FRACTION)
    else:
        area_score = 1.0

    # Combine components with weights
    finger_conf = FINGER_WEIGHT_HAND_DETECTION * hand_conf + FINGER_WEIGHT_MASK_VALIDITY * area_score

    return float(np.clip(finger_conf, 0, 1))


def compute_measurement_confidence(
    width_data: Dict[str, Any],
    median_width_cm: float,
) -> float:
    """
    Compute confidence score from measurement stability.

    Uses constants:
    - MEASUREMENT_CV_POOR: Coefficient of variation threshold (0.15)
    - MEASUREMENT_CONSISTENCY_THRESHOLD: Median-mean difference threshold (0.1)
    - MEASUREMENT_OUTLIER_STD_MULTIPLIER: Outlier detection threshold (2.0)
    - MEASUREMENT_WIDTH_*: Realistic width ranges (1.0-3.0 cm)
    - MEASUREMENT_WEIGHT_*: Component weights (variance: 40%, consistency: 20%, outliers: 20%, range: 20%)
    - MEASUREMENT_RANGE_SCORE_*: Range score values

    Args:
        width_data: Output from compute_cross_section_width()
        median_width_cm: Median width in centimeters

    Returns:
        Measurement confidence score [0, 1]
    """
    widths_px = np.array(width_data.get("widths_px", []))

    if len(widths_px) == 0:
        return 0.0

    median_px = width_data.get("median_width_px", 0.0)
    mean_px = width_data.get("mean_width_px", 0.0)
    std_px = width_data.get("std_width_px", 0.0)

    # 1. Variance score (lower variance = higher confidence)
    coefficient_of_variation = std_px / (median_px + 1e-8)
    # CV < MEASUREMENT_CV_POOR is acceptable
    variance_score = max(0, 1.0 - coefficient_of_variation / MEASUREMENT_CV_POOR)

    # 2. Median-Mean consistency
    median_mean_diff = abs(median_px - mean_px) / (median_px + 1e-8)
    consistency_score = max(0, 1.0 - median_mean_diff / MEASUREMENT_CONSISTENCY_THRESHOLD)

    # 3. Outlier ratio (measurements far from median)
    outlier_threshold = MEASUREMENT_OUTLIER_STD_MULTIPLIER * std_px
    outliers = np.sum(np.abs(widths_px - median_px) > outlier_threshold)
    outlier_ratio = outliers / len(widths_px)
    outlier_score = max(0, 1.0 - outlier_ratio)

    # 4. Realistic range check
    if MEASUREMENT_WIDTH_TYPICAL_MIN <= median_width_cm <= MEASUREMENT_WIDTH_TYPICAL_MAX:
        range_score = MEASUREMENT_RANGE_SCORE_IDEAL
    elif MEASUREMENT_WIDTH_ABSOLUTE_MIN <= median_width_cm <= MEASUREMENT_WIDTH_ABSOLUTE_MAX:
        # Borderline acceptable
        range_score = MEASUREMENT_RANGE_SCORE_BORDERLINE
    else:
        # Outside realistic range
        range_score = MEASUREMENT_RANGE_SCORE_OUTSIDE

    # Combine components with weights
    measurement_conf = (
        MEASUREMENT_WEIGHT_VARIANCE * variance_score +
        MEASUREMENT_WEIGHT_CONSISTENCY * consistency_score +
        MEASUREMENT_WEIGHT_OUTLIERS * outlier_score +
        MEASUREMENT_WEIGHT_RANGE * range_score
    )

    return float(np.clip(measurement_conf, 0, 1))


def compute_edge_quality_confidence(
    edge_quality_data: Optional[Dict[str, Any]] = None
) -> float:
    """
    Compute confidence score from edge quality (v1 Sobel method).

    Args:
        edge_quality_data: Output from compute_edge_quality_score()
                          None if using contour method (v0)

    Returns:
        Edge quality confidence score [0, 1]
        Returns 1.0 for contour method (not applicable)
    """
    if edge_quality_data is None:
        # Contour method - edge quality not applicable
        return 1.0

    # Use overall edge quality score directly
    # It's already a weighted combination of 4 metrics
    edge_conf = edge_quality_data.get("overall_score", 0.0)

    return float(np.clip(edge_conf, 0, 1))


def compute_overall_confidence(
    card_confidence: float,
    finger_confidence: float,
    measurement_confidence: float,
    edge_method: EdgeMethod = "contour",
    edge_quality_confidence: Optional[float] = None,
) -> Dict[str, Any]:
    """
    Compute overall confidence by combining component scores.

    Supports both v0 (contour) and v1 (Sobel) confidence calculation:
    - v0 (contour): 3 components with V0_WEIGHT_* constants
    - v1 (sobel): 4 components with V1_WEIGHT_* constants

    Uses constants:
    - V0_WEIGHT_*: v0 component weights (card: 30%, finger: 30%, measurement: 40%)
    - V1_WEIGHT_*: v1 component weights (card: 25%, finger: 25%, edge: 20%, measurement: 30%)
    - CONFIDENCE_LEVEL_*_THRESHOLD: Level thresholds (high: >0.85, medium: >=0.6)

    Args:
        card_confidence: Card detection confidence
        finger_confidence: Finger detection confidence
        measurement_confidence: Measurement stability confidence
        edge_method: Edge detection method used
        edge_quality_confidence: Edge quality confidence (v1 only)

    Returns:
        Dictionary containing:
        - overall: Overall confidence [0, 1]
        - card: Card component score
        - finger: Finger component score
        - measurement: Measurement component score
        - edge_quality: Edge quality score (v1 only, None for v0)
        - level: "high", "medium", or "low"
        - method: Edge method used
    """
    result = {
        "card": float(card_confidence),
        "finger": float(finger_confidence),
        "measurement": float(measurement_confidence),
        "method": edge_method,
    }

    # Calculate overall confidence based on method
    if edge_method == "sobel" and edge_quality_confidence is not None:
        # v1 scoring: 4 components with V1_WEIGHT_* constants
        overall = (
            V1_WEIGHT_CARD * card_confidence +
            V1_WEIGHT_FINGER * finger_confidence +
            V1_WEIGHT_EDGE_QUALITY * edge_quality_confidence +
            V1_WEIGHT_MEASUREMENT * measurement_confidence
        )
        result["edge_quality"] = float(edge_quality_confidence)

    else:
        # v0 scoring: 3 components with V0_WEIGHT_* constants (contour method or sobel fallback)
        overall = (
            V0_WEIGHT_CARD * card_confidence +
            V0_WEIGHT_FINGER * finger_confidence +
            V0_WEIGHT_MEASUREMENT * measurement_confidence
        )
        result["edge_quality"] = None

    overall = float(np.clip(overall, 0, 1))

    # Classify confidence level using threshold constants
    if overall > CONFIDENCE_LEVEL_HIGH_THRESHOLD:
        level = "high"
    elif overall >= CONFIDENCE_LEVEL_MEDIUM_THRESHOLD:
        level = "medium"
    else:
        level = "low"

    result["overall"] = overall
    result["level"] = level

    return result