Manasa B Rao commited on
Commit
43eb230
·
unverified ·
1 Parent(s): 2c9f477

Add files via upload

Browse files
Files changed (1) hide show
  1. test.py +43 -0
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)