Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import numpy as np
|
| 4 |
import json
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
# Load model
|
| 8 |
model = tf.keras.models.load_model("polyp_efficientnet_model.h5")
|
|
@@ -14,12 +15,26 @@ with open("class_names.json") as f:
|
|
| 14 |
IMG_SIZE = (224, 224)
|
| 15 |
|
| 16 |
def predict(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
image = image.resize(IMG_SIZE)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
image = np.expand_dims(image, axis=0)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
preds = model.predict(image)[0]
|
| 22 |
-
idx = np.argmax(preds)
|
| 23 |
|
| 24 |
return {
|
| 25 |
class_names[i]: float(preds[i])
|
|
@@ -31,7 +46,7 @@ interface = gr.Interface(
|
|
| 31 |
inputs=gr.Image(type="pil"),
|
| 32 |
outputs=gr.Label(num_top_classes=4),
|
| 33 |
title="Polyp Disease Classification",
|
| 34 |
-
description="
|
| 35 |
)
|
| 36 |
|
| 37 |
interface.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import json
|
| 5 |
from PIL import Image
|
| 6 |
+
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 7 |
|
| 8 |
# Load model
|
| 9 |
model = tf.keras.models.load_model("polyp_efficientnet_model.h5")
|
|
|
|
| 15 |
IMG_SIZE = (224, 224)
|
| 16 |
|
| 17 |
def predict(image):
|
| 18 |
+
if image is None:
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
# Resize
|
| 22 |
image = image.resize(IMG_SIZE)
|
| 23 |
+
|
| 24 |
+
# Convert to numpy
|
| 25 |
+
image = np.array(image)
|
| 26 |
+
|
| 27 |
+
# Ensure RGB
|
| 28 |
+
if image.shape[-1] == 4:
|
| 29 |
+
image = image[..., :3]
|
| 30 |
+
|
| 31 |
+
# Batch dimension
|
| 32 |
image = np.expand_dims(image, axis=0)
|
| 33 |
|
| 34 |
+
# ✅ CORRECT preprocessing
|
| 35 |
+
image = preprocess_input(image)
|
| 36 |
+
|
| 37 |
preds = model.predict(image)[0]
|
|
|
|
| 38 |
|
| 39 |
return {
|
| 40 |
class_names[i]: float(preds[i])
|
|
|
|
| 46 |
inputs=gr.Image(type="pil"),
|
| 47 |
outputs=gr.Label(num_top_classes=4),
|
| 48 |
title="Polyp Disease Classification",
|
| 49 |
+
description="EfficientNet-based medical image classifier"
|
| 50 |
)
|
| 51 |
|
| 52 |
interface.launch()
|