Yadvendra/Brain_Tumor_Detection_Using_Tensorflow
0
1import streamlit as st2import numpy as np3import cv24import tensorflow as tf5from PIL import Image6from sklearn.preprocessing import LabelEncoder7 8# Load your pre-trained model (Make sure this matches the version used during training)9model = tf.keras.models.load_model('brain_tumor_model.h5')10 11# Example class labels (update this list with your actual class labels)12class_labels = ['glioma', 'pituitary', 'meningioma', 'healthy']13label_encoder = LabelEncoder()14label_encoder.fit(class_labels) # Fit the label encoder with your class labels15 16# Function to load and preprocess the uploaded image17def 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 format20 img = np.array(img) # Convert to NumPy array21 img = cv2.resize(img, (224, 224)) # Resize the image to 224x22422 img = img / 255.0 # Normalize pixel values23 img = np.reshape(img, (1, 224, 224, 3)) # Reshape for prediction24 return img25 26# Function to predict the image class27def predict_image(img):28 predictions = model.predict(img) # Make a prediction29 predicted_class_index = np.argmax(predictions[0]) # Get the predicted class index30 return predicted_class_index31 32# Function to get class label33def get_class_label(predicted_class_index):34 return label_encoder.inverse_transform([predicted_class_index])[0] # Get class label35 36# Streamlit App UI37st.title("Brain Tumor using CNN 🧠")38st.write("Upload a brain scan (JPG format), and the model will predict its class.")39 40# File uploader for user to upload images41uploaded_file = st.file_uploader("Choose a JPG image...", type="jpg")42 43if uploaded_file is not None:44 # Display the uploaded image on the left side45 col1, col2 = st.columns([2, 1]) # Create two columns46 47 with col1:48 st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)49 50 with col2:51 # Button to trigger prediction52 if st.button("Detect"):53 st.write("Detecting...")54 # Load and preprocess the image55 processed_image = load_and_preprocess_image(uploaded_file)56 57 # Make prediction58 predicted_class_index = predict_image(processed_image)59 60 # Get predicted class label61 predicted_class_label = get_class_label(predicted_class_index)62 63 # Center display for the prediction result64 st.markdown(f"<h3 style='color: #4CAF50; text-align: center;'>The Prediction is : <strong>{predicted_class_label}</strong></h3>", unsafe_allow_html=True)65 66 