-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
217 lines (161 loc) · 6.25 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from skimage.io import imread
import joblib
from flask import Flask, render_template, request, redirect, url_for, jsonify
import tensorflow as tf
import os
import numpy as np
from tensorflow import keras
from PIL import Image
import tensorflow_hub as hub
import smtplib
from keras.models import load_model
from keras.utils import custom_object_scope
from ultralytics import YOLO
os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
# Flask uygulamasını oluştur
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
class FixedDropout(tf.keras.layers.Dropout):
def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape
return tuple([shape if shape is not None else tf.shape(inputs)[i] for i, shape in enumerate(self.noise_shape)])
# EffnetB7 modelini özel nesne kapsamı ile yükle
with custom_object_scope({'FixedDropout': FixedDropout, 'KerasLayer': hub.KerasLayer}):
effnet_model = load_model('models/efficientnet_b7_model.h5')
# YOLOv8 modelini yükle
yolo_model = YOLO('models/best_model.pt')
# SVM modelini yükle
svm = joblib.load('models/svm_model.joblib')
@app.route('/')
def home():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/services')
def services():
return render_template('services.html')
@app.route('/service1')
def service1():
return render_template('service1.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/blogs')
def blogs():
return render_template('blogs.html')
@app.route('/blog1')
def blog1():
return render_template('blog1.html')
@app.route('/blog2')
def blog2():
return render_template('blog2.html')
@app.route('/blog3')
def blog3():
return render_template('blog3.html')
@app.route('/alzheimerdetection')
def alzheimerdetection():
return render_template('alzheimerdetection.html')
@app.route('/braintumordetectwithyolo')
def braintumordetectwithyolo():
return render_template('braintumordetectwithyolo.html')
# Beyin tümörü tespiti için EffecientNetB7 modelini kullanır
@app.route('/detect', methods=['POST'])
def detect():
file = request.files['image']
img = Image.open(file)
img = img.convert("RGB")
img = img.resize((256, 256))
img_array = np.array(img)
img_array = img_array.reshape((1, 256, 256, 3))
img_array = img_array / 255.0
pred = effnet_model.predict(img_array)
pred = np.round(pred)
if pred[0][0] == 0:
result_text = "Brain tumor not detected."
else:
result_text = "Brain tumor detected"
return render_template('index.html', prediction=pred.tolist(), result=result_text)
# Alzheimer tespiti için SVM modelini kullanır
@app.route('/predict', methods=['POST'])
def predict():
# Görüntüyü yükle ve ön işlem yap
file = request.files['image']
img = Image.open(file)
img_size = (64, 64)
# Görüntüyü img_size boyutuna yeniden boyutlandır
img = img.resize(img_size)
# Görüntüyü gri tonlamaya çevir
img_gray = img.convert("L")
# Görüntüyü numpy dizisine çevir ve düzleştir
img_gray_arr = np.array(img_gray)
img_gray_arr_flat = img_gray_arr.reshape(1, -1)
# Piksel değerlerini 0 ile 1 arasında normalize et
img_gray_arr_flat_norm = img_gray_arr_flat / 255.0
img_processed = img_gray_arr_flat_norm
# SVM modeli ile tahmin yap
prediction = svm.predict(img_processed)[0]
# Tahmin için sınıf adını al
class_names = ["Very_Mild_Demented", "Mild_Demented",
"Moderate_Demented", "Non_Demented"]
class_name = class_names[prediction]
result = "Level Of : " + class_name
return render_template('alzheimerdetection.html', image=img, prediction=prediction.tolist(), result=result)
# YOLO modeli kullanarak nesne tespiti yapar
@app.route('/yolodetect', methods=['POST'])
def yolodetect():
file = request.files['image']
img = Image.open(file)
img_path = os.path.join('uploads', file.filename)
img.save(img_path)
# Görüntüyü yükle ve modelle tespit yap
results = yolo_model(img_path)
# Benzersiz bir dosya adı oluştur
result_img_name = f'result_{file.filename}'
# Tespit sonuçlarını işleyin ve görselleştirin
for i, result in enumerate(results):
result_img = result.plot() # Görselleştirilmiş görüntüyü al
result_img = Image.fromarray(result_img.astype(np.uint8)) # numpy array'den PIL Image'e dönüştür
result_img_path = os.path.join('static/predictions', result_img_name)
result_img.save(result_img_path) # Görselleştirilmiş görüntüyü kaydet
return render_template('braintumordetectwithyolo.html', filename=result_img_name)
# E-posta gönderme işlevi
@app.route('/send_mail', methods=['GET', 'POST'])
def send_mail():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
message = request.form['message']
# email body
body = f"Name: {name}\nEmail: {email}\n\nMessage:\n{message}"
# email gönder
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('[email protected]', 'your_password')
server.sendmail('[email protected]',
'[email protected]', body)
server.quit()
return render_template('contact.html', success=True)
except:
return render_template('contact.html', success=False)
return render_template('contact.html', success=None)
# Sunucuyu kapatma işlevi
@app.route('/stop', methods=['POST'])
def shutdown():
# İsteğin güvenilir bir kaynaktan geldiğini kontrol et
if request.remote_addr != '127.0.0.1':
return "Unauthorized", 403
# Sunucuyu kapat
shutdown_func = request.environ.get('werkzeug.server.shutdown')
if shutdown_func is None:
raise RuntimeError('Not running with the Werkzeug Server')
shutdown_func()
return 'Server shutting down...'
if __name__ == '__main__':
if not os.path.exists('uploads'):
os.makedirs('uploads')
if not os.path.exists('static/predictions'):
os.makedirs('static/predictions')
app.run(debug=True)