File size: 2,570 Bytes
eb9f6f0 6a0530c eb9f6f0 b223f7b eb9f6f0 778540a 87bcf2a f1c099b 2d811c7 6b873a1 eb9f6f0 b41053b |
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 |
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
# Load necessary files and model
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')
# Feature extraction function
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
# Custom CSS for background and title
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,
)
# Streamlit UI
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:
# Display uploaded image with reduced size
img = Image.open(file)
st.image(img, caption='Uploaded Image', width=200)
# Extract features from the uploaded image
input_image = extract_features_from_images(file, model)
# Get recommendations
distance, indices = neighbors.kneighbors([input_image])
st.subheader("Recommended Products:")
# Display recommendations in a grid layout
cols = st.columns(3) # 3 images per row
for i, idx in enumerate(indices[0]):
product_path = filenames[idx]
# Replace any instances of double backslashes with a single one
product_path = product_path.replace('\\', '/')
st.write(product_path)
product_image = Image.open(product_path)
# Assign columns dynamically
with cols[i % 3]:
st.image(product_image, caption=f"Product {i + 1}")
|