[32b2c5]: / app.py

Download this file

174 lines (122 with data), 6.2 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
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
import numpy as np # dealing with arrays
import os # dealing with directories
from random import shuffle # mixing up or currently ordered data that might lead our network astray in training.
from tqdm import \
tqdm # a nice pretty percentage bar for tasks. Thanks to viewer Daniel BA1/4hler for this suggestion
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tensorflow as tf
import matplotlib.pyplot as plt
from flask import Flask, render_template, url_for, request
import sqlite3
import cv2
import shutil
app = Flask(__name__)
@app.route('/')
def index():
return render_template('home.html')
@app.route('/userlog', methods=['GET', 'POST'])
def userlog():
if request.method == 'POST':
connection = sqlite3.connect('user_data.db')
cursor = connection.cursor()
name = request.form['name']
password = request.form['password']
query = "SELECT name, password FROM user WHERE name = '"+name+"' AND password= '"+password+"'"
cursor.execute(query)
result = cursor.fetchall()
if len(result) == 0:
return render_template('index.html', msg='Sorry, Incorrect Credentials Provided, Try Again')
else:
return render_template('userlog.html')
return render_template('index.html')
@app.route('/userreg', methods=['GET', 'POST'])
def userreg():
if request.method == 'POST':
connection = sqlite3.connect('user_data.db')
cursor = connection.cursor()
name = request.form['name']
password = request.form['password']
mobile = request.form['phone']
email = request.form['email']
print(name, mobile, email, password)
command = """CREATE TABLE IF NOT EXISTS user(name TEXT, password TEXT, mobile TEXT, email TEXT)"""
cursor.execute(command)
cursor.execute("INSERT INTO user VALUES ('"+name+"', '"+password+"', '"+mobile+"', '"+email+"')")
connection.commit()
return render_template('index.html', msg='Successfully Registered')
return render_template('index.html')
@app.route('/image', methods=['GET', 'POST'])
def image():
if request.method == 'POST':
dirPath = "static/images"
fileList = os.listdir(dirPath)
for fileName in fileList:
os.remove(dirPath + "/" + fileName)
fileName=request.form['filename']
dst = "static/images"
shutil.copy("test\\"+fileName, dst)
verify_dir = 'static/images'
IMG_SIZE = 50
LR = 1e-3
MODEL_NAME = 'HEMMORRHAGE-{}-{}.model'.format(LR, '2conv-basic')
## MODEL_NAME='keras_model.h5'
def process_verify_data():
verifying_data = []
for img in os.listdir(verify_dir):
path = os.path.join(verify_dir, img)
img_num = img.split('.')[0]
img = cv2.imread(path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
verifying_data.append([np.array(img), img_num])
np.save('verify_data.npy', verifying_data)
return verifying_data
verify_data = process_verify_data()
#verify_data = np.load('verify_data.npy')
tf.compat.v1.reset_default_graph()
#tf.reset_default_graph()
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3], name='input')
convnet = conv_2d(convnet, 32, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = conv_2d(convnet, 64, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = conv_2d(convnet, 128, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = conv_2d(convnet, 32, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = conv_2d(convnet, 64, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet, tensorboard_dir='log')
if os.path.exists('{}.meta'.format(MODEL_NAME)):
model.load(MODEL_NAME)
print('model loaded!')
accuracy=" "
str_label=" "
for num, data in enumerate(verify_data):
img_num = data[1]
img_data = data[0]
#y = fig.add_subplot(3, 4, num + 1)
orig = img_data
data = img_data.reshape(IMG_SIZE, IMG_SIZE, 3)
# model_out = model.predict([data])[0]
model_out = model.predict([data])[0]
print(model_out)
print('model {}'.format(np.argmax(model_out)))
if np.argmax(model_out) == 0:
str_label = 'HEMORRHAGE'
print("The predicted image of the brain with hemmorrhage detected with a accuracy of {} %".format(model_out[0]*90))
accuracy = "The predicted image of the brain with hemmorrhage detected with a accuracy of {} %".format(model_out[0]*90)
elif np.argmax(model_out) == 1:
str_label = 'NORMAL'
print("The predicted image of the brain is normal with a accuracy of {} %".format(model_out[1]*100))
accuracy = "The predicted image of the brain is normal with a accuracy of {} %".format(model_out[1]*100)
return render_template('home.html', status=str_label,accuracy=accuracy, ImageDisplay="http://127.0.0.1:5000/static/images/"+fileName)
return render_template('home.html')
if __name__ == "__main__":
app.run(debug=True, use_reloader=False)