Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import streamlit as stfrom tensorflow.keras.models import load_modelfrom tensorflow.keras.preprocessing import imageimport numpy as npfrom PIL import Image
# Function to load and preprocess the uploaded imagedef load_and_preprocess_image(uploaded_image, target_size=(224, 224)): img = Image.open(uploaded_image) # Open the uploaded image img = img.resize(target_size) # Resize to match model's input shape img_array = np.array(img) # Convert to numpy array img_array = np.expand_dims(img_array, axis=0) # Add batch dimension img_array = img_array / 255.0 # Normalize pixel values return img_array
# Load your pre-trained model (assuming it's in HDF5 format)@st.cache(allow_output_mutation=True) # Cache the model to avoid reloading it each timedef load_cnn_model(): model = load_model('brain_tumor_model.h5') # Replace with your model path return model
# Streamlit App UIst.title("Brain Tumor using CNN")st.write("Upload a brain scan (JPG format), and the model will predict its class.")
# Upload image buttonuploaded_file = st.file_uploader("Choose a JPG image...", type="jpg")
# If an image is uploaded, process itif uploaded_file is not None: st.image(uploaded_file, caption="Uploaded Image", use_column_width=True) st.write("Classifying...")
# Preprocess the uploaded image processed_image = load_and_preprocess_image(uploaded_file, target_size=(224, 224))
# Load the model model = load_cnn_model()
# Make predictions predictions = model.predict(processed_image) predicted_class = np.argmax(predictions, axis=1)
# Map the class index to the actual class names class_names = ['glioma', 'pituitary', 'meningioma', 'healthy'] result = class_names[predicted_class[0]]
# Display the prediction st.write(f"Predicted Class: **{result}**")else: st.write("Please upload an image to classify.")
app.pyAdded+52