Manasa1 commited on
Commit
092df1b
1 Parent(s): 4b3eeff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -55
app.py CHANGED
@@ -1,55 +1,131 @@
1
- import numpy as np
2
- import tensorflow
3
-
4
- from tensorflow.keras.preprocessing import image
5
- from tensorflow.keras.layers import GlobalMaxPooling2D
6
- from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input
7
- from numpy.linalg import norm
8
- import os
9
- from tqdm import tqdm
10
- import pickle
11
-
12
- model = ResNet50(weights="imagenet", include_top=False,input_shape=(224,224,3))
13
- model.trainable=False
14
-
15
- model1 = tensorflow.keras.Sequential([
16
- model,
17
- GlobalMaxPooling2D()
18
- ])
19
-
20
- def extract_features(img_path,model):
21
- img=image.load_img(img_path,target_size = (224,224))
22
- image_array = image.img_to_array(img)
23
- expanded_image_array = np.expand_dims(image_array,axis=0)
24
- processed_image = preprocess_input(expanded_image_array)
25
- result = model.predict(processed_image).flatten()
26
- normalized_result=result/norm(result)
27
- return normalized_result
28
-
29
- filenames =[]
30
-
31
- for file in os.listdir('set0'):
32
- filenames.append(os.path.join('set0',file))
33
-
34
- for file in os.listdir('set1'):
35
- filenames.append(os.path.join('set1',file))
36
-
37
- for file in os.listdir('set2'):
38
- filenames.append(os.path.join('set2',file))
39
-
40
- for file in os.listdir('set3'):
41
- filenames.append(os.path.join('set3',file))
42
-
43
- for file in os.listdir('set4'):
44
- filenames.append(os.path.join('set4',file))
45
-
46
- feature_list = []
47
-
48
- for i in tqdm(filenames):
49
- feature_list.append(extract_features(i,model1))
50
-
51
- print(np.array(feature_list).shape)
52
-
53
- import pickle
54
- pickle.dump(feature_list,open('embeddings2.pkl','wb'))
55
- pickle.dump(filenames,open('filenames2.pkl','wb'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import tensorflow
3
+
4
+ from tensorflow.keras.preprocessing import image
5
+ from tensorflow.keras.layers import GlobalMaxPooling2D
6
+ from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input
7
+ from numpy.linalg import norm
8
+ import os
9
+ from tqdm import tqdm
10
+ import pickle
11
+
12
+ model = ResNet50(weights="imagenet", include_top=False,input_shape=(224,224,3))
13
+ model.trainable=False
14
+
15
+ model1 = tensorflow.keras.Sequential([
16
+ model,
17
+ GlobalMaxPooling2D()
18
+ ])
19
+
20
+ def extract_features(img_path,model):
21
+ img=image.load_img(img_path,target_size = (224,224))
22
+ image_array = image.img_to_array(img)
23
+ expanded_image_array = np.expand_dims(image_array,axis=0)
24
+ processed_image = preprocess_input(expanded_image_array)
25
+ result = model.predict(processed_image).flatten()
26
+ normalized_result=result/norm(result)
27
+ return normalized_result
28
+
29
+ filenames =[]
30
+
31
+ for file in os.listdir('set0'):
32
+ filenames.append(os.path.join('set0',file))
33
+
34
+ for file in os.listdir('set1'):
35
+ filenames.append(os.path.join('set1',file))
36
+
37
+ for file in os.listdir('set2'):
38
+ filenames.append(os.path.join('set2',file))
39
+
40
+ for file in os.listdir('set3'):
41
+ filenames.append(os.path.join('set3',file))
42
+
43
+ for file in os.listdir('set4'):
44
+ filenames.append(os.path.join('set4',file))
45
+
46
+ feature_list = []
47
+
48
+ for i in tqdm(filenames):
49
+ feature_list.append(extract_features(i,model1))
50
+
51
+ print(np.array(feature_list).shape)
52
+
53
+ import pickle
54
+ pickle.dump(feature_list,open('embeddings2.pkl','wb'))
55
+ pickle.dump(filenames,open('filenames2.pkl','wb'))
56
+
57
+
58
+ import streamlit as st
59
+ import os
60
+ from PIL import Image
61
+ import pickle
62
+ import tensorflow
63
+ import numpy as np
64
+ from numpy.linalg import norm
65
+ from tensorflow.keras.preprocessing import image
66
+ from tensorflow.keras.layers import GlobalMaxPooling2D
67
+ from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
68
+ from sklearn.neighbors import NearestNeighbors
69
+
70
+ feature_list = np.array(pickle.load(open('embeddings2.pkl', 'rb')))
71
+ filenames = pickle.load(open('filenames2.pkl', 'rb'))
72
+
73
+ model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
74
+ model.trainable = False
75
+
76
+ model = tensorflow.keras.Sequential([
77
+ model,
78
+ GlobalMaxPooling2D()
79
+ ])
80
+
81
+ st.title("Fashion Recommender System")
82
+
83
+
84
+ def extract_features(img_path, model):
85
+ img = image.load_img(img_path, target_size=(224, 224))
86
+ image_array = image.img_to_array(img)
87
+ expanded_image_array = np.expand_dims(image_array, axis=0)
88
+ processed_image = preprocess_input(expanded_image_array)
89
+ result = model.predict(processed_image).flatten()
90
+ normalized_result = result / norm(result)
91
+ return normalized_result
92
+
93
+ def recommend(features,feature_list):
94
+ neighbors = NearestNeighbors(n_neighbors=5, algorithm='brute', metric='euclidean')
95
+ neighbors.fit(feature_list)
96
+
97
+ distances, indices = neighbors.kneighbors([features])
98
+ return indices
99
+
100
+
101
+ def save_uploaded_file(uploaded_file):
102
+ try:
103
+ with open(os.path.join('uploads', uploaded_file.name), 'wb') as f:
104
+ f.write(uploaded_file.getbuffer())
105
+ return 1
106
+ except:
107
+ return 0
108
+
109
+
110
+ uploaded_file = st.file_uploader("choose an image")
111
+
112
+ if uploaded_file is not None:
113
+ if save_uploaded_file(uploaded_file):
114
+ display_image = Image.open(uploaded_file)
115
+ st.image(display_image)
116
+ features = extract_features(os.path.join("uploads",uploaded_file.name),model)
117
+ #st.text(features)
118
+ indices = recommend(features,feature_list)
119
+ col1,col2,col3,col4,col5 = st.columns(5)
120
+ with col1:
121
+ st.image(filenames[indices[0][0]])
122
+ with col2:
123
+ st.image(filenames[indices[0][1]])
124
+ with col3:
125
+ st.image(filenames[indices[0][2]])
126
+ with col4:
127
+ st.image(filenames[indices[0][3]])
128
+ with col5:
129
+ st.image(filenames[indices[0][4]])
130
+ else:
131
+ st.header("Some error has occured while uploading file")