Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +89 -0
- brain_tumor_model.h5 +3 -0
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from keras.models import load_model
|
| 4 |
+
from keras.preprocessing import image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
from werkzeug.utils import secure_filename
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
# Configuration
|
| 12 |
+
UPLOAD_FOLDER = 'static/uploads'
|
| 13 |
+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
|
| 14 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 15 |
+
|
| 16 |
+
# Create upload folder if it doesn't exist
|
| 17 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
# Load model once at startup
|
| 20 |
+
model = None
|
| 21 |
+
|
| 22 |
+
def get_model():
|
| 23 |
+
global model
|
| 24 |
+
if model is None:
|
| 25 |
+
model = load_model('brain_tumor_model.h5')
|
| 26 |
+
return model
|
| 27 |
+
|
| 28 |
+
def allowed_file(filename):
|
| 29 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 30 |
+
|
| 31 |
+
def preprocess_image(img_path, target_size=(150, 150)): # ← Update this!
|
| 32 |
+
"""Preprocess image for model prediction"""
|
| 33 |
+
img = image.load_img(img_path, target_size=target_size)
|
| 34 |
+
img_array = image.img_to_array(img)
|
| 35 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 36 |
+
img_array = img_array / 255.0 # Normalize
|
| 37 |
+
return img_array
|
| 38 |
+
|
| 39 |
+
@app.route("/")
|
| 40 |
+
def index():
|
| 41 |
+
return render_template('index.html')
|
| 42 |
+
|
| 43 |
+
@app.route("/predict", methods=["POST"])
|
| 44 |
+
def predict():
|
| 45 |
+
if 'file' not in request.files:
|
| 46 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 47 |
+
|
| 48 |
+
file = request.files['file']
|
| 49 |
+
|
| 50 |
+
if file.filename == '':
|
| 51 |
+
return jsonify({"error": "No file selected"}), 400
|
| 52 |
+
|
| 53 |
+
if file and allowed_file(file.filename):
|
| 54 |
+
filename = secure_filename(file.filename)
|
| 55 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 56 |
+
file.save(filepath)
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
# Preprocess and predict
|
| 60 |
+
model = get_model()
|
| 61 |
+
processed_image = preprocess_image(filepath)
|
| 62 |
+
prediction = model.predict(processed_image)
|
| 63 |
+
|
| 64 |
+
# Adjust this based on your model's output
|
| 65 |
+
# For binary classification:
|
| 66 |
+
if prediction[0][0] > 0.5:
|
| 67 |
+
result = "Tumor Detected"
|
| 68 |
+
confidence = float(prediction[0][0]) * 100
|
| 69 |
+
else:
|
| 70 |
+
result = "No Tumor Detected"
|
| 71 |
+
confidence = (1 - float(prediction[0][0])) * 100
|
| 72 |
+
|
| 73 |
+
return jsonify({
|
| 74 |
+
"success": True,
|
| 75 |
+
"prediction": result,
|
| 76 |
+
"confidence": f"{confidence:.2f}%",
|
| 77 |
+
"image_path": filepath
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return jsonify({"error": str(e)}), 500
|
| 82 |
+
|
| 83 |
+
return jsonify({"error": "Invalid file type. Use PNG, JPG, or JPEG"}), 400
|
| 84 |
+
|
| 85 |
+
if __name__ == '__main__':
|
| 86 |
+
print("Loading model...")
|
| 87 |
+
get_model()
|
| 88 |
+
print("Model loaded successfully!")
|
| 89 |
+
app.run(debug=True)
|
brain_tumor_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d0014d21e4e24a683de89a3660faea5118bd816bee38fcc3654bace3102bb237
|
| 3 |
+
size 57990192
|