Update app.py
Browse files
app.py
CHANGED
|
@@ -69,28 +69,50 @@ def predict_onnx(image: Image.Image):
|
|
| 69 |
image = image.resize((ONNX_IMG_SIZE, ONNX_IMG_SIZE))
|
| 70 |
|
| 71 |
img = np.array(image, dtype=np.float32) / 255.0
|
| 72 |
-
|
| 73 |
-
# HWC → CHW
|
| 74 |
-
img = np.transpose(img, (2, 0, 1))
|
| 75 |
img = np.expand_dims(img, axis=0)
|
| 76 |
|
| 77 |
-
|
| 78 |
[onnx_output_name],
|
| 79 |
{onnx_input_name: img}
|
| 80 |
-
)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
return {
|
| 85 |
"model": "ONNX Paregi/Pelise",
|
| 86 |
"prediction": ONNX_CLASSES[idx],
|
| 87 |
-
"confidence": float(
|
| 88 |
"probabilities": {
|
| 89 |
-
ONNX_CLASSES[i]: float(
|
| 90 |
for i in range(len(ONNX_CLASSES))
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
|
|
|
| 94 |
# -----------------------------
|
| 95 |
# MASTER PREDICT FUNCTION
|
| 96 |
# -----------------------------
|
|
|
|
| 69 |
image = image.resize((ONNX_IMG_SIZE, ONNX_IMG_SIZE))
|
| 70 |
|
| 71 |
img = np.array(image, dtype=np.float32) / 255.0
|
| 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] # shape: (N, attributes)
|
| 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 |
+
class_probs = det[5:]
|
| 89 |
+
class_scores.append(class_probs)
|
| 90 |
+
|
| 91 |
+
if not class_scores:
|
| 92 |
+
return {
|
| 93 |
+
"model": "ONNX Paregi/Pelise",
|
| 94 |
+
"prediction": "no_defect_detected",
|
| 95 |
+
"confidence": 0.0,
|
| 96 |
+
"probabilities": {
|
| 97 |
+
"paregi": 0.0,
|
| 98 |
+
"pelise": 0.0
|
| 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 |
+
|
| 116 |
# -----------------------------
|
| 117 |
# MASTER PREDICT FUNCTION
|
| 118 |
# -----------------------------
|