| | import gradio as gr |
| | import tensorflow as tf |
| | import numpy as np |
| | from PIL import Image |
| | import cv2 |
| |
|
| | |
| | model = tf.keras.models.load_model('brain_tumor_model.h5') |
| |
|
| | |
| | CLASS_LABELS = [ |
| | 'glioma_tumor', |
| | 'meningioma_tumor', |
| | 'no_tumor', |
| | 'pituitary_tumor' |
| | ] |
| |
|
| | def preprocess_image(image): |
| | """Preprocess image for Xception model""" |
| | |
| | img_array = np.array(image) |
| | |
| | |
| | img_resized = cv2.resize(img_array, (299, 299)) |
| | |
| | |
| | if len(img_resized.shape) == 3 and img_resized.shape[2] == 3: |
| | pass |
| | elif len(img_resized.shape) == 2: |
| | img_resized = cv2.cvtColor(img_resized, cv2.COLOR_GRAY2RGB) |
| | |
| | |
| | img_normalized = img_resized.astype('float32') / 255.0 |
| | |
| | |
| | img_batch = np.expand_dims(img_normalized, axis=0) |
| | |
| | return img_batch |
| |
|
| | def predict(image): |
| | """Make prediction on uploaded image""" |
| | if image is None: |
| | return "Please upload an image" |
| | |
| | try: |
| | |
| | processed_image = preprocess_image(image) |
| | |
| | |
| | predictions = model.predict(processed_image) |
| | |
| | |
| | probabilities = tf.nn.softmax(predictions[0]).numpy() |
| | |
| | |
| | results = {} |
| | for i, label in enumerate(CLASS_LABELS): |
| | results[label.replace('_', ' ').title()] = float(probabilities[i]) |
| | |
| | return results |
| | |
| | except Exception as e: |
| | return f"Error processing image: {str(e)}" |
| |
|
| | |
| | demo = gr.Interface( |
| | fn=predict, |
| | inputs=gr.Image(type="pil", label="Upload Brain MRI Scan"), |
| | outputs=gr.Label(num_top_classes=4, label="Prediction"), |
| | title="🧠 Brain Tumor Classification - Xception Model", |
| | description=""" |
| | Upload an MRI brain scan image to classify tumor types. |
| | |
| | **Model:** Sequential Xception Architecture |
| | **Accuracy:** 99% (on validation set) |
| | **Classes:** |
| | - Glioma Tumor |
| | - Meningioma Tumor |
| | - No Tumor |
| | - Pituitary Tumor |
| | |
| | ⚠️ **Disclaimer:** For research/educational purposes only. Not for medical diagnosis. |
| | """, |
| | examples=[ |
| | |
| | ], |
| | theme=gr.themes.Soft(), |
| | analytics_enabled=False |
| | ) |
| |
|
| | if __name__ == "__main__": |
| | demo.launch() |