RaniyaK commited on
Commit
5d55f41
1 Parent(s): cd2ed74

Update app.py

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