Update app.py
Browse filesFilter filter code update
app.py
CHANGED
@@ -92,7 +92,7 @@ def filter_bbox_outliers(detections: List[Dict],
|
|
92 |
|
93 |
Args:
|
94 |
detections: List of detection dictionaries with 'bbox', 'label', 'score'
|
95 |
-
method: 'iqr' (Interquartile Range)
|
96 |
threshold: Multiplier for IQR method or Z-score threshold
|
97 |
min_score: Minimum confidence score to keep detection
|
98 |
|
@@ -109,8 +109,20 @@ def filter_bbox_outliers(detections: List[Dict],
|
|
109 |
areas = [calculate_bbox_area(det['bbox']) for det in detections]
|
110 |
areas = np.array(areas)
|
111 |
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
# Z-score method
|
115 |
mean_area = np.mean(areas)
|
116 |
std_area = np.std(areas)
|
@@ -118,9 +130,8 @@ def filter_bbox_outliers(detections: List[Dict],
|
|
118 |
z_scores = np.abs((areas - mean_area) / std_area)
|
119 |
valid_indices = np.where(z_scores <= threshold)[0]
|
120 |
|
121 |
-
|
122 |
else:
|
123 |
-
raise ValueError("Method must be 'iqr'
|
124 |
|
125 |
# Return filtered detections
|
126 |
filtered_detections = [detections[i] for i in valid_indices]
|
@@ -167,7 +178,7 @@ def detect_objects_owlv2(text_query, image, threshold=0.1):
|
|
167 |
detections.append(detection)
|
168 |
|
169 |
print(detections)
|
170 |
-
return filter_bbox_outliers(detections,method = '
|
171 |
|
172 |
except Exception as e:
|
173 |
print(f"Detection error: {e}")
|
|
|
92 |
|
93 |
Args:
|
94 |
detections: List of detection dictionaries with 'bbox', 'label', 'score'
|
95 |
+
method: 'iqr' (Interquartile Range) or 'zscore' (Z-score) or 'percentile'
|
96 |
threshold: Multiplier for IQR method or Z-score threshold
|
97 |
min_score: Minimum confidence score to keep detection
|
98 |
|
|
|
109 |
areas = [calculate_bbox_area(det['bbox']) for det in detections]
|
110 |
areas = np.array(areas)
|
111 |
|
112 |
+
if method == 'iqr':
|
113 |
+
# IQR method
|
114 |
+
q1 = np.percentile(areas, 25)
|
115 |
+
q3 = np.percentile(areas, 75)
|
116 |
+
iqr = q3 - q1
|
117 |
|
118 |
+
# Define outlier boundaries
|
119 |
+
lower_bound = q1 - threshold * iqr
|
120 |
+
upper_bound = q3 + threshold * iqr
|
121 |
+
|
122 |
+
# Find valid indices (non-outliers)
|
123 |
+
valid_indices = np.where((areas >= lower_bound) & (areas <= upper_bound))[0]
|
124 |
+
|
125 |
+
elif method == 'zscore':
|
126 |
# Z-score method
|
127 |
mean_area = np.mean(areas)
|
128 |
std_area = np.std(areas)
|
|
|
130 |
z_scores = np.abs((areas - mean_area) / std_area)
|
131 |
valid_indices = np.where(z_scores <= threshold)[0]
|
132 |
|
|
|
133 |
else:
|
134 |
+
raise ValueError("Method must be 'iqr' or 'zscore'")
|
135 |
|
136 |
# Return filtered detections
|
137 |
filtered_detections = [detections[i] for i in valid_indices]
|
|
|
178 |
detections.append(detection)
|
179 |
|
180 |
print(detections)
|
181 |
+
return filter_bbox_outliers(detections,method = 'zscore'),image
|
182 |
|
183 |
except Exception as e:
|
184 |
print(f"Detection error: {e}")
|