File size: 3,779 Bytes
ca453f0 00b564a ca453f0 389578e ca453f0 389578e 00b564a 389578e 57c187f 389578e 57c56aa fb79c27 57c56aa 389578e 57c187f 389578e ca453f0 fb79c27 00b564a ca453f0 00b564a fb79c27 389578e fb79c27 389578e fb79c27 389578e 57c187f 389578e 57c56aa fb79c27 57c56aa 57c187f fb79c27 389578e 00b564a 389578e fb79c27 389578e fb79c27 57c56aa fb79c27 389578e fb79c27 00b564a 57c56aa fb79c27 57c56aa fb79c27 00b564a fb79c27 00b564a 57c56aa 00b564a fb79c27 00b564a 57c56aa 00b564a a944776 00b564a | 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 | import streamlit as st
import os
import zipfile
import numpy as np
import pickle
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.layers import GlobalMaxPool2D
from sklearn.neighbors import NearestNeighbors
from numpy.linalg import norm
from PIL import Image
# --------------------------------
# 1. ZIP DOSYASINI AÇMA (En Önemli Kısım)
# --------------------------------
# Eğer images klasörü boşsa veya yoksa zip'i açar
if not os.path.exists("images") or len(os.listdir("images")) < 10:
if os.path.exists("images.zip"):
with st.spinner("Resimler paketten çıkarılıyor (296 MB)..."):
with zipfile.ZipFile("images.zip", "r") as zip_ref:
zip_ref.extractall(".")
else:
st.error("Hata: images.zip dosyası bulunamadı!")
# --------------------------------
# SAYFA AYARI
# --------------------------------
st.set_page_config(page_title="Moda Öneri Sistemi", layout="centered")
st.title("🛍️ Moda Öneri Sistemi")
# --------------------------------
# MODEL YÜKLE
# --------------------------------
@st.cache_resource
def load_model():
base_model = ResNet50(weights="imagenet", include_top=False, input_shape=(224,224,3))
base_model.trainable = False
model = tf.keras.models.Sequential([base_model, GlobalMaxPool2D()])
return model
model = load_model()
# --------------------------------
# DATA YÜKLE
# --------------------------------
@st.cache_resource
def load_data():
features = np.array(pickle.load(open("Images_features.pkl","rb")))
filenames = pickle.load(open("filenames.pkl","rb"))
return features, filenames
feature_list, filenames = load_data()
# --------------------------------
# FEATURE ÇIKARMA
# --------------------------------
def extract_features(img_path, model):
img = image.load_img(img_path, target_size=(224,224))
img_array = image.img_to_array(img)
expanded_img = np.expand_dims(img_array, axis=0)
preprocessed = preprocess_input(expanded_img)
result = model.predict(preprocessed).flatten()
normalized = result / norm(result)
return normalized
# --------------------------------
# ARAYÜZ VE RESİM YÜKLEME
# --------------------------------
uploaded_file = st.file_uploader("Bir kıyafet resmi yükleyin", type=["jpg","png","jpeg"])
if uploaded_file is not None:
img = Image.open(uploaded_file)
st.image(img, width=300, caption="Seçtiğiniz ürün")
with open("temp.jpg","wb") as f:
f.write(uploaded_file.getbuffer())
with st.spinner("Benzer ürünler bulunuyor..."):
input_feature = extract_features("temp.jpg", model)
neighbors = NearestNeighbors(n_neighbors=6, algorithm="brute", metric="euclidean")
neighbors.fit(feature_list)
distances, indices = neighbors.kneighbors([input_feature])
st.subheader("✨ Benzer Ürünler")
cols = st.columns(5)
# Akıllı Resim Arama (Hangi klasörde olursa olsun bulur)
image_db = {}
for root, dirs, files in os.walk('images'):
for f in files:
image_db[f.lower()] = os.path.join(root, f)
for i in range(1, 6):
with cols[i-1]:
raw_path = filenames[indices[0][i]].replace("\\", "/")
file_name = os.path.basename(raw_path).lower()
if file_name in image_db:
st.image(image_db[file_name], use_container_width=True)
else:
st.write("Eksik:", file_name)
# Hata Ayıklama Paneli (Yan tarafta)
if os.path.exists("images"):
file_count = sum([len(files) for r, d, files in os.walk("images")])
st.sidebar.write(f"📂 images klasöründeki resim sayısı: {file_count}") |