|
import streamlit as st |
|
import numpy as np |
|
import cv2 |
|
import tensorflow as tf |
|
from PIL import Image |
|
from sklearn.preprocessing import LabelEncoder |
|
|
|
|
|
model = tf.keras.models.load_model('brain_tumor_model.h5') |
|
|
|
|
|
class_labels = ['glioma', 'pituitary', 'meningioma', 'healthy'] |
|
label_encoder = LabelEncoder() |
|
label_encoder.fit(class_labels) |
|
|
|
|
|
def load_and_preprocess_image(uploaded_file): |
|
img = Image.open(uploaded_file) |
|
img = img.convert("RGB") |
|
img = np.array(img) |
|
img = cv2.resize(img, (224, 224)) |
|
img = img / 255.0 |
|
img = np.reshape(img, (1, 224, 224, 3)) |
|
return img |
|
|
|
|
|
def predict_image(img): |
|
predictions = model.predict(img) |
|
predicted_class_index = np.argmax(predictions[0]) |
|
return predicted_class_index |
|
|
|
|
|
def get_class_label(predicted_class_index): |
|
return label_encoder.inverse_transform([predicted_class_index])[0] |
|
|
|
|
|
st.title("Brain Tumor using CNN 🧠") |
|
st.write("Upload a brain scan (JPG format), and the model will predict its class.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose a JPG image...", type="jpg") |
|
|
|
if uploaded_file is not None: |
|
|
|
col1, col2 = st.columns([2, 1]) |
|
|
|
with col1: |
|
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True) |
|
|
|
with col2: |
|
|
|
if st.button("Detect"): |
|
st.write("Detecting...") |
|
|
|
processed_image = load_and_preprocess_image(uploaded_file) |
|
|
|
|
|
predicted_class_index = predict_image(processed_image) |
|
|
|
|
|
predicted_class_label = get_class_label(predicted_class_index) |
|
|
|
|
|
st.markdown(f"<h3 style='color: #4CAF50; text-align: center;'>The Prediction is : <strong>{predicted_class_label}</strong></h3>", unsafe_allow_html=True) |
|
|
|
|