panotedi commited on
Commit
51905ca
·
unverified ·
1 Parent(s): 66630f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from tensorflow.keras.applications.resnet50 import ResNet50
4
+ from tensorflow.keras.preprocessing import image
5
+ from tensorflow.keras.applications.resnet50 import preprocess_input
6
+ from sklearn.neighbors import NearestNeighbors
7
+ from PIL import Image
8
+ import numpy as np
9
+ import glob
10
+ import os
11
+
12
+ resnet_model = ResNet50(weights='imagenet')
13
+
14
+ st.title("CS634 - Assignment 3")
15
+
16
+ user_image_input = st.file_uploader("Upload Images", type=["jpg"])
17
+
18
+
19
+ path='lfw/V*'
20
+ photos=[]
21
+ for fold in glob.glob(path, recursive=True):
22
+ for subdir, dirs, files in os.walk(fold):
23
+ for file in files:
24
+ #st.write(file)
25
+ photos.append(os.path.join(subdir, file))
26
+
27
+ photos.insert(0,"")
28
+ celebrity_photo = st.selectbox("Select Photo",photos)
29
+
30
+
31
+
32
+ def extract_features(photos, resnet_model):
33
+ features = {}
34
+ for photo in photos:
35
+ if(photo!=""):
36
+ img = image.load_img(photo, target_size=(224, 224))
37
+ x = image.img_to_array(img)
38
+ x = np.expand_dims(x, axis=0)
39
+ x = preprocess_input(x)
40
+
41
+ features_vector = resnet_model.predict(x)
42
+ features_vector = features_vector.flatten()
43
+ features[photo] = features_vector
44
+
45
+ return features
46
+
47
+
48
+
49
+
50
+ if(len(celebrity_photo) != 0):
51
+ #st.image(user_image_input, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
52
+ user_input_image = None
53
+ st.write(celebrity_photo)
54
+ #st.write(user_image_input.read())
55
+ size=len(photos)
56
+ #st.write(size)
57
+ st.write("Query Image: ")
58
+ st.image(celebrity_photo)
59
+ features = extract_features(photos, resnet_model)
60
+
61
+ features_array = np.array(list(features.values()))
62
+
63
+ nn_model = NearestNeighbors(n_neighbors=11, metric='euclidean')
64
+ nn_model.fit(features_array)
65
+
66
+ query_image_path = photos[size-1]
67
+ query_image_feature = features[query_image_path].reshape(1, -1)
68
+ distances, indices = nn_model.kneighbors(query_image_feature)
69
+
70
+
71
+ st.write("Similar Images:")
72
+ for i in range(1,11):
73
+ similar_image_path = photos[indices[0][i]]
74
+ similar_image_distance = distances[0][i]
75
+ st.write("Similar Image #{}: Distance: {}".format(i, similar_image_distance))
76
+ st.image(similar_image_path)
77
+
78
+
79
+ if(user_image_input != None):
80
+ celebrity_photo = []
81
+ #st.image(user_image_input, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
82
+ im = Image.open(user_image_input)
83
+ im=im.resize((224,224))
84
+ im.save("input_image.jpg", "JPEG")
85
+ photos.append("input_image.jpg")
86
+ #st.write(user_image_input.read())
87
+ size=len(photos)
88
+ #st.write(size)
89
+ st.write("Query Image: ")
90
+ st.image(photos[size-1])
91
+ features = extract_features(photos, resnet_model)
92
+
93
+ features_array = np.array(list(features.values()))
94
+
95
+ nn_model = NearestNeighbors(n_neighbors=11, metric='euclidean')
96
+ nn_model.fit(features_array)
97
+
98
+ query_image_path = photos[size-1]
99
+ query_image_feature = features[query_image_path].reshape(1, -1)
100
+ distances, indices = nn_model.kneighbors(query_image_feature)
101
+
102
+
103
+ st.write("Similar Images:")
104
+ for i in range(1,11):
105
+ similar_image_path = photos[indices[0][i]]
106
+ similar_image_distance = distances[0][i]
107
+ st.write("Similar Image #{}: Distance: {}".format(i, similar_image_distance))
108
+ st.image(similar_image_path)
109
+ #else:
110
+ # size=len(photos)
111
+ # st.write(size)
112
+ # st.image(photos[size-1])
113
+ # features = extract_features(photos, resnet_model)
114
+
115
+
116
+
117
+
118
+