JosephRM commited on
Commit
9ebfda9
·
verified ·
1 Parent(s): 3a9b3aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
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
- outputs = onnx_session.run(
76
- [onnx_output_name],
 
77
  {onnx_input_name: img}
78
- )[0]
79
 
80
- detections = outputs[0]
81
 
82
- CONF_THRESH = 0.4
83
- class_scores = []
84
 
85
- for det in detections:
86
- conf = det[4]
87
- if conf > CONF_THRESH:
88
- # 👇 CRITICAL FIX
89
- class_probs = det[5:5 + len(ONNX_CLASSES)]
90
- class_scores.append(class_probs)
91
 
92
- if not class_scores:
 
 
 
 
 
 
 
93
  return {
94
  "model": "ONNX Paregi/Pelise",
95
- "prediction": "no_defect_detected",
96
  "confidence": 0.0,
97
- "probabilities": {
98
- cls: 0.0 for cls in ONNX_CLASSES
99
- }
100
  }
101
 
102
- avg_scores = np.mean(class_scores, axis=0)
103
- idx = int(np.argmax(avg_scores))
 
 
 
104
 
105
  return {
106
  "model": "ONNX Paregi/Pelise",
107
- "prediction": ONNX_CLASSES[idx],
108
- "confidence": float(avg_scores[idx]),
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