Update app.py
Browse files
app.py
CHANGED
|
@@ -11,98 +11,123 @@ from numpy.linalg import norm
|
|
| 11 |
from PIL import Image
|
| 12 |
import zipfile
|
| 13 |
|
| 14 |
-
# ---
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
st.set_page_config(page_title="Moda Öneri Sistemi", layout="centered")
|
| 27 |
-
st.title(
|
| 28 |
|
| 29 |
-
# ---
|
|
|
|
|
|
|
| 30 |
@st.cache_resource
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
base_model = ResNet50(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
base_model.trainable = False
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def extract_features(img_path, model):
|
| 52 |
-
|
|
|
|
| 53 |
img_array = image.img_to_array(img)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
f.write(uploaded_file.getbuffer())
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
neighbors.fit(feature_list)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
st.subheader(
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
cols = st.columns(5)
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
if os.path.exists('images'):
|
| 89 |
-
for root, dirs, files in os.walk('images'):
|
| 90 |
-
for f in files:
|
| 91 |
-
image_database[f.lower()] = os.path.join(root, f)
|
| 92 |
-
|
| 93 |
-
for i in range(1, 6):
|
| 94 |
with cols[i-1]:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
else:
|
| 103 |
-
st.error(f"Eksik:\n{clean_name}")
|
| 104 |
|
| 105 |
-
|
| 106 |
-
if os.path.exists('images'):
|
| 107 |
-
count = sum([len(files) for r, d, files in os.walk('images')])
|
| 108 |
-
st.sidebar.write(f"Sistemdeki Toplam Resim: {count}")
|
|
|
|
| 11 |
from PIL import Image
|
| 12 |
import zipfile
|
| 13 |
|
| 14 |
+
# -----------------------------
|
| 15 |
+
# 1 ZIP VARSA AÇ
|
| 16 |
+
# -----------------------------
|
| 17 |
+
if not os.path.exists("images"):
|
| 18 |
+
if os.path.exists("images.zip"):
|
| 19 |
+
with zipfile.ZipFile("images.zip", "r") as zip_ref:
|
| 20 |
+
zip_ref.extractall(".")
|
| 21 |
+
st.write("images klasörü zipten çıkarıldı")
|
| 22 |
+
|
| 23 |
+
# -----------------------------
|
| 24 |
+
# 2 SAYFA AYARI
|
| 25 |
+
# -----------------------------
|
| 26 |
st.set_page_config(page_title="Moda Öneri Sistemi", layout="centered")
|
| 27 |
+
st.title("🛍️ Moda Öneri Sistemi")
|
| 28 |
|
| 29 |
+
# -----------------------------
|
| 30 |
+
# 3 MODEL YÜKLE
|
| 31 |
+
# -----------------------------
|
| 32 |
@st.cache_resource
|
| 33 |
+
def load_model():
|
| 34 |
+
|
| 35 |
+
base_model = ResNet50(
|
| 36 |
+
weights='imagenet',
|
| 37 |
+
include_top=False,
|
| 38 |
+
input_shape=(224,224,3)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
base_model.trainable = False
|
| 42 |
+
|
| 43 |
+
model = tf.keras.models.Sequential([
|
| 44 |
+
base_model,
|
| 45 |
+
GlobalMaxPool2D()
|
| 46 |
+
])
|
| 47 |
+
|
| 48 |
+
return model
|
| 49 |
+
|
| 50 |
+
model = load_model()
|
| 51 |
+
|
| 52 |
+
# -----------------------------
|
| 53 |
+
# 4 FEATURE VE DOSYA LİSTESİ
|
| 54 |
+
# -----------------------------
|
| 55 |
+
@st.cache_resource
|
| 56 |
+
def load_data():
|
| 57 |
+
|
| 58 |
+
features = np.array(pkl.load(open("Images_features.pkl","rb")))
|
| 59 |
+
filenames = pkl.load(open("filenames.pkl","rb"))
|
| 60 |
+
|
| 61 |
+
return features, filenames
|
| 62 |
+
|
| 63 |
+
feature_list, filenames = load_data()
|
| 64 |
+
|
| 65 |
+
# -----------------------------
|
| 66 |
+
# 5 FEATURE ÇIKAR
|
| 67 |
+
# -----------------------------
|
| 68 |
def extract_features(img_path, model):
|
| 69 |
+
|
| 70 |
+
img = image.load_img(img_path, target_size=(224,224))
|
| 71 |
img_array = image.img_to_array(img)
|
| 72 |
+
|
| 73 |
+
expanded = np.expand_dims(img_array, axis=0)
|
| 74 |
+
|
| 75 |
+
preprocessed = preprocess_input(expanded)
|
| 76 |
+
|
| 77 |
+
result = model.predict(preprocessed).flatten()
|
| 78 |
+
|
| 79 |
+
normalized = result / norm(result)
|
| 80 |
+
|
| 81 |
+
return normalized
|
| 82 |
+
|
| 83 |
+
# -----------------------------
|
| 84 |
+
# 6 RESİM YÜKLEME
|
| 85 |
+
# -----------------------------
|
| 86 |
+
uploaded_file = st.file_uploader("Bir kıyafet resmi yükleyin", type=["jpg","png","jpeg"])
|
| 87 |
+
|
| 88 |
+
if uploaded_file is not None:
|
| 89 |
+
|
| 90 |
+
img = Image.open(uploaded_file)
|
| 91 |
+
st.image(img, width=300)
|
| 92 |
+
|
| 93 |
+
with open("temp.jpg","wb") as f:
|
| 94 |
f.write(uploaded_file.getbuffer())
|
| 95 |
+
|
| 96 |
+
with st.spinner("Benzer ürünler aranıyor..."):
|
| 97 |
+
|
| 98 |
+
input_feature = extract_features("temp.jpg", model)
|
| 99 |
+
|
| 100 |
+
neighbors = NearestNeighbors(
|
| 101 |
+
n_neighbors=6,
|
| 102 |
+
algorithm="brute",
|
| 103 |
+
metric="euclidean"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
neighbors.fit(feature_list)
|
| 107 |
+
|
| 108 |
+
distances, indices = neighbors.kneighbors([input_feature])
|
| 109 |
+
|
| 110 |
+
st.subheader("✨ Benzer Ürünler")
|
| 111 |
+
|
| 112 |
+
# -----------------------------
|
| 113 |
+
# 7 RESİMLERİ GÖSTER
|
| 114 |
+
# -----------------------------
|
| 115 |
cols = st.columns(5)
|
| 116 |
+
|
| 117 |
+
for i in range(1,6):
|
| 118 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
with cols[i-1]:
|
| 120 |
+
|
| 121 |
+
file_path = filenames[indices[0][i]]
|
| 122 |
+
|
| 123 |
+
file_name = os.path.basename(file_path)
|
| 124 |
+
|
| 125 |
+
image_path = os.path.join("images", file_name)
|
| 126 |
+
|
| 127 |
+
if os.path.exists(image_path):
|
| 128 |
+
|
| 129 |
+
st.image(image_path, use_container_width=True)
|
| 130 |
+
|
| 131 |
else:
|
|
|
|
| 132 |
|
| 133 |
+
st.write("Eksik:", file_name)
|
|
|
|
|
|
|
|
|