Update app.py
Browse files
app.py
CHANGED
|
@@ -10,32 +10,24 @@ from sklearn.neighbors import NearestNeighbors
|
|
| 10 |
from numpy.linalg import norm
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
-
# Sayfa
|
| 14 |
st.set_page_config(page_title="Moda Öneri Sistemi", layout="centered")
|
| 15 |
-
|
| 16 |
-
st.markdown("""
|
| 17 |
-
<style>
|
| 18 |
-
.stTitle, .stSubheader, p { text-align: center; }
|
| 19 |
-
.stImage { display: flex; justify-content: center; }
|
| 20 |
-
</style>
|
| 21 |
-
""", unsafe_allow_html=True)
|
| 22 |
-
|
| 23 |
st.title('🛍️ Moda Öneri Sistemi')
|
| 24 |
|
| 25 |
-
# Model ve
|
| 26 |
@st.cache_resource
|
| 27 |
def load_data():
|
| 28 |
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
| 29 |
base_model.trainable = False
|
| 30 |
model = tf.keras.models.Sequential([base_model, GlobalMaxPool2D()])
|
| 31 |
|
| 32 |
-
# Pickle dosyalarını
|
| 33 |
try:
|
| 34 |
features = np.array(pkl.load(open('Images_features.pkl', 'rb')))
|
| 35 |
filenames = pkl.load(open('filenames.pkl', 'rb'))
|
| 36 |
return model, features, filenames
|
| 37 |
except Exception as e:
|
| 38 |
-
st.error(f"Hata: .pkl dosyaları
|
| 39 |
return None, None, None
|
| 40 |
|
| 41 |
model, feature_list, filenames = load_data()
|
|
@@ -49,19 +41,20 @@ def extract_features(img_path, model):
|
|
| 49 |
norm_result = result / norm(result)
|
| 50 |
return norm_result
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
if uploaded_file is not None and model is not None:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
st.image(display_image, use_container_width=True, caption='Yüklenen Resim')
|
| 59 |
|
| 60 |
temp_path = "temp_upload.jpg"
|
| 61 |
with open(temp_path, "wb") as f:
|
| 62 |
f.write(uploaded_file.getbuffer())
|
| 63 |
|
| 64 |
-
|
|
|
|
| 65 |
input_features = extract_features(temp_path, model)
|
| 66 |
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
|
| 67 |
neighbors.fit(feature_list)
|
|
@@ -72,25 +65,25 @@ if uploaded_file is not None and model is not None:
|
|
| 72 |
|
| 73 |
cols = st.columns(5)
|
| 74 |
|
| 75 |
-
# RESİM
|
|
|
|
| 76 |
image_folder = 'images'
|
| 77 |
-
# Klasördeki tüm resimleri küçük harfe duyarlı olmadan listele
|
| 78 |
if os.path.exists(image_folder):
|
|
|
|
| 79 |
actual_files = {f.lower(): f for f in os.listdir(image_folder)}
|
| 80 |
else:
|
| 81 |
actual_files = {}
|
| 82 |
-
st.error("
|
| 83 |
|
| 84 |
for i in range(1, 6):
|
| 85 |
with cols[i-1]:
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
img_name = os.path.basename(
|
| 89 |
|
| 90 |
-
# Klasörde bu
|
| 91 |
if img_name in actual_files:
|
| 92 |
-
|
| 93 |
-
st.image(
|
| 94 |
else:
|
| 95 |
-
st.warning(f"Eksik:\n{img_name}")
|
| 96 |
-
|
|
|
|
| 10 |
from numpy.linalg import norm
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
+
# 1. Sayfa Ayarları
|
| 14 |
st.set_page_config(page_title="Moda Öneri Sistemi", layout="centered")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
st.title('🛍️ Moda Öneri Sistemi')
|
| 16 |
|
| 17 |
+
# 2. Model ve Verileri Yükleme
|
| 18 |
@st.cache_resource
|
| 19 |
def load_data():
|
| 20 |
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
| 21 |
base_model.trainable = False
|
| 22 |
model = tf.keras.models.Sequential([base_model, GlobalMaxPool2D()])
|
| 23 |
|
| 24 |
+
# Pickle dosyalarını ana dizinde arıyoruz
|
| 25 |
try:
|
| 26 |
features = np.array(pkl.load(open('Images_features.pkl', 'rb')))
|
| 27 |
filenames = pkl.load(open('filenames.pkl', 'rb'))
|
| 28 |
return model, features, filenames
|
| 29 |
except Exception as e:
|
| 30 |
+
st.error(f"Kritik Hata: .pkl dosyaları bulunamadı! {e}")
|
| 31 |
return None, None, None
|
| 32 |
|
| 33 |
model, feature_list, filenames = load_data()
|
|
|
|
| 41 |
norm_result = result / norm(result)
|
| 42 |
return norm_result
|
| 43 |
|
| 44 |
+
# 3. Resim Yükleme
|
| 45 |
+
uploaded_file = st.file_uploader("Bir kıyafet resmi yükleyin...", type=['jpg', 'png', 'jpeg'])
|
| 46 |
|
| 47 |
if uploaded_file is not None and model is not None:
|
| 48 |
+
# Seçilen resmi göster
|
| 49 |
+
display_image = Image.open(uploaded_file)
|
| 50 |
+
st.image(display_image, width=300, caption='Yüklediğiniz Resim')
|
|
|
|
| 51 |
|
| 52 |
temp_path = "temp_upload.jpg"
|
| 53 |
with open(temp_path, "wb") as f:
|
| 54 |
f.write(uploaded_file.getbuffer())
|
| 55 |
|
| 56 |
+
# Benzerleri Bul
|
| 57 |
+
with st.spinner('Öneriler hazırlanıyor...'):
|
| 58 |
input_features = extract_features(temp_path, model)
|
| 59 |
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
|
| 60 |
neighbors.fit(feature_list)
|
|
|
|
| 65 |
|
| 66 |
cols = st.columns(5)
|
| 67 |
|
| 68 |
+
# 4. RESİM GÖSTERME MANTIĞI (En Kritik Kısım)
|
| 69 |
+
# 'images' klasöründeki tüm dosyaları tarayıp küçük harfe çeviriyoruz
|
| 70 |
image_folder = 'images'
|
|
|
|
| 71 |
if os.path.exists(image_folder):
|
| 72 |
+
# Klasördeki gerçek dosya isimlerini bir sözlüğe alıyoruz
|
| 73 |
actual_files = {f.lower(): f for f in os.listdir(image_folder)}
|
| 74 |
else:
|
| 75 |
actual_files = {}
|
| 76 |
+
st.error("'images' klasörü bulunamadı!")
|
| 77 |
|
| 78 |
for i in range(1, 6):
|
| 79 |
with cols[i-1]:
|
| 80 |
+
# Pickle'dan gelen yolu temizle (Windows yolunu Linux'a çevir)
|
| 81 |
+
raw_path = filenames[indices[0][i]].replace('\\', '/')
|
| 82 |
+
img_name = os.path.basename(raw_path).lower() # Sadece '10448.jpg' kısmını alır
|
| 83 |
|
| 84 |
+
# Klasörde bu dosya var mı bak
|
| 85 |
if img_name in actual_files:
|
| 86 |
+
final_img_path = os.path.join(image_folder, actual_files[img_name])
|
| 87 |
+
st.image(final_img_path, use_container_width=True)
|
| 88 |
else:
|
| 89 |
+
st.warning(f"Eksik:\n{img_name}")
|
|
|