-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
272 lines (176 loc) · 6.16 KB
/
app.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
261
# """ IMPORTING ALL THE REQUIRED LIBRARIES"""
# Streamlit
import streamlit as st
# Pillow library for image
from PIL import Image
#
import cv2
# Numpy for converting image into arrays
import numpy as np
# Importing pickle for storing and loading the models
import pickle
# """IMPORTING TENSORFLOW AND SIMILAR LIBRARIES"""
import tensorflow
from tensorflow.keras.preprocessing import image
from tensorflow.keras.layers import GlobalMaxPooling2D
from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input
# Import NEAREST NEIGHBORS Algorithm from Scikit-Learn
from sklearn.neighbors import NearestNeighbors
# To display progress
from numpy.linalg import norm
# Importing os for file path
import os
# """LOADING MODELS USING PICKLE
# i) feature_list
# ii) filenames """
feature_list = np.array(pickle.load(open('embeddings4.pkl','rb')))
filenames = pickle.load(open('filenames34.pkl','rb'))
# USING RESNet50 Model
model = ResNet50(weights='imagenet',include_top=False,input_shape=(224,224,3))
model.trainable = False
model = tensorflow.keras.Sequential([
model,
GlobalMaxPooling2D()
])
#DROPDOWN for brands
option = st.sidebar.selectbox(
'Choose your favourite BRANDS',
('WELCOME','ADDIDAS', 'AMERICAN TOURISTER','LAKME','LEVIS','NIVEA','PUMA'))
# """FUNCTION TO EXTRACT FEATURES from images Logo's of brands given """
def feature_extraction(img_path,model):
# passing the images
img = image.load_img(img_path, target_size=(224, 224))
# """Converting into numpy array of images and expanding it and then proprocessing it"""
img_array = image.img_to_array(img)
expanded_img_array = np.expand_dims(img_array, axis=0)
preprocessed_img = preprocess_input(expanded_img_array)
# predicting the model in result variable and flattening
result = model.predict(preprocessed_img).flatten()
# normalising the result
normalized_result = result / norm(result)
# Returning the normalized result
return normalized_result
# """"FUNCTION FOR GETTING SIMILAR IMAGES BASED ON LOGO USING NEAREST NEIGHBORS ALGORITHMS"""
def recommend(features,feature_list):
neighbors = NearestNeighbors(n_neighbors=20, algorithm='brute', metric='euclidean')
neighbors.fit(feature_list)
distances, indices = neighbors.kneighbors([features])
# returning indices
return indices
# FUNCTION FOR NIVEA
def NIVEA():
# Uploading the image logo (NIVEA LOGO)
original = Image.open('BrandImages/Nivea_logo.svg (1).png')
st.image(original)
st.title(option)
# reading the NIVEA logo file
image2 = cv2.imread('BrandImages/NIVEA.png')
features = feature_extraction(os.path.join('BrandImages/NIVEA.png'), model)
indices = recommend(features, feature_list)
# MAKING 3 COLUMNS
col1, col2, col3 = st.columns(3)
with col1:
st.image(filenames[indices[0][1]])
st.caption('ARJUN RAMPAL')
with col2:
st.image(filenames[indices[0][11]])
st.caption('TEEJAY SIDHU')
# first dropdown of WELCOME
def WELCOME():
# st.caption('customer adviser')
# LOADING THE BACKGROUND IMAGE
st.image('BrandImages/BGround.png')
# FUNCTION FOR PUMA
def PUMA():
original = Image.open('BrandImages/PUMA.png')
st.title(option)
st.image(original)
image2 = cv2.imread('BrandImages/PUMA.png')
features = feature_extraction(os.path.join('BrandImages/PUMA.png'), model)
indices = recommend(features, feature_list)
# show
col1, col2, col3 = st.columns(3)
with col1:
st.image(filenames[indices[0][1]])
st.caption('VIRAT KOHLI')
with col2:
st.image(filenames[indices[0][0]])
st.caption('K.L RAHUL')
with col3:
st.image(filenames[indices[0][4]])
st.caption('BANI J')
# FUNCTION LAKME
def LAKME():
original = Image.open('BrandImages/LAKME.png')
st.image(original)
st.header(option)
image2 = cv2.imread('BrandImages/LAKME.png')
features = feature_extraction(os.path.join('BrandImages/LAKME.png'), model)
indices = recommend(features, feature_list)
# show
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
st.image(filenames[indices[0][1]])
st.caption('DIA MIRZA')
with col2:
st.image(filenames[indices[0][11]])
st.caption('ARPITA MEHTA')
# AMERICAN TOURISTER function
def AT():
original = Image.open('BrandImages/AmericanTourister.jpg')
st.image(original)
st.header(option)
image2 = cv2.imread('BrandImages/AmericanTourister.jpg')
features = feature_extraction(os.path.join('BrandImages/AmericanTourister.jpg'), model)
indices = recommend(features, feature_list)
# show
col1, col2, col3 = st.columns(3)
with col1:
st.image(filenames[indices[0][0]])
st.caption('RONALDO')
# ADDIDAS function
def ADDIDAS():
original = Image.open('BrandImages/Addidas.jpg')
st.image(original)
st.title(option)
image2 = cv2.imread('BrandImages/Addidas.jpg')
features = feature_extraction(os.path.join('BrandImages/Addidas.jpg'), model)
indices = recommend(features, feature_list)
# show
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
st.image(filenames[indices[0][5]])
st.caption('HIMA DAS')
# funtion for LEVI'S
def LEVI():
original = Image.open('BrandImages/LEVI.png')
st.image(original)
st.title("LEVI'S")
image2 = cv2.imread('BrandImages/LEVI.png')
features = feature_extraction(os.path.join('BrandImages/LEVI.png'), model)
indices = recommend(features, feature_list)
# show
col1, col2 = st.columns(2)
with col1:
st.image(filenames[indices[0][4]])
st.caption('ZOYA AKHTAR')
with col2:
st.image(filenames[indices[0][5]])
st.caption('HARSHVARDHAN KAPOOR')
#
# """BASED ON THE BRANDS SELECTED BY THE USER THE RESPETIVE FUNCTION IS CALLED
# for example if user SELECTS NIVEA brand then NIVEA function is called and similarly"""
if option == 'WELCOME':
WELCOME()
if option == 'NIVEA':
NIVEA()
if option=='LAKME':
LAKME()
if option =='PUMA':
PUMA()
if option =='ADDIDAS':
ADDIDAS()
if option=='AMERICAN TOURISTER':
AT()
if option=='LEVIS':
LEVI()