Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +158 -0
- embedding_large.pkl +3 -0
- embedding_myntra.pkl +3 -0
- filenames_large.pkl +3 -0
- filenames_myntra.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import pickle
|
6 |
+
import tensorflow
|
7 |
+
import pandas as pd
|
8 |
+
from tensorflow.keras.preprocessing import image
|
9 |
+
from tensorflow.keras.layers import GlobalMaxPooling2D
|
10 |
+
from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input
|
11 |
+
from sklearn.neighbors import NearestNeighbors
|
12 |
+
from numpy.linalg import norm
|
13 |
+
|
14 |
+
feature_list = np.array(pickle.load(open('embedding_large.pkl','rb')))
|
15 |
+
# print(feature_list)
|
16 |
+
filenames = pd.read_pickle('filenames_large.pkl')
|
17 |
+
|
18 |
+
# print(filenames)
|
19 |
+
|
20 |
+
feature_list_myntra = np.array(pickle.load(open('embedding_myntra.pkl','rb')))
|
21 |
+
# print(feature_list)
|
22 |
+
filenames_myntra = pd.read_pickle('filenames_myntra.pkl')
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
model = ResNet50(weights='imagenet',include_top=False,input_shape=(224,224,3))
|
31 |
+
model.trainable = False
|
32 |
+
|
33 |
+
model = tensorflow.keras.Sequential([
|
34 |
+
model,
|
35 |
+
GlobalMaxPooling2D()
|
36 |
+
])
|
37 |
+
|
38 |
+
st.title('Fashion Recommender System')
|
39 |
+
|
40 |
+
def save_uploaded_file(uploaded_file):
|
41 |
+
try:
|
42 |
+
with open(os.path.join('uploads',uploaded_file.name),'wb') as f:
|
43 |
+
f.write(uploaded_file.getbuffer())
|
44 |
+
return 1
|
45 |
+
except:
|
46 |
+
return 0
|
47 |
+
|
48 |
+
def feature_extraction(img_path,model):
|
49 |
+
img = image.load_img(img_path, target_size=(224, 224))
|
50 |
+
img_array = image.img_to_array(img)
|
51 |
+
expanded_img_array = np.expand_dims(img_array, axis=0)
|
52 |
+
preprocessed_img = preprocess_input(expanded_img_array)
|
53 |
+
result = model.predict(preprocessed_img).flatten()
|
54 |
+
normalized_result = result / norm(result)
|
55 |
+
|
56 |
+
return normalized_result
|
57 |
+
|
58 |
+
def recommend(features,feature_list):
|
59 |
+
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
|
60 |
+
neighbors.fit(feature_list)
|
61 |
+
|
62 |
+
distances, indices = neighbors.kneighbors([features])
|
63 |
+
print(distances,indices)
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
return indices
|
69 |
+
|
70 |
+
|
71 |
+
def recommend_myntra(features,feature_list):
|
72 |
+
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
|
73 |
+
neighbors.fit(feature_list_myntra)
|
74 |
+
|
75 |
+
distances, indices = neighbors.kneighbors([features])
|
76 |
+
print(distances,indices)
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
return indices
|
82 |
+
|
83 |
+
#
|
84 |
+
|
85 |
+
menu = ['FR','FRM','AB']
|
86 |
+
option = st.sidebar.selectbox("Select your model",menu)
|
87 |
+
|
88 |
+
if option=='FR':
|
89 |
+
uploaded_file = st.file_uploader("Choose an image")
|
90 |
+
if uploaded_file is not None:
|
91 |
+
if save_uploaded_file(uploaded_file):
|
92 |
+
display_image = Image.open(uploaded_file)
|
93 |
+
st.image(display_image)
|
94 |
+
# feature extract
|
95 |
+
features = feature_extraction(os.path.join("uploads",uploaded_file.name),model)
|
96 |
+
# recommendention
|
97 |
+
indices = recommend(features,feature_list)
|
98 |
+
# show
|
99 |
+
st.header("Recommend For You....")
|
100 |
+
st.text("")
|
101 |
+
col1,col2,col3,col4,col5 = st.columns(5)
|
102 |
+
with col1:
|
103 |
+
st.image(filenames[indices[0][1]])
|
104 |
+
with col2:
|
105 |
+
st.image(filenames[indices[0][2]])
|
106 |
+
with col3:
|
107 |
+
st.image(filenames[indices[0][3]])
|
108 |
+
with col4:
|
109 |
+
st.image(filenames[indices[0][4]])
|
110 |
+
with col5:
|
111 |
+
st.image(filenames[indices[0][5]])
|
112 |
+
else:
|
113 |
+
st.header("Some error occured in file upload")
|
114 |
+
|
115 |
+
|
116 |
+
elif option=='FRM':
|
117 |
+
uploaded_file = st.file_uploader("Choose an image")
|
118 |
+
if uploaded_file is not None:
|
119 |
+
if save_uploaded_file(uploaded_file):
|
120 |
+
display_image = Image.open(uploaded_file)
|
121 |
+
st.image(display_image)
|
122 |
+
# feature extract
|
123 |
+
features = feature_extraction(os.path.join("uploads",uploaded_file.name),model)
|
124 |
+
# recommendention
|
125 |
+
indices = recommend_myntra(features,feature_list)
|
126 |
+
# show
|
127 |
+
st.header("Recommend For You....")
|
128 |
+
st.text("")
|
129 |
+
col1,col2,col3,col4,col5 = st.columns(5)
|
130 |
+
with col1:
|
131 |
+
st.image(filenames_myntra[indices[0][1]])
|
132 |
+
with col2:
|
133 |
+
st.image(filenames_myntra[indices[0][2]])
|
134 |
+
with col3:
|
135 |
+
st.image(filenames_myntra[indices[0][3]])
|
136 |
+
with col4:
|
137 |
+
st.image(filenames_myntra[indices[0][4]])
|
138 |
+
with col5:
|
139 |
+
st.image(filenames_myntra[indices[0][5]])
|
140 |
+
else:
|
141 |
+
st.header("Some error occured in file upload")
|
142 |
+
|
143 |
+
|
144 |
+
elif option=="AB":
|
145 |
+
st.markdown("FR: First Model Only Recommend Women Fashion Dresses...")
|
146 |
+
st.markdown("FRM: Second Model Recommend Men Women include also footwears and clothes.")
|
147 |
+
st.title("Product Recommendation Engine V-2.0")
|
148 |
+
st.markdown("This Engine Developed by <a href='https://github.com/datamind321'>DataMind Platform</a>",unsafe_allow_html=True)
|
149 |
+
st.subheader("if you have any query Contact us on : bme19rahul.r@invertisuniversity.ac.in")
|
150 |
+
st.markdown("More on : ")
|
151 |
+
|
152 |
+
|
153 |
+
st.markdown("[![Linkedin](https://content.linkedin.com/content/dam/me/business/en-us/amp/brand-site/v2/bg/LI-Bug.svg.original.svg)](https://www.linkedin.com/in/rahul-rathour-402408231/)",unsafe_allow_html=True)
|
154 |
+
|
155 |
+
|
156 |
+
st.markdown("[![Instagram](https://img.icons8.com/color/1x/instagram-new.png)](https://instagram.com/_technical__mind?igshid=YmMyMTA2M2Y=)")
|
157 |
+
|
158 |
+
|
embedding_large.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7c43edde14bf1064d3b57543dafd687bfd47b9523ccbeefb1684cf9a0bc5e2e2
|
3 |
+
size 116924045
|
embedding_myntra.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6f9a02954886f35f0fd2450d156d94921f8589047c2bbc0123773a752ef2e70d
|
3 |
+
size 23908148
|
filenames_large.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:602fbb469cad2793999dadee5d009348a96c5624fc4487ee7d8086c04bb55266
|
3 |
+
size 312069
|
filenames_myntra.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:461692e11e7b5fffbc50fd9c1f8b1c96f134c98748e4cfec799a88155c787fa9
|
3 |
+
size 48891
|