Spaces:
Sleeping
Sleeping
| # Step 1: Install Required Libraries | |
| # pip install streamlit tensorflow pillow huggingface_hub | |
| # Step 2: Import Necessary Libraries | |
| import streamlit as st | |
| import tensorflow as tf | |
| from tensorflow.keras.preprocessing.image import load_img, img_to_array | |
| from huggingface_hub import hf_hub_download | |
| import numpy as np | |
| import os | |
| # Step 3: Download Model from Hugging Face | |
| def load_model(): | |
| model_path = hf_hub_download(repo_id="krishnamishra8848/Brain_Tumor_Detection_Model", filename="brain_tumor_model.h5") | |
| return tf.keras.models.load_model(model_path) | |
| model = load_model() | |
| # Step 4: Define Prediction Function | |
| def predict_image(image_path): | |
| IMG_HEIGHT, IMG_WIDTH = 128, 128 # Ensure this matches your model's input size | |
| img = load_img(image_path, target_size=(IMG_HEIGHT, IMG_WIDTH)) # Resize image | |
| img_array = img_to_array(img) # Convert image to array | |
| img_array = np.expand_dims(img_array, axis=0) # Add batch dimension | |
| img_array = img_array / 255.0 # Normalize pixel values to [0, 1] | |
| prediction = model.predict(img_array)[0][0] | |
| confidence = prediction if prediction > 0.5 else 1 - prediction | |
| return prediction, confidence | |
| # Step 5: Streamlit Interface | |
| st.title("Brain Tumor Detection") | |
| st.write("Upload a CT scan image to classify it as Healthy or Tumor with confidence score.") | |
| uploaded_file = st.file_uploader("Upload CT Scan Image", type=["png", "jpg", "jpeg"]) | |
| if uploaded_file is not None: | |
| # Save the uploaded file to a temporary location | |
| temp_file_path = os.path.join("temp_image.jpg") | |
| with open(temp_file_path, "wb") as f: | |
| f.write(uploaded_file.getbuffer()) | |
| # Make prediction | |
| prediction, confidence = predict_image(temp_file_path) | |
| os.remove(temp_file_path) # Clean up the temporary file | |
| # Convert confidence to percentage | |
| confidence_percentage = confidence * 100 | |
| # Display the result | |
| if prediction > 0.5: | |
| st.markdown( | |
| f"<h2 style='color:red;'>Prediction: Tumor Detected with {confidence_percentage:.2f}% confidence.</h2>", | |
| unsafe_allow_html=True | |
| ) | |
| st.write("**Note:** Consult a medical professional for further evaluation.") | |
| else: | |
| st.markdown( | |
| f"<h2 style='color:green;'>Prediction: Healthy with {confidence_percentage:.2f}% confidence.</h2>", | |
| unsafe_allow_html=True | |
| ) | |
| st.write("**Note:** The result suggests a healthy scan. Regular checkups are recommended.") | |