Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,56 @@
|
|
1 |
import streamlit as st
|
2 |
-
import tensorflow as tf
|
3 |
-
from PIL import Image
|
4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Load model
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Define class labels
|
10 |
class_labels = ['Healthy', 'Moderate', 'Severe']
|
11 |
|
12 |
-
#
|
13 |
-
st.
|
14 |
|
15 |
-
#
|
16 |
-
uploaded_file
|
|
|
|
|
17 |
|
|
|
18 |
if uploaded_file:
|
19 |
-
|
20 |
-
image = Image
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
img_array = np.
|
26 |
-
if img_array.shape[-1] == 4: # if RGBA, convert to RGB
|
27 |
-
img_array = img_array[:, :, :3]
|
28 |
-
img_array = img_array / 255.0
|
29 |
-
img_array = np.expand_dims(img_array, axis=0)
|
30 |
|
31 |
# Predict
|
32 |
-
|
33 |
-
predicted_class = class_labels[np.argmax(
|
34 |
-
confidence = np.max(
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
# Show result
|
37 |
-
st.markdown(f"### 🩺 Predicted Class: **{predicted_class}**")
|
38 |
-
st.markdown(f"Confidence: `{confidence:.2f}`")
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing import image
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Set page config
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="Knee Osteoarthritis Classifier",
|
10 |
+
layout="centered",
|
11 |
+
initial_sidebar_state="auto"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Title
|
15 |
+
st.title("🔍 Knee Osteoarthritis Severity Classifier")
|
16 |
+
st.markdown("Upload a **knee X-ray** image to predict the severity level: **Healthy**, **Moderate**, or **Severe**.")
|
17 |
|
18 |
# Load model
|
19 |
+
@st.cache_resource
|
20 |
+
def load_effnet_model():
|
21 |
+
model = load_model("efficientnetb3_knee_oa.keras")
|
22 |
+
return model
|
23 |
+
|
24 |
+
model = load_effnet_model()
|
25 |
|
26 |
# Define class labels
|
27 |
class_labels = ['Healthy', 'Moderate', 'Severe']
|
28 |
|
29 |
+
# Image uploader
|
30 |
+
uploaded_file = st.file_uploader("📤 Upload an X-ray image (PNG/JPG)", type=["jpg", "jpeg", "png"])
|
31 |
|
32 |
+
# Or show default image
|
33 |
+
if uploaded_file is None:
|
34 |
+
st.info("No image uploaded. Displaying a default sample image.")
|
35 |
+
uploaded_file = "sample_image.png"
|
36 |
|
37 |
+
# Prediction and result
|
38 |
if uploaded_file:
|
39 |
+
img = Image.open(uploaded_file).convert("RGB")
|
40 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
41 |
+
|
42 |
+
# Preprocess image
|
43 |
+
img_resized = img.resize((224, 224))
|
44 |
+
img_array = image.img_to_array(img_resized)
|
45 |
+
img_array = np.expand_dims(img_array, axis=0) / 255.0
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# Predict
|
48 |
+
preds = model.predict(img_array)
|
49 |
+
predicted_class = class_labels[np.argmax(preds)]
|
50 |
+
confidence = np.max(preds)
|
51 |
+
|
52 |
+
# Output
|
53 |
+
st.subheader("🔬 Prediction:")
|
54 |
+
st.success(f"**{predicted_class}** with {confidence*100:.2f}% confidence.")
|
55 |
+
|
56 |
|
|
|
|
|
|