Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,10 @@
|
|
1 |
-
import
|
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 |
-
|
8 |
-
app = Flask(__name__, template_folder='.')
|
9 |
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
|
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') # This will look for index.html in the specified template folder
|
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 |
+
from flask import Flask, jsonify
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
app = Flask(__name__)
|
|
|
4 |
|
5 |
+
@app.route('/api/data')
|
6 |
+
def get_data():
|
7 |
+
return jsonify({"message": "Hello from Flask!"})
|
8 |
|
9 |
+
if __name__ == "__main__":
|
10 |
+
app.run(port=5000) # Run on port 5000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|