-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (50 loc) · 2.12 KB
/
main.py
File metadata and controls
64 lines (50 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask, render_template, request, send_from_directory
from tensorflow.keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
import numpy as np
import os
# Initialize Flask app
app = Flask(__name__)
# Load the trained model
model = load_model('models/model.h5')
# Class labels
class_labels = ['pituitary', 'glioma', 'notumor', 'meningioma']
# Define the uploads folder
UPLOAD_FOLDER = './uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Helper function to predict tumor type
def predict_tumor(image_path):
IMAGE_SIZE = 128
img = load_img(image_path, target_size=(IMAGE_SIZE, IMAGE_SIZE))
img_array = img_to_array(img) / 255.0 # Normalize pixel values
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
predictions = model.predict(img_array)
predicted_class_index = np.argmax(predictions, axis=1)[0]
confidence_score = np.max(predictions, axis=1)[0]
if class_labels[predicted_class_index] == 'notumor':
return "No Tumor", confidence_score
else:
return f"Tumor: {class_labels[predicted_class_index]}", confidence_score
# Route for the main page (index.html)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# Handle file upload
file = request.files['file']
if file:
# Save the file
file_location = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(file_location)
# Predict the tumor
result, confidence = predict_tumor(file_location)
# Return result along with image path for display
return render_template('index.html', result=result, confidence=f"{confidence*100:.2f}", file_path=f'/uploads/{file.filename}')
return render_template('index.html', result=None)
# Route to serve uploaded files
@app.route('/uploads/<filename>')
def get_uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.run(debug=True, port=8000)