Update app.py
Browse files
app.py
CHANGED
|
@@ -11,19 +11,38 @@ model = tf.keras.models.load_model(MODEL_PATH)
|
|
| 11 |
print("✅ Model loaded successfully")
|
| 12 |
|
| 13 |
# -----------------------------
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# -----------------------------
|
| 16 |
CLASS_NAMES = ["good", "breakage"]
|
| 17 |
-
IMG_SIZE = (64, 64) # 🔥 THIS IS THE KEY FIX
|
| 18 |
|
| 19 |
# -----------------------------
|
| 20 |
# PREDICTION FUNCTION
|
| 21 |
# -----------------------------
|
| 22 |
def predict(image: Image.Image):
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
img_array =
|
| 27 |
img_array = np.expand_dims(img_array, axis=0)
|
| 28 |
|
| 29 |
preds = model.predict(img_array)[0]
|
|
@@ -35,7 +54,8 @@ def predict(image: Image.Image):
|
|
| 35 |
"probabilities": {
|
| 36 |
CLASS_NAMES[i]: float(preds[i])
|
| 37 |
for i in range(len(CLASS_NAMES))
|
| 38 |
-
}
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
# -----------------------------
|
|
@@ -46,7 +66,7 @@ demo = gr.Interface(
|
|
| 46 |
inputs=gr.Image(type="pil", label="Upload O-Ring Image"),
|
| 47 |
outputs=gr.JSON(label="Prediction Output"),
|
| 48 |
title="O-Ring Defect Detection",
|
| 49 |
-
description="CNN-based classifier (
|
| 50 |
)
|
| 51 |
|
| 52 |
-
demo.launch()
|
|
|
|
| 11 |
print("✅ Model loaded successfully")
|
| 12 |
|
| 13 |
# -----------------------------
|
| 14 |
+
# READ MODEL INPUT SHAPE
|
| 15 |
+
# -----------------------------
|
| 16 |
+
# Example: (None, 128, 128, 3) or (None, 64, 64, 1)
|
| 17 |
+
_, H, W, C = model.input_shape
|
| 18 |
+
|
| 19 |
+
print(f"📐 Model expects: {H}x{W}x{C}")
|
| 20 |
+
|
| 21 |
+
# -----------------------------
|
| 22 |
+
# CONFIG
|
| 23 |
# -----------------------------
|
| 24 |
CLASS_NAMES = ["good", "breakage"]
|
|
|
|
| 25 |
|
| 26 |
# -----------------------------
|
| 27 |
# PREDICTION FUNCTION
|
| 28 |
# -----------------------------
|
| 29 |
def predict(image: Image.Image):
|
| 30 |
+
# Convert correctly based on channels
|
| 31 |
+
if C == 1:
|
| 32 |
+
image = image.convert("L") # grayscale
|
| 33 |
+
else:
|
| 34 |
+
image = image.convert("RGB")
|
| 35 |
+
|
| 36 |
+
# Resize EXACTLY to training size
|
| 37 |
+
image = image.resize((W, H))
|
| 38 |
+
|
| 39 |
+
img_array = np.array(image, dtype=np.float32)
|
| 40 |
+
|
| 41 |
+
# If grayscale, add channel dim
|
| 42 |
+
if C == 1:
|
| 43 |
+
img_array = np.expand_dims(img_array, axis=-1)
|
| 44 |
|
| 45 |
+
img_array = img_array / 255.0
|
| 46 |
img_array = np.expand_dims(img_array, axis=0)
|
| 47 |
|
| 48 |
preds = model.predict(img_array)[0]
|
|
|
|
| 54 |
"probabilities": {
|
| 55 |
CLASS_NAMES[i]: float(preds[i])
|
| 56 |
for i in range(len(CLASS_NAMES))
|
| 57 |
+
},
|
| 58 |
+
"model_input_shape": f"{H}x{W}x{C}"
|
| 59 |
}
|
| 60 |
|
| 61 |
# -----------------------------
|
|
|
|
| 66 |
inputs=gr.Image(type="pil", label="Upload O-Ring Image"),
|
| 67 |
outputs=gr.JSON(label="Prediction Output"),
|
| 68 |
title="O-Ring Defect Detection",
|
| 69 |
+
description="CNN-based classifier (auto-matches model input shape)"
|
| 70 |
)
|
| 71 |
|
| 72 |
+
demo.launch()
|