Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,65 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from tensorflow.keras.models import load_model
|
| 3 |
-
from tensorflow.keras.preprocessing import image
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Function to load and preprocess the uploaded image
|
| 8 |
-
def load_and_preprocess_image(
|
| 9 |
-
img = Image.open(
|
| 10 |
-
img = img.
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Streamlit App UI
|
| 23 |
-
st.title("Brain Tumor using CNN")
|
| 24 |
st.write("Upload a brain scan (JPG format), and the model will predict its class.")
|
| 25 |
|
| 26 |
-
#
|
| 27 |
uploaded_file = st.file_uploader("Choose a JPG image...", type="jpg")
|
| 28 |
|
| 29 |
-
# If an image is uploaded, process it
|
| 30 |
if uploaded_file is not None:
|
| 31 |
-
|
| 32 |
-
st.
|
| 33 |
-
|
| 34 |
-
# Preprocess the uploaded image
|
| 35 |
-
processed_image = load_and_preprocess_image(uploaded_file, target_size=(224, 224))
|
| 36 |
-
|
| 37 |
-
# Load the model
|
| 38 |
-
model = load_cnn_model()
|
| 39 |
-
|
| 40 |
-
# Make predictions
|
| 41 |
-
predictions = model.predict(processed_image)
|
| 42 |
-
predicted_class = np.argmax(predictions, axis=1)
|
| 43 |
-
|
| 44 |
-
# Map the class index to the actual class names
|
| 45 |
-
class_names = ['glioma', 'pituitary', 'meningioma', 'healthy']
|
| 46 |
-
result = class_names[predicted_class[0]]
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import tensorflow as tf
|
| 5 |
from PIL import Image
|
| 6 |
+
from sklearn.preprocessing import LabelEncoder
|
| 7 |
+
|
| 8 |
+
# Load your pre-trained model (Make sure this matches the version used during training)
|
| 9 |
+
model = tf.keras.models.load_model('dementia_cnn_model.h5')
|
| 10 |
+
|
| 11 |
+
# Example class labels (update this list with your actual class labels)
|
| 12 |
+
class_labels = ['glioma', 'pituitary', 'meningioma', 'healthy']
|
| 13 |
+
label_encoder = LabelEncoder()
|
| 14 |
+
label_encoder.fit(class_labels) # Fit the label encoder with your class labels
|
| 15 |
|
| 16 |
# Function to load and preprocess the uploaded image
|
| 17 |
+
def load_and_preprocess_image(uploaded_file):
|
| 18 |
+
img = Image.open(uploaded_file)
|
| 19 |
+
img = img.convert("RGB") # Convert to RGB if it's in another format
|
| 20 |
+
img = np.array(img) # Convert to NumPy array
|
| 21 |
+
img = cv2.resize(img, (224, 224)) # Resize the image to 224x224
|
| 22 |
+
img = img / 255.0 # Normalize pixel values
|
| 23 |
+
img = np.reshape(img, (1, 224, 224, 3)) # Reshape for prediction
|
| 24 |
+
return img
|
| 25 |
+
|
| 26 |
+
# Function to predict the image class
|
| 27 |
+
def predict_image(img):
|
| 28 |
+
predictions = model.predict(img) # Make a prediction
|
| 29 |
+
predicted_class_index = np.argmax(predictions[0]) # Get the predicted class index
|
| 30 |
+
return predicted_class_index
|
| 31 |
+
|
| 32 |
+
# Function to get class label
|
| 33 |
+
def get_class_label(predicted_class_index):
|
| 34 |
+
return label_encoder.inverse_transform([predicted_class_index])[0] # Get class label
|
| 35 |
|
| 36 |
# Streamlit App UI
|
| 37 |
+
st.title("Brain Tumor using CNN 🧠")
|
| 38 |
st.write("Upload a brain scan (JPG format), and the model will predict its class.")
|
| 39 |
|
| 40 |
+
# File uploader for user to upload images
|
| 41 |
uploaded_file = st.file_uploader("Choose a JPG image...", type="jpg")
|
| 42 |
|
|
|
|
| 43 |
if uploaded_file is not None:
|
| 44 |
+
# Display the uploaded image on the left side
|
| 45 |
+
col1, col2 = st.columns([2, 1]) # Create two columns
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
with col1:
|
| 48 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 49 |
+
|
| 50 |
+
with col2:
|
| 51 |
+
# Button to trigger prediction
|
| 52 |
+
if st.button("Detect"):
|
| 53 |
+
st.write("Detecting...")
|
| 54 |
+
# Load and preprocess the image
|
| 55 |
+
processed_image = load_and_preprocess_image(uploaded_file)
|
| 56 |
+
|
| 57 |
+
# Make prediction
|
| 58 |
+
predicted_class_index = predict_image(processed_image)
|
| 59 |
+
|
| 60 |
+
# Get predicted class label
|
| 61 |
+
predicted_class_label = get_class_label(predicted_class_index)
|
| 62 |
+
|
| 63 |
+
# Center display for the prediction result
|
| 64 |
+
st.markdown(f"<h3 style='color: #4CAF50; text-align: center;'>The Prediction is : <strong>{predicted_class_label}</strong></h3>", unsafe_allow_html=True)
|
| 65 |
|