| import streamlit as st |
| import pickle as pkl |
| from PIL import Image |
| from sklearn.neighbors import NearestNeighbors |
| from tensorflow.keras.models import load_model |
| from tensorflow.keras.applications.resnet50 import preprocess_input |
| from tensorflow.keras.preprocessing import image |
| import os |
| from numpy.linalg import norm |
| import numpy as np |
|
|
| |
| filenames = pkl.load(open('filenames2.pkl', 'rb')) |
| Image_features = pkl.load(open('Images_features2.pkl', 'rb')) |
| neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean') |
| neighbors.fit(Image_features) |
| model = load_model('model.h5') |
|
|
| |
| 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 |
|
|
| |
| st.markdown( |
| """ |
| <style> |
| body { |
| background-color: #f0f8ff; /* Soft light blue background */ |
| } |
| .title { |
| color: #1f7a8c; /* Teal color */ |
| font-family: 'Arial Black', sans-serif; |
| text-align: center; |
| } |
| </style> |
| """, |
| unsafe_allow_html=True, |
| ) |
|
|
| |
| st.markdown('<h1 class="title">Fashion Deep Learning Classification 👗</h1>', unsafe_allow_html=True) |
| st.write('Upload a clothing image, and the model recommends similar products.') |
|
|
| file = st.file_uploader('Upload an Image', type=['jpg', 'jpeg', 'png']) |
|
|
| if file is not None: |
| |
| img = Image.open(file) |
| st.image(img, caption='Uploaded Image', width=200) |
|
|
| |
| input_image = extract_features_from_images(file, model) |
|
|
| |
| distance, indices = neighbors.kneighbors([input_image]) |
| st.subheader("Recommended Products:") |
|
|
| |
| cols = st.columns(3) |
| base_dir = os.path.dirname(__file__) |
| images_dir = os.path.join(base_dir, "images") |
|
|
| for i, idx in enumerate(indices[0]): |
| product_path = filenames[idx] |
| st.write(product_path) |
| product_image = Image.open(product_path) |
| |
| |
| with cols[i % 3]: |
| st.image(product_image, caption=f"Product {i + 1}", use_column_width=True) |
|
|