Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,66 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import pickle as pkl
|
3 |
-
import tensorflow as tf
|
4 |
-
from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input
|
5 |
-
from tensorflow.keras.preprocessing import image
|
6 |
-
from tensorflow.keras.layers import GlobalMaxPool2D
|
7 |
-
|
8 |
-
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
Image_features = pkl.load(open('Images_features.pkl','rb'))
|
16 |
-
filenames = pkl.load(open('filenames.pkl','rb'))
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
model =
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pickle as pkl
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
from tensorflow.keras.layers import GlobalMaxPool2D
|
7 |
+
from sklearn.neighbors import NearestNeighbors
|
8 |
+
import os
|
9 |
+
from numpy.linalg import norm
|
10 |
+
import streamlit as st
|
11 |
+
|
12 |
+
st.header('Fashion Recommendation System')
|
13 |
+
|
14 |
+
# Load precomputed features
|
15 |
+
Image_features = pkl.load(open('Images_features.pkl', 'rb'))
|
16 |
+
filenames = pkl.load(open('filenames.pkl', 'rb'))
|
17 |
+
|
18 |
+
# Feature extraction function
|
19 |
+
def extract_features_from_images(image_path, model):
|
20 |
+
img = image.load_img(image_path, target_size=(224, 224))
|
21 |
+
img_array = image.img_to_array(img)
|
22 |
+
img_expand_dim = np.expand_dims(img_array, axis=0)
|
23 |
+
img_preprocess = preprocess_input(img_expand_dim)
|
24 |
+
result = model.predict(img_preprocess).flatten()
|
25 |
+
norm_result = result / norm(result)
|
26 |
+
return norm_result
|
27 |
+
|
28 |
+
# Load ResNet50 model
|
29 |
+
model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
30 |
+
model.trainable = False
|
31 |
+
model = tf.keras.models.Sequential([model, GlobalMaxPool2D()])
|
32 |
+
|
33 |
+
# Nearest Neighbors Model
|
34 |
+
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
|
35 |
+
neighbors.fit(Image_features)
|
36 |
+
|
37 |
+
# Upload Image
|
38 |
+
upload_file = st.file_uploader("Upload Image")
|
39 |
+
if upload_file is not None:
|
40 |
+
upload_path = os.path.join('/tmp', upload_file.name) # Use /tmp instead of 'upload/'
|
41 |
+
|
42 |
+
with open(upload_path, 'wb') as f:
|
43 |
+
f.write(upload_file.getbuffer())
|
44 |
+
|
45 |
+
st.subheader('Uploaded Image')
|
46 |
+
st.image(upload_file)
|
47 |
+
|
48 |
+
# Extract features
|
49 |
+
input_img_features = extract_features_from_images(upload_path, model)
|
50 |
+
|
51 |
+
# Get recommendations
|
52 |
+
distance, indices = neighbors.kneighbors([input_img_features])
|
53 |
+
|
54 |
+
# Display Recommended Images
|
55 |
+
st.subheader('Recommended Images')
|
56 |
+
col1, col2, col3, col4, col5 = st.columns(5)
|
57 |
+
with col1:
|
58 |
+
st.image(filenames[indices[0][1]])
|
59 |
+
with col2:
|
60 |
+
st.image(filenames[indices[0][2]])
|
61 |
+
with col3:
|
62 |
+
st.image(filenames[indices[0][3]])
|
63 |
+
with col4:
|
64 |
+
st.image(filenames[indices[0][4]])
|
65 |
+
with col5:
|
66 |
+
st.image(filenames[indices[0][5]])
|