-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
187 lines (158 loc) · 5.85 KB
/
app.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import pyrebase
from flask import Flask, render_template, request, jsonify, flash, redirect, url_for, session
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import base64
from PIL import Image
import os
import datetime
from dotenv import load_dotenv
load_dotenv()
import collections
if sys.version_info.major == 3 and sys.version_info.minor >= 10:
from collections.abc import MutableMapping
else:
from collections import MutableMapping
app = Flask(__name__)
app.secret_key = 'd@123'
# Load the model
gmodal = load_model('./models/vgg16-epc15.h5')
gindex=['Brain', 'Eyes', 'Lungs']
lmodal = load_model('./models/Lungs/VGG-res - epc15.h5')
lindex = index=['Bacterial Pneumonia', 'Corona Virus Disease', 'Normal', 'Tuberculosis', 'Viral Pneumonia']
emodal = load_model('./models/Eyes/VGG-res.h5')
eindex = index=[ 'cataract', 'diabetic _ retinopathy' , ' glaucoma ' , ' normal' ]
bmodal = load_model('./models/Brain/res-vgg-epc15.h5')
bindex = ['brain_glioma', 'brain_menin', 'brain_tumor', 'normal']
# Configure Firebase
firebase_config = {
"apiKey": os.environ.get("FIREBASE_API_KEY"),
"authDomain": os.environ.get("FIREBASE_AUTH_DOMAIN"),
"databaseURL": os.environ.get("FIREBASE_DATABASE_URL"),
"storageBucket": os.environ.get("FIREBASE_STORAGE_BUCKET"),
}
firebase = pyrebase.initialize_app(firebase_config)
auth = firebase.auth()
db = firebase.database()
storage = firebase.storage()
@app.route('/')
def index():
return render_template('login.html')
@app.route('/signup', methods=['POST'])
def signup():
# Handle sign-up logic here
name = request.form.get('name')
email = request.form.get('email')
password = request.form.get('password')
try:
# Create a new user in Firebase Authentication
user = auth.create_user_with_email_and_password(email, password)
# Store the user's information in the Firebase Realtime Database
user_data = {
'name': name,
'email': email
}
db.child('users').child(user['localId']).set(user_data)
flash('User created successfully', 'success')
return redirect('/')
except Exception as e:
# Handle exceptions
flash(str(e), 'error')
return redirect('/')
@app.route('/signin', methods=['POST'])
def signin():
# Handle sign-in logic here
email = request.form.get('email')
password = request.form.get('password')
try:
# Sign in the user using Firebase Authentication
user = auth.sign_in_with_email_and_password(email, password)
# user is authenticated, store user's email in the session
session['email'] = email
# User is authenticated, redirect to the medical form
return render_template('form.html')
except Exception as e:
# Handle exceptions
return jsonify({'error': str(e)})
@app.route('/logout', methods=['POST'])
def logout():
# Remove the email from the session
session.pop('email', None)
# Redirect to the home page
return render_template("login.html")
@app.route('/details', methods=['POST'])
def details():
# Get form data
firstname = request.form.get('firstname')
lastname = request.form.get('lastname')
age = request.form.get('age')
dob = request.form.get('dob')
gender = request.form.get('occupation')
email = request.form.get('email')
address = request.form.get('address')
address2 = request.form.get('address2')
phone = request.form.get('phone')
post = request.form.get('post')
city = request.form.get('city')
upload = request.files['upload']
uploaded = upload
upload = upload.read()
encoded = base64.b64encode(upload).decode('utf-8')
img = Image.open(uploaded)
img = img.convert()
img = img.resize((250,250))
mime_type = 'image/jpeg' # Replace with the actual MIME type of your image
data_url = f'data:{mime_type};base64,{encoded}'
# # Check if the image mode is RGBA
# if img.mode == 'RGBA':
# Convert the image to RGB
img = img.convert('RGB')
img.save("temp.jpg")
# Upload the temporary file to Firebase Storage
with open("temp.jpg", "rb") as image_file:
storage.child("images/"+firstname+lastname+".jpg").put(image_file)
# Delete the temporary file
os.remove("temp.jpg")
# Process the data as needed
x = img
x = image.img_to_array(x)
x = np.expand_dims(x,axis = 0)#changing the shape
g_preds=gmodal.predict(x)
g_pred=np.argmax(g_preds, axis=1)
g_result = str(gindex[g_pred[0]])
if g_result=="Brain":
b_preds=bmodal.predict(x)
b_pred=np.argmax(b_preds, axis=1)
res = str(bindex[b_pred[0]])
elif g_result=="Eyes":
e_preds=emodal.predict(x)
e_pred=np.argmax(e_preds, axis=1)
res = str(eindex[e_pred[0]])
else:
l_preds=lmodal.predict(x)
l_pred=np.argmax(l_preds, axis=1)
res = str(lindex[l_pred[0]])
# Get the current timestamp as a datetime object
current_time = datetime.datetime.now()
# You can format the timestamp as a string if needed
current_time_str = current_time.strftime('%Y-%m-%d %H:%M:%S')
dat = {
"firstname": firstname,
"lastname": lastname,
"age": age,
"dob": dob,
"gender": gender,
"email": email,
"address": address,
"address2": address2,
"phone": phone,
"post": post,
"city": city,
"img":"images/"+res+"_"+firstname+lastname+current_time_str+".jpg"
}
db.child("details").push(dat)
# Render the results page
return render_template('result.html', firstname=firstname, lastname=lastname, age=age, dob=dob, gender=gender, email=email, address=address, address2=address2, phone=phone, post=post, city=city, upload=data_url, res=res)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)