-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (90 loc) · 3.63 KB
/
main.py
File metadata and controls
103 lines (90 loc) · 3.63 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from flask import Flask, render_template, request, redirect, send_from_directory, session
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__, template_folder='templates')
app.secret_key = 'your-secret-key' # for session use
# Set valid credentials
VALID_USERNAME = "admin"
VALID_PASSWORD = "rajdeep"
# Load the trained model
model = load_model('Models/model.h5')
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
img_array = np.expand_dims(img_array, axis=0)
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 "Squares Cell Carcenioma", confidence_score
else:
return f"Tumor: {class_labels[predicted_class_index]}", confidence_score
@app.route('/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if username == VALID_USERNAME and password == VALID_PASSWORD:
return redirect('/dashboard')
else:
error = 'Invalid credentials. Please try again.'
return render_template('login.html', error=error)
return render_template('login.html')
@app.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
if request.method == 'POST':
file = request.files['file']
if file:
file_location = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(file_location)
result, confidence = predict_tumor(file_location)
session['disease'] = result
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)
@app.route('/report')
def report():
disease = session.get('disease', 'Unknown')
treatment_map = {
"Large cell Carcinoma": [
"Chemotherapy sessions as prescribed",
"Liver function tests every 3 months",
"Low-fat, high-protein diet",
"Regular hydration and enzyme monitoring"
],
"Tumor: pituitary": [
"Hormonal replacement therapy",
"Surgical removal if enlarged",
"MRI checkups every 6 months"
],
"Tumor: glioma": [
"Radiotherapy and chemotherapy",
"Avoid alcohol/smoking",
"Monitor neurological health"
],
"Tumor: meningioma": [
"CT/MRI scans periodically",
"Surgical resection if symptomatic",
"Anti-inflammatory support"
]
}
tips = treatment_map.get(disease, ["Consult a liver specialist for personalized treatment."])
return render_template('report.html', disease=disease, tips=tips)
@app.route('/heat')
def heatmap():
return render_template('heat.html', confidence="87.24")
@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)