lavanjv commited on
Commit
93406ff
·
verified ·
1 Parent(s): 13a5a37

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,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
+
8
+ from sklearn.neighbors import NearestNeighbors
9
+ import os
10
+ from numpy.linalg import norm
11
+ import streamlit as st
12
+
13
+ st.header('Fashion Recommendation System')
14
+
15
+ Image_features = pkl.load(open('Images_features.pkl','rb'))
16
+ filenames = pkl.load(open('filenames.pkl','rb'))
17
+
18
+ def extract_features_from_images(image_path, model):
19
+ img = image.load_img(image_path, target_size=(224,224))
20
+ img_array = image.img_to_array(img)
21
+ img_expand_dim = np.expand_dims(img_array, axis=0)
22
+ img_preprocess = preprocess_input(img_expand_dim)
23
+ result = model.predict(img_preprocess).flatten()
24
+ norm_result = result/norm(result)
25
+ return norm_result
26
+ model = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3))
27
+ model.trainable = False
28
+
29
+ model = tf.keras.models.Sequential([model,
30
+ GlobalMaxPool2D()
31
+ ])
32
+ neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
33
+ neighbors.fit(Image_features)
34
+ upload_file = st.file_uploader("Upload Image")
35
+ if upload_file is not None:
36
+ with open(os.path.join('upload', upload_file.name), 'wb') as f:
37
+ f.write(upload_file.getbuffer())
38
+ st.subheader('Uploaded Image')
39
+ st.image(upload_file)
40
+ input_img_features = extract_features_from_images(upload_file, model)
41
+ distance,indices = neighbors.kneighbors([input_img_features])
42
+ st.subheader('Recommended Images')
43
+ col1,col2,col3,col4,col5 = st.columns(5)
44
+ with col1:
45
+ st.image(filenames[indices[0][1]])
46
+ with col2:
47
+ st.image(filenames[indices[0][2]])
48
+ with col3:
49
+ st.image(filenames[indices[0][3]])
50
+ with col4:
51
+ st.image(filenames[indices[0][4]])
52
+ with col5:
53
+ st.image(filenames[indices[0][5]])