Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,74 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
from PIL import Image
|
4 |
-
import pickle
|
5 |
-
import tensorflow
|
6 |
-
import numpy as np
|
7 |
-
from numpy.linalg import norm
|
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 |
-
|
13 |
-
feature_list = np.array(pickle.load(open('embeddings2.pkl', 'rb')))
|
14 |
-
filenames = pickle.load(open('filenames2.pkl', 'rb'))
|
15 |
-
|
16 |
-
model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
17 |
-
model.trainable = False
|
18 |
-
|
19 |
-
model = tensorflow.keras.Sequential([
|
20 |
-
model,
|
21 |
-
GlobalMaxPooling2D()
|
22 |
-
])
|
23 |
-
|
24 |
-
st.title("Fashion Recommender System")
|
25 |
-
|
26 |
-
|
27 |
-
def extract_features(img_path, model):
|
28 |
-
img = image.load_img(img_path, target_size=(224, 224))
|
29 |
-
image_array = image.img_to_array(img)
|
30 |
-
expanded_image_array = np.expand_dims(image_array, axis=0)
|
31 |
-
processed_image = preprocess_input(expanded_image_array)
|
32 |
-
result = model.predict(processed_image).flatten()
|
33 |
-
normalized_result = result / norm(result)
|
34 |
-
return normalized_result
|
35 |
-
|
36 |
-
def recommend(features,feature_list):
|
37 |
-
neighbors = NearestNeighbors(n_neighbors=5, algorithm='brute', metric='euclidean')
|
38 |
-
neighbors.fit(feature_list)
|
39 |
-
|
40 |
-
distances, indices = neighbors.kneighbors([features])
|
41 |
-
return indices
|
42 |
-
|
43 |
-
|
44 |
-
def save_uploaded_file(uploaded_file):
|
45 |
-
try:
|
46 |
-
with open(os.path.join('uploads', uploaded_file.name), 'wb') as f:
|
47 |
-
f.write(uploaded_file.getbuffer())
|
48 |
-
return 1
|
49 |
-
except:
|
50 |
-
return 0
|
51 |
-
|
52 |
-
|
53 |
-
uploaded_file = st.file_uploader("choose an image")
|
54 |
-
|
55 |
-
if uploaded_file is not None:
|
56 |
-
if save_uploaded_file(uploaded_file):
|
57 |
-
display_image = Image.open(uploaded_file)
|
58 |
-
st.image(display_image)
|
59 |
-
features = extract_features(os.path.join("uploads",uploaded_file.name),model)
|
60 |
-
#st.text(features)
|
61 |
-
indices = recommend(features,feature_list)
|
62 |
-
col1,col2,col3,col4,col5 = st.columns(5)
|
63 |
-
with col1:
|
64 |
-
st.image(filenames[indices[0][0]])
|
65 |
-
with col2:
|
66 |
-
st.image(filenames[indices[0][1]])
|
67 |
-
with col3:
|
68 |
-
st.image(filenames[indices[0][2]])
|
69 |
-
with col4:
|
70 |
-
st.image(filenames[indices[0][3]])
|
71 |
-
with col5:
|
72 |
-
st.image(filenames[indices[0][4]])
|
73 |
-
else:
|
74 |
-
st.header("Some error has occured while uploading file")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from PIL import Image
|
4 |
+
import pickle
|
5 |
+
import tensorflow
|
6 |
+
import numpy as np
|
7 |
+
from numpy.linalg import norm
|
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 |
+
|
13 |
+
feature_list = np.array(pickle.load(open('C:/Users/sanath/PycharmProjects/Fasion Recommender System/embeddings2.pkl', 'rb')))
|
14 |
+
filenames = pickle.load(open('C:/Users/sanath/PycharmProjects/Fasion Recommender System/filenames2.pkl', 'rb'))
|
15 |
+
|
16 |
+
model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
17 |
+
model.trainable = False
|
18 |
+
|
19 |
+
model = tensorflow.keras.Sequential([
|
20 |
+
model,
|
21 |
+
GlobalMaxPooling2D()
|
22 |
+
])
|
23 |
+
|
24 |
+
st.title("Fashion Recommender System")
|
25 |
+
|
26 |
+
|
27 |
+
def extract_features(img_path, model):
|
28 |
+
img = image.load_img(img_path, target_size=(224, 224))
|
29 |
+
image_array = image.img_to_array(img)
|
30 |
+
expanded_image_array = np.expand_dims(image_array, axis=0)
|
31 |
+
processed_image = preprocess_input(expanded_image_array)
|
32 |
+
result = model.predict(processed_image).flatten()
|
33 |
+
normalized_result = result / norm(result)
|
34 |
+
return normalized_result
|
35 |
+
|
36 |
+
def recommend(features,feature_list):
|
37 |
+
neighbors = NearestNeighbors(n_neighbors=5, algorithm='brute', metric='euclidean')
|
38 |
+
neighbors.fit(feature_list)
|
39 |
+
|
40 |
+
distances, indices = neighbors.kneighbors([features])
|
41 |
+
return indices
|
42 |
+
|
43 |
+
|
44 |
+
def save_uploaded_file(uploaded_file):
|
45 |
+
try:
|
46 |
+
with open(os.path.join('uploads', uploaded_file.name), 'wb') as f:
|
47 |
+
f.write(uploaded_file.getbuffer())
|
48 |
+
return 1
|
49 |
+
except:
|
50 |
+
return 0
|
51 |
+
|
52 |
+
|
53 |
+
uploaded_file = st.file_uploader("choose an image")
|
54 |
+
|
55 |
+
if uploaded_file is not None:
|
56 |
+
if save_uploaded_file(uploaded_file):
|
57 |
+
display_image = Image.open(uploaded_file)
|
58 |
+
st.image(display_image)
|
59 |
+
features = extract_features(os.path.join("uploads",uploaded_file.name),model)
|
60 |
+
#st.text(features)
|
61 |
+
indices = recommend(features,feature_list)
|
62 |
+
col1,col2,col3,col4,col5 = st.columns(5)
|
63 |
+
with col1:
|
64 |
+
st.image(filenames[indices[0][0]])
|
65 |
+
with col2:
|
66 |
+
st.image(filenames[indices[0][1]])
|
67 |
+
with col3:
|
68 |
+
st.image(filenames[indices[0][2]])
|
69 |
+
with col4:
|
70 |
+
st.image(filenames[indices[0][3]])
|
71 |
+
with col5:
|
72 |
+
st.image(filenames[indices[0][4]])
|
73 |
+
else:
|
74 |
+
st.header("Some error has occured while uploading file")
|