andevs commited on
Commit
c5c0b4b
·
verified ·
1 Parent(s): 42dd5f0

Create maskQuality.py

Browse files
Files changed (1) hide show
  1. maskQuality.py +72 -0
maskQuality.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # maskQuality.py - Lightweight Mask Quality
2
+ # FROZEN - DO NOT MODIFY
3
+
4
+ import numpy as np
5
+ from scipy.ndimage import label
6
+
7
+ class MaskQualityChecker:
8
+ """Lightweight mask quality validation - FROZEN."""
9
+
10
+ def __init__(self):
11
+ self.min_foreground = 0.02
12
+ self.max_foreground = 0.95
13
+ self.max_border_ratio = 0.30
14
+ self.max_components = 100
15
+ self.min_confidence = 0.40
16
+
17
+ def check(self, mask: np.ndarray) -> dict:
18
+ if mask.dtype != np.uint8:
19
+ mask = (mask * 255).astype(np.uint8)
20
+
21
+ h, w = mask.shape
22
+
23
+ # Foreground ratio
24
+ fg = np.sum(mask > 128) / (h * w)
25
+
26
+ # Border ratio
27
+ border_pixels = 0
28
+ border_total = 0
29
+ for x in range(w):
30
+ if mask[0, x] > 128: border_pixels += 1
31
+ if mask[h-1, x] > 128: border_pixels += 1
32
+ border_total += 2
33
+ for y in range(1, h-1):
34
+ if mask[y, 0] > 128: border_pixels += 1
35
+ if mask[y, w-1] > 128: border_pixels += 1
36
+ border_total += 2
37
+ border_ratio = border_pixels / border_total if border_total > 0 else 0
38
+
39
+ # Components
40
+ labeled, num_components = label(mask > 128)
41
+ sizes = np.bincount(labeled.flatten())
42
+ small_components = sum(1 for s in sizes[1:] if s < 100)
43
+ fragmentation = small_components / num_components if num_components > 0 else 0
44
+
45
+ # Confidence
46
+ confidence = 1.0
47
+ issues = []
48
+
49
+ if fg < self.min_foreground:
50
+ confidence -= 0.3
51
+ issues.append('mask_nearly_empty')
52
+ if fg > self.max_foreground:
53
+ confidence -= 0.3
54
+ issues.append('mask_nearly_full')
55
+ if border_ratio > self.max_border_ratio:
56
+ confidence -= 0.2
57
+ issues.append('border_contamination')
58
+ if num_components > self.max_components:
59
+ confidence -= 0.1
60
+ issues.append('too_many_components')
61
+
62
+ confidence = max(0.0, min(1.0, confidence))
63
+
64
+ return {
65
+ 'passed': confidence >= self.min_confidence,
66
+ 'foreground_ratio': fg,
67
+ 'border_ratio': border_ratio,
68
+ 'components': num_components,
69
+ 'fragmentation': fragmentation,
70
+ 'confidence': confidence,
71
+ 'issues': issues
72
+ }