-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
261 lines (233 loc) · 15 KB
/
main.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import streamlit as st
import os
from PIL import Image
import numpy as np
import pickle
import tensorflow
from tensorflow.keras.preprocessing import image
from tensorflow.keras.layers import GlobalMaxPooling2D
from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input
from sklearn.neighbors import NearestNeighbors
from numpy.linalg import norm
import streamlit.components.v1 as components
feature_list = np.array(pickle.load(open('embeddings.pkl','rb')))
filenames = pickle.load(open('filenames.pkl','rb'))
model = ResNet50(weights='imagenet',include_top=False,input_shape=(224,224,3))
model.trainable = False
model = tensorflow.keras.Sequential([
model,
GlobalMaxPooling2D()
])
st. set_page_config(page_title="Fipkart",page_icon="",layout="wide")
import numpy as np
st.image("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQMAAADCCAMAAAB6zFdcAAABQVBMVEX///8Qe9T45CoAc9L9zQkAeNP63B771xcAdtMAcdH35yv9ywD72Rr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uFM47cSLoxkH5ujWIMAxXOdeGVV5cXDx/ePp+4+zLx88f3jhG9VNV4Ckhdg4uyvLrmjemgb+/eL2gBotlhM8FBwfnl5eXD4FLtWg0PsLhuZLgxjSw1QrwurHKgS/klqUYgjcqJ73EQ/+yG9LgKyu9ZBEazOEvPs3JV+XHKj1U/yjLdnnVDWkQrXKuDYtbHUv5T0+Dx5GmG9KgX2NJfstgNrM12JCFle5EWuZvak7kSfEGIoFrMEOCufIXpcHn8i1pcGMU5mdooEol43VUg7v2HU1tMh/P4n+Y+HJEdWMu3HJw211Onmd/i2W/rvgr3HBX/ycLQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQRAEQdxh/gtcSUZ8GcEbxAAAAABJRU5ErkJggg==", caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
page = """ <div class="container-fluid bg-primary Header fixed-top">
<div class="row py-2 d-flex">
<!-- Logo -->
<div class="col logo offset-lg-1">
<a href="#">
<img width="75" src="//img1a.flixcart.com/www/linchpin/fk-cp-zion/img/flipkart-plus_8d85f4.png"
alt="Flipkart" title="Flipkart">
</a>
<a href="#">Explore <span>Plus</span>
<img width="10" src="//img1a.flixcart.com/www/linchpin/fk-cp-zion/img/plus_aef861.png">
</a>
</div>
<!-- Search -->
<div class="col col-md-4 search d-flex dropdown bg-white">
<input class="form-control dropdown-toggle" type="search"
placeholder="Search for products, brands and more" aria-label="Search" id="navbarDropdown"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-search mt-2 ml-sm-1 text-primary"></i>
<div class="dropdown-menu col-12 search-item" aria-labelledby="navbarDropdown">
<h6 class="ml-4">Discover More</h6>
<div class="dropdown-divider"></div>
<a class="dropdown-item py-2" href=""><i class="fa fa-search text-secondary mr-3"></i>mobiles</a>
<a class="dropdown-item py-2" href=""><i class="fa fa-search text-secondary mr-3"></i>t-shirts</a>
<a class="dropdown-item py-2" href=""><i class="fa fa-search text-secondary mr-3"></i>shoes</a>
<a class="dropdown-item py-2" href=""><i class="fa fa-search text-secondary mr-3"></i>laptop</a>
<a class="dropdown-item py-2" href=""><i class="fa fa-search text-secondary mr-3"></i>tv</a>
<a class="dropdown-item py-2" href=""><i class="fa fa-search text-secondary mr-3"></i>sarees</a>
</div>
</div>
<div class="col upload">
<button class="form-control"><a href="http://localhost:8501">upload image</a></button>
</div>
<!-- Login -->
<div class="col dropdown login">
<button class="btn bg-white text-primary" type="button" id="loginMenuButton" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="true">
Login
</button>
<div class="dropdown-menu login-list col-12 aria-labelledby=" loginMenuButton">
<div class="d-flex">
<h6 class="ml-md-1">New Customer?</h6>
<a href="#" class="ml-auto mr-2" id="signUp">Sign Up</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-user-circle text-primary mt-2 ml-2" aria-hidden="true"></i>
<a class="dropdown-item" href="">My Profile</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-plus text-primary mt-2 ml-2" aria-hidden="true"></i>
<a class="dropdown-item" href="">Flipkart Plus Zone</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-book text-primary mt-2 ml-2" aria-hidden="true"></i>
<a class="dropdown-item" href="">Orders</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-heart text-primary mt-2 ml-2" aria-hidden="true"></i>
<a class="dropdown-item" href="">Wishlist</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-chess-bishop text-primary mt-2 ml-2" aria-hidden="true"></i>
<a class="dropdown-item" href="">Rewards</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-gift text-primary mt-2 ml-2" aria-hidden="true"></i>
<a class="dropdown-item" href="">Gift Cards</a>
</div>
</div>
</div>
<!-- More -->
<div class="col dropdown more">
<a class="btn dropdown-toggle text-white ml-lg-2 ml-sm-0" href="#" role="button" id="moreMenuLink"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
More
</a>
<div class="dropdown-menu more-list" aria-labelledby="moreMenuLink">
<div class="d-flex">
<i class="fa fa-bell text-primary ml-md-3 mt-2" aria-hidden="true"></i>
<a class="dropdown-item" href="#">Notification Preferences</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-archive text-primary ml-md-3 mt-2" aria-hidden="true"></i>
<a class="dropdown-item" href="#">Sell On Flipkart</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-question-circle text-primary ml-md-3 mt-2" aria-hidden="true"></i>
<a class="dropdown-item" href="#">24x7 Customer Care</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-chart-line text-primary ml-md-3 mt-2" aria-hidden="true"></i>
<a class="dropdown-item" href="#">Advertise</a>
</div>
<div class="dropdown-divider"></div>
<div class="d-flex">
<i class="fa fa-download text-primary ml-md-3 mt-2" aria-hidden="true"></i>
<a class="dropdown-item" href="#">Download App</a>
</div>
</div>
</div>
<!-- Cart -->
<div class="col col-md-1 d-flex justify-content-center">
<i class="fa fa-shopping-cart text-white mt-2" aria-hidden="true"></i>
<a href="" class="btn text-white">Cart</a>
</div>
</div>
</div> """
st.title('Find Product from Image "Recommender System"')
def save_uploaded_file(uploaded_file):
try:
with open(os.path.join('uploads',uploaded_file.name),'wb') as f:
f.write(uploaded_file.getbuffer())
return 1
except:
return 0
def feature_extraction(img_path,model):
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
expanded_img_array = np.expand_dims(img_array, axis=0)
preprocessed_img = preprocess_input(expanded_img_array)
result = model.predict(preprocessed_img).flatten()
normalized_result = result / norm(result)
return normalized_result
def recommend(features,feature_list):
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
neighbors.fit(feature_list)
distances, indices = neighbors.kneighbors([features])
return indices
# steps
# file upload -> save
# uploaded_file = st.file_uploader("Choose an image")
with st.chat_message("user"):
st.write("Hello 👋")
st.write("I am your flipkart assiatant...")
st.write("Please Upload the image for you want Recommendation")
uploaded_file = st.file_uploader("Choose an image")
# st.line_chart(np.random.randn(30, 3))
if uploaded_file is not None:
if save_uploaded_file(uploaded_file):
# display the file
with st.chat_message("user"):
st.write("Showing recommendations for this Inmage : ")
display_image = Image.open(uploaded_file)
st.image(display_image)
# feature extract
features = feature_extraction(os.path.join("uploads",uploaded_file.name),model)
#st.text(features)
# recommendention
indices = recommend(features,feature_list)
# show
message = st.chat_message("user")
message.write("Hello human here are some recommendations :")
# message.bar_chart(np.random.randn(30, 3))
col1,col2,col3,col4,col5 = st.columns(5)
with col1:
st.image(filenames[indices[0][0]])
with col2:
st.image(filenames[indices[0][1]])
with col3:
st.image(filenames[indices[0][2]])
with col4:
st.image(filenames[indices[0][3]])
with col5:
st.image(filenames[indices[0][4]])
if len(indices)>=10 :
col6,col7,col8,col9,col10 = st.columns(5)
with col6:
st.image(filenames[indices[0][5]])
with col7:
st.image(filenames[indices[0][6]])
with col8:
st.image(filenames[indices[0][7]])
with col9:
st.image(filenames[indices[0][8]])
with col10:
st.image(filenames[indices[0][9]])
if len(indices) >= 15 :
col11,col12,col13,col14,col15 = st.columns(5)
with col11:
st.image(filenames[indices[0][10]])
with col12:
st.image(filenames[indices[0][11]])
with col13:
st.image(filenames[indices[0][12]])
with col14:
st.image(filenames[indices[0][13]])
with col15:
st.image(filenames[indices[0][14]])
if len(indices) >= 20 :
col16,col17,col18,col19,col20 = st.columns(5)
with col16:
st.image(filenames[indices[0][15]])
with col17:
st.image(filenames[indices[0][16]])
with col18:
st.image(filenames[indices[0][17]])
with col19:
st.image(filenames[indices[0][18]])
with col20:
st.image(filenames[indices[0][19]])
else:
st.header("Some error occured in file upload")