Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,69 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
img
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
img =
|
20 |
-
img = img.astype(np.
|
21 |
-
|
22 |
-
|
23 |
-
img =
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
file_path
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
processed_image
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
app.run(debug=True)
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
from flask import Flask, request, jsonify, render_template
|
5 |
+
from tensorflow.keras.models import load_model # type: ignore
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Load the saved model
|
10 |
+
model = load_model('pneumonia_model_final.keras')
|
11 |
+
|
12 |
+
def preprocess_image(image):
|
13 |
+
"""Preprocess the image for prediction."""
|
14 |
+
img = cv2.imread(image, cv2.IMREAD_GRAYSCALE) # Read image as grayscale
|
15 |
+
if img is None:
|
16 |
+
return None
|
17 |
+
|
18 |
+
img = cv2.resize(img, (224, 224)) # Resize to 224x224 pixels
|
19 |
+
img = img.astype(np.float32) / 255.0 # Normalize to [0, 1] range
|
20 |
+
img = (img * 255).astype(np.uint8) # Convert back to 8-bit unsigned integer format
|
21 |
+
|
22 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) # Convert to RGB
|
23 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
24 |
+
return img
|
25 |
+
|
26 |
+
@app.route('/')
|
27 |
+
def home():
|
28 |
+
"""Render the home page for uploading images."""
|
29 |
+
return render_template('index.html', template_folder='.')
|
30 |
+
|
31 |
+
@app.route('/predict', methods=['POST'])
|
32 |
+
def predict():
|
33 |
+
"""Endpoint for making predictions."""
|
34 |
+
if 'file' not in request.files:
|
35 |
+
return jsonify({'error': 'No file provided.'}), 400
|
36 |
+
|
37 |
+
file = request.files['file']
|
38 |
+
|
39 |
+
# Save the uploaded file temporarily
|
40 |
+
file_path = 'temp_image.jpg'
|
41 |
+
file.save(file_path)
|
42 |
+
|
43 |
+
# Preprocess the image
|
44 |
+
processed_image = preprocess_image(file_path)
|
45 |
+
if processed_image is None:
|
46 |
+
return jsonify({'error': 'Invalid image format.'}), 400
|
47 |
+
|
48 |
+
# Make a prediction
|
49 |
+
prediction = model.predict(processed_image)
|
50 |
+
probability = prediction[0][0] # Get the probability score
|
51 |
+
|
52 |
+
# Calculate percentage probability
|
53 |
+
probability_percent = round(probability * 100, 2)
|
54 |
+
|
55 |
+
# Classification threshold
|
56 |
+
threshold = 0.7 # Adjust threshold if necessary
|
57 |
+
is_pneumonia = probability > threshold
|
58 |
+
|
59 |
+
# Determine result message
|
60 |
+
result_text = f"Pneumonia" if is_pneumonia else f"Not Pneumonia"
|
61 |
+
|
62 |
+
# Remove the temporary file
|
63 |
+
os.remove(file_path)
|
64 |
+
|
65 |
+
# Render the result page with prediction details
|
66 |
+
return render_template('result.html', result=result_text, probability=probability_percent, template_folder='.')
|
67 |
+
|
68 |
+
if __name__ == '__main__':
|
69 |
+
app.run(debug=True)
|
|