Spaces:
Sleeping
Sleeping
Manasa B Rao
commited on
Commit
•
4b3eeff
1
Parent(s):
ab9e5d2
Add files via upload
Browse files- app.py +55 -0
- filenames2.pkl +0 -0
- main.py +74 -0
- test.py +43 -0
- uploads/i1.jpg +0 -0
- uploads/i1.webp +0 -0
- uploads/i2.jpg +0 -0
- uploads/i3.jpg +0 -0
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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'))
|
filenames2.pkl
ADDED
Binary file (749 kB). View file
|
|
main.py
ADDED
@@ -0,0 +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")
|
test.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import tensorflow
|
3 |
+
import numpy as np
|
4 |
+
from numpy.linalg import norm
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
from tensorflow.keras.layers import GlobalMaxPooling2D
|
7 |
+
from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input
|
8 |
+
from sklearn.neighbors import NearestNeighbors
|
9 |
+
import cv2
|
10 |
+
|
11 |
+
feature_list = np.array(pickle.load(open('embeddings.pkl','rb')))
|
12 |
+
filenames = pickle.load(open('filenames.pkl','rb'))
|
13 |
+
|
14 |
+
model = ResNet50(weights='imagenet',include_top=False,input_shape=(224,224,3))
|
15 |
+
model.trainable = False
|
16 |
+
|
17 |
+
model = tensorflow.keras.Sequential([
|
18 |
+
model,
|
19 |
+
GlobalMaxPooling2D()
|
20 |
+
])
|
21 |
+
|
22 |
+
img = image.load_img('sample/i4.jpg',target_size=(224,224))
|
23 |
+
img_array = image.img_to_array(img)
|
24 |
+
expanded_img_array = np.expand_dims(img_array, axis=0)
|
25 |
+
preprocessed_img = preprocess_input(expanded_img_array)
|
26 |
+
result = model.predict(preprocessed_img).flatten()
|
27 |
+
normalized_result = result / norm(result)
|
28 |
+
|
29 |
+
neighbors = NearestNeighbors(n_neighbors=5,algorithm='brute',metric='euclidean')
|
30 |
+
neighbors.fit(feature_list)
|
31 |
+
|
32 |
+
distances,indices = neighbors.kneighbors([normalized_result])
|
33 |
+
|
34 |
+
print(indices)
|
35 |
+
|
36 |
+
for file in indices[0][0:5]:
|
37 |
+
temp_img = cv2.imread(filenames[file])
|
38 |
+
cv2.imshow('output',cv2.resize(temp_img,(512,512)))
|
39 |
+
cv2.waitKey(0)
|
40 |
+
|
41 |
+
distances,indices = neighbors.kneighbors([normalized_result])
|
42 |
+
|
43 |
+
print(indices)
|
uploads/i1.jpg
ADDED
uploads/i1.webp
ADDED
uploads/i2.jpg
ADDED
uploads/i3.jpg
ADDED