-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (51 loc) · 1.96 KB
/
app.py
File metadata and controls
62 lines (51 loc) · 1.96 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
import os
import numpy as np
import cv2
import pickle
from flask import Flask, render_template, request
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D
from sklearn.metrics.pairwise import cosine_similarity
from werkzeug.utils import secure_filename
app = Flask(__name__)
UPLOAD_FOLDER = 'static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# 📌 Model
base_model = MobileNetV2(include_top=False, input_shape=(224, 224, 3), weights='imagenet')
x = GlobalAveragePooling2D()(base_model.output)
embed_model = Model(inputs=base_model.input, outputs=x)
# 🔁 Embedding verileri
with open("embeddings.pkl", "rb") as f:
embeddings, labels = pickle.load(f)
# 🔍 Tahmin fonksiyonu
def predict_class(image_path):
img = cv2.imread(image_path)
if img is None:
return "Görsel okunamadı", 0.0
img = cv2.resize(img, (224, 224))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = preprocess_input(img)
img = np.expand_dims(img, axis=0)
test_emb = embed_model.predict(img, verbose=0)
sims = cosine_similarity(test_emb, embeddings)[0]
best_idx = np.argmax(sims)
best_label = labels[best_idx]
confidence = sims[best_idx]
return best_label, confidence
# 🌐 Ana route
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
file = request.files['file']
if file:
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
prediction, prob = predict_class(filepath)
return render_template('index.html', filename=filename, prediction=prediction, prob=prob)
return render_template('index.html')
# 🔥 Uygulama başlat
if __name__ == '__main__':
app.run(debug=True, use_reloader=False)