lavanjv commited on
Commit
547b42c
·
verified ·
1 Parent(s): 4bb55f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -53
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
- 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]])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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]])