sadek1 / app.py
Abdallahsadek's picture
Upload 3 files
bd3e4cc verified
from flask import Flask, render_template, request
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
import os
from werkzeug.utils import secure_filename
# Init app
app = Flask(__name__)
model = tf.keras.models.load_model("brain_stroke_model.keras")
# Upload folder
os.makedirs("static/uploads/", exist_ok=True)
UPLOAD_FOLDER = 'static/uploads/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Prediction function
def predict_image(img_path):
img = image.load_img(img_path, target_size=(150, 150))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) / 255.0 # Normalize
prediction = model.predict(img_array)
return prediction[0][0]
# Routes
@app.route('/', methods=['GET', 'POST'])
def index():
prediction = None
if request.method == 'POST':
file = request.files['file']
if file:
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
result = predict_image(filepath)
print('='*50)
print(result)
prediction = "Stroke Detected" if result >= 0.5 else "No Stroke"
return render_template('index.html', prediction=prediction, image=filepath)
return render_template('index.html', prediction=prediction)
if __name__ == '__main__':
app.run(debug=True)