Update app.py
Browse files
app.py
CHANGED
|
@@ -65,6 +65,7 @@ def predict_cnn(image: Image.Image):
|
|
| 65 |
# ONNX PREDICTION (PAREGI / PELISE)
|
| 66 |
# -----------------------------
|
| 67 |
def predict_onnx(image: Image.Image):
|
|
|
|
| 68 |
image = image.convert("RGB")
|
| 69 |
image = image.resize((ONNX_IMG_SIZE, ONNX_IMG_SIZE))
|
| 70 |
|
|
@@ -72,44 +73,48 @@ def predict_onnx(image: Image.Image):
|
|
| 72 |
img = np.transpose(img, (2, 0, 1)) # HWC → CHW
|
| 73 |
img = np.expand_dims(img, axis=0)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
{onnx_input_name: img}
|
| 78 |
-
)[0]
|
| 79 |
|
| 80 |
-
|
| 81 |
|
| 82 |
-
CONF_THRESH = 0.
|
| 83 |
-
|
| 84 |
|
| 85 |
-
for det in
|
| 86 |
-
|
| 87 |
-
if
|
| 88 |
-
|
| 89 |
-
class_probs = det[5:5 + len(ONNX_CLASSES)]
|
| 90 |
-
class_scores.append(class_probs)
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
return {
|
| 94 |
"model": "ONNX Paregi/Pelise",
|
| 95 |
-
"prediction": "
|
| 96 |
"confidence": 0.0,
|
| 97 |
-
"probabilities": {
|
| 98 |
-
cls: 0.0 for cls in ONNX_CLASSES
|
| 99 |
-
}
|
| 100 |
}
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
return {
|
| 106 |
"model": "ONNX Paregi/Pelise",
|
| 107 |
-
"prediction": ONNX_CLASSES[
|
| 108 |
-
"confidence": float(
|
| 109 |
-
"probabilities":
|
| 110 |
-
ONNX_CLASSES[i]: float(avg_scores[i])
|
| 111 |
-
for i in range(len(ONNX_CLASSES))
|
| 112 |
-
}
|
| 113 |
}
|
| 114 |
|
| 115 |
|
|
|
|
| 65 |
# ONNX PREDICTION (PAREGI / PELISE)
|
| 66 |
# -----------------------------
|
| 67 |
def predict_onnx(image: Image.Image):
|
| 68 |
+
# --- Preprocess ---
|
| 69 |
image = image.convert("RGB")
|
| 70 |
image = image.resize((ONNX_IMG_SIZE, ONNX_IMG_SIZE))
|
| 71 |
|
|
|
|
| 73 |
img = np.transpose(img, (2, 0, 1)) # HWC → CHW
|
| 74 |
img = np.expand_dims(img, axis=0)
|
| 75 |
|
| 76 |
+
# --- Inference ---
|
| 77 |
+
output = onnx_session.run(
|
| 78 |
+
None,
|
| 79 |
{onnx_input_name: img}
|
| 80 |
+
)[0] # shape: (1, 84, N)
|
| 81 |
|
| 82 |
+
output = np.squeeze(output).T # → (N, 84)
|
| 83 |
|
| 84 |
+
CONF_THRESH = 0.15 # MUST match training/inference
|
| 85 |
+
detections = []
|
| 86 |
|
| 87 |
+
for det in output:
|
| 88 |
+
obj_conf = det[4]
|
| 89 |
+
if obj_conf < CONF_THRESH:
|
| 90 |
+
continue
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
class_probs = det[5:5 + len(ONNX_CLASSES)]
|
| 93 |
+
cls_id = int(np.argmax(class_probs))
|
| 94 |
+
cls_conf = class_probs[cls_id] * obj_conf
|
| 95 |
+
|
| 96 |
+
if cls_conf > CONF_THRESH:
|
| 97 |
+
detections.append((cls_id, cls_conf))
|
| 98 |
+
|
| 99 |
+
if not detections:
|
| 100 |
return {
|
| 101 |
"model": "ONNX Paregi/Pelise",
|
| 102 |
+
"prediction": "no_object_detected",
|
| 103 |
"confidence": 0.0,
|
| 104 |
+
"probabilities": {cls: 0.0 for cls in ONNX_CLASSES}
|
|
|
|
|
|
|
| 105 |
}
|
| 106 |
|
| 107 |
+
# --- Pick highest confidence detection ---
|
| 108 |
+
best_cls, best_conf = max(detections, key=lambda x: x[1])
|
| 109 |
+
|
| 110 |
+
probs = {cls: 0.0 for cls in ONNX_CLASSES}
|
| 111 |
+
probs[ONNX_CLASSES[best_cls]] = float(best_conf)
|
| 112 |
|
| 113 |
return {
|
| 114 |
"model": "ONNX Paregi/Pelise",
|
| 115 |
+
"prediction": ONNX_CLASSES[best_cls],
|
| 116 |
+
"confidence": float(best_conf),
|
| 117 |
+
"probabilities": probs
|
|
|
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
|
| 120 |
|