Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +43 -0
- index.html +67 -0
- requirements (1).txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow.keras.preprocessing import image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
from werkzeug.utils import secure_filename
|
| 7 |
+
|
| 8 |
+
# Init app
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
model = tf.keras.models.load_model("brain_stroke_model.keras")
|
| 11 |
+
|
| 12 |
+
# Upload folder
|
| 13 |
+
os.makedirs("static/uploads/", exist_ok=True)
|
| 14 |
+
UPLOAD_FOLDER = 'static/uploads/'
|
| 15 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 16 |
+
|
| 17 |
+
# Prediction function
|
| 18 |
+
def predict_image(img_path):
|
| 19 |
+
img = image.load_img(img_path, target_size=(150, 150))
|
| 20 |
+
img_array = image.img_to_array(img)
|
| 21 |
+
img_array = np.expand_dims(img_array, axis=0) / 255.0 # Normalize
|
| 22 |
+
prediction = model.predict(img_array)
|
| 23 |
+
return prediction[0][0]
|
| 24 |
+
|
| 25 |
+
# Routes
|
| 26 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 27 |
+
def index():
|
| 28 |
+
prediction = None
|
| 29 |
+
if request.method == 'POST':
|
| 30 |
+
file = request.files['file']
|
| 31 |
+
if file:
|
| 32 |
+
filename = secure_filename(file.filename)
|
| 33 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 34 |
+
file.save(filepath)
|
| 35 |
+
result = predict_image(filepath)
|
| 36 |
+
print('='*50)
|
| 37 |
+
print(result)
|
| 38 |
+
prediction = "Stroke Detected" if result >= 0.5 else "No Stroke"
|
| 39 |
+
return render_template('index.html', prediction=prediction, image=filepath)
|
| 40 |
+
return render_template('index.html', prediction=prediction)
|
| 41 |
+
|
| 42 |
+
if __name__ == '__main__':
|
| 43 |
+
app.run(debug=True)
|
index.html
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>Brain Stroke Prediction</title>
|
| 6 |
+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
background: #f8f9fa;
|
| 10 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 11 |
+
padding-top: 50px;
|
| 12 |
+
}
|
| 13 |
+
.container {
|
| 14 |
+
max-width: 500px;
|
| 15 |
+
background: white;
|
| 16 |
+
padding: 30px;
|
| 17 |
+
border-radius: 10px;
|
| 18 |
+
box-shadow: 0 0 15px rgba(0,0,0,0.1);
|
| 19 |
+
}
|
| 20 |
+
h2 {
|
| 21 |
+
margin-bottom: 25px;
|
| 22 |
+
color: #343a40;
|
| 23 |
+
}
|
| 24 |
+
.btn-primary {
|
| 25 |
+
width: 100%;
|
| 26 |
+
}
|
| 27 |
+
.prediction {
|
| 28 |
+
margin-top: 30px;
|
| 29 |
+
text-align: center;
|
| 30 |
+
}
|
| 31 |
+
.prediction img {
|
| 32 |
+
margin-top: 15px;
|
| 33 |
+
max-width: 100%;
|
| 34 |
+
border-radius: 10px;
|
| 35 |
+
}
|
| 36 |
+
.footer {
|
| 37 |
+
margin-top: 40px;
|
| 38 |
+
text-align: center;
|
| 39 |
+
color: #999;
|
| 40 |
+
font-size: 0.9em;
|
| 41 |
+
}
|
| 42 |
+
</style>
|
| 43 |
+
</head>
|
| 44 |
+
<body>
|
| 45 |
+
<div class="container text-center">
|
| 46 |
+
<h2>Brain Stroke CT Scan Classifier</h2>
|
| 47 |
+
<form action="/" method="POST" enctype="multipart/form-data">
|
| 48 |
+
<div class="form-group">
|
| 49 |
+
<input type="file" name="file" class="form-control-file" required>
|
| 50 |
+
</div>
|
| 51 |
+
<button type="submit" class="btn btn-primary">Predict</button>
|
| 52 |
+
</form>
|
| 53 |
+
|
| 54 |
+
{% if prediction %}
|
| 55 |
+
<div class="prediction">
|
| 56 |
+
<h4 class="mt-4">Prediction Result:</h4>
|
| 57 |
+
<div class="alert alert-info">{{ prediction }}</div>
|
| 58 |
+
<img src="{{ image }}" alt="Uploaded Image">
|
| 59 |
+
</div>
|
| 60 |
+
{% endif %}
|
| 61 |
+
|
| 62 |
+
<div class="footer">
|
| 63 |
+
<p>© 2025 Brain Stroke AI – Demo App</p>
|
| 64 |
+
</div>
|
| 65 |
+
</div>
|
| 66 |
+
</body>
|
| 67 |
+
</html>
|
requirements (1).txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
tensorflow
|
| 3 |
+
pillow
|