Spaces:
Sleeping
Sleeping
import numpy as np | |
import pickle as pkl | |
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 | |
import os | |
from numpy.linalg import norm | |
import streamlit as st | |
st.header('🧚♀️ Moda Tavsiye Sistemi 🧚♀️') | |
Image_features = pkl.load(open('Images_features.pkl','rb')) | |
filenames = pkl.load(open('filenames.pkl','rb')) | |
def extract_features_from_images(image_path, model): | |
img = image.load_img(image_path, target_size=(224,224)) | |
img_array = image.img_to_array(img) | |
img_expand_dim = np.expand_dims(img_array, axis=0) | |
img_preprocess = preprocess_input(img_expand_dim) | |
result = model.predict(img_preprocess).flatten() | |
norm_result = result/norm(result) | |
return norm_result | |
model = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3)) | |
model.trainable = False | |
model = tf.keras.models.Sequential([ | |
model, | |
GlobalMaxPool2D() | |
]) | |
model.build((None, 224, 224, 3)) | |
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean') | |
neighbors.fit(Image_features) | |
# 'upload' klasörünün varlığını kontrol et ve yoksa oluştur | |
upload_dir = 'upload' | |
images_dir = 'images' | |
for directory in [upload_dir, images_dir]: | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
uploaded_file = st.file_uploader("Resim yükle", type=["jpg","png"]) | |
if uploaded_file is not None: | |
with open(os.path.join(upload_dir, uploaded_file.name), 'wb') as f: | |
f.write(uploaded_file.getbuffer()) | |
st.subheader('Yüklenen Resim') | |
st.image(uploaded_file) | |
input_img_features = extract_features_from_images(uploaded_file, model) | |
distance,indices = neighbors.kneighbors([input_img_features]) | |
st.subheader('Tavsiye Edilen Resimler') | |
col1,col2,col3,col4,col5 = st.columns(5) | |
for i, col in enumerate([col1, col2, col3, col4, col5], start=1): | |
with col: | |
image_path = filenames[indices[0][i]] | |
if os.path.exists(image_path): | |
st.image(image_path) | |
else: | |
st.write(f"Resim bulunamadı: {image_path}") | |