delete me
This commit is contained in:
48
inference.py
48
inference.py
@@ -31,7 +31,7 @@ class FruitVegetableDetector:
|
||||
print(f"Failed to load ONNX model: {e}. Falling back to SIMULATED mode.")
|
||||
self.session = None
|
||||
|
||||
def detect(self, image_rgb, confidence_threshold=0.10):
|
||||
def detect(self, image_rgb, confidence_threshold=0.85):
|
||||
"""
|
||||
Runs inference on an RGB image.
|
||||
Args:
|
||||
@@ -90,8 +90,54 @@ class FruitVegetableDetector:
|
||||
|
||||
# Sort predictions by confidence descending
|
||||
predictions = sorted(predictions, key=lambda x: x["confidence"], reverse=True)
|
||||
|
||||
# Apply Non-Maximum Suppression (NMS) to eliminate duplicate/overlapping boxes
|
||||
predictions = self.apply_nms(predictions, iou_threshold=0.5)
|
||||
|
||||
return predictions
|
||||
|
||||
def apply_nms(self, predictions, iou_threshold=0.5):
|
||||
"""
|
||||
Applies Non-Maximum Suppression (NMS) to predictions.
|
||||
"""
|
||||
if not predictions:
|
||||
return []
|
||||
|
||||
keep = []
|
||||
# Predictions are already sorted by confidence descending
|
||||
candidates = list(predictions)
|
||||
|
||||
while candidates:
|
||||
best = candidates.pop(0)
|
||||
keep.append(best)
|
||||
|
||||
remaining = []
|
||||
for pred in candidates:
|
||||
if self.calculate_iou(best["box"], pred["box"]) < iou_threshold:
|
||||
remaining.append(pred)
|
||||
candidates = remaining
|
||||
|
||||
return keep
|
||||
|
||||
def calculate_iou(self, box1, box2):
|
||||
"""
|
||||
Calculates Intersection over Union (IoU) of two boxes [xmin, ymin, xmax, ymax].
|
||||
"""
|
||||
x1 = max(box1[0], box2[0])
|
||||
y1 = max(box1[1], box2[1])
|
||||
x2 = min(box1[2], box2[2])
|
||||
y2 = min(box1[3], box2[3])
|
||||
|
||||
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
||||
|
||||
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
||||
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
||||
union = area1 + area2 - intersection
|
||||
|
||||
if union == 0:
|
||||
return 0
|
||||
return intersection / union
|
||||
|
||||
def _simulate_detection(self, width, height, confidence_threshold):
|
||||
"""
|
||||
Generates simulated detections based on random chances.
|
||||
|
||||
Reference in New Issue
Block a user