Prathamesh1420 commited on
Commit
e05b641
·
verified ·
1 Parent(s): 9f78594

Delete test.py

Browse files
Files changed (1) hide show
  1. test.py +0 -48
test.py DELETED
@@ -1,48 +0,0 @@
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
- # Load the precomputed feature vectors and filenames from pickle files
12
- feature_list = np.array(pickle.load(open('embeddings.pkl', 'rb')))
13
- filenames = pickle.load(open('filenames.pkl', 'rb'))
14
-
15
- # Load ResNet50 model without the top layer for feature extraction
16
- model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
17
- model.trainable = False
18
-
19
- # Create a Sequential model with ResNet50 and GlobalMaxPooling2D layers
20
- model = tensorflow.keras.Sequential([
21
- model,
22
- GlobalMaxPooling2D()
23
- ])
24
-
25
- # Load and preprocess the query image
26
- img = image.load_img('sample/khade.jpg', target_size=(224, 224))
27
- img_array = image.img_to_array(img)
28
- expanded_img_array = np.expand_dims(img_array, axis=0)
29
- preprocessed_img = preprocess_input(expanded_img_array)
30
-
31
- # Extract features from the query image and normalize
32
- result = model.predict(preprocessed_img).flatten()
33
- normalized_result = result / norm(result)
34
-
35
- # Initialize NearestNeighbors model and fit with the feature vectors
36
- neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
37
- neighbors.fit(feature_list)
38
-
39
- # Find the nearest neighbors (excluding itself)
40
- distances, indices = neighbors.kneighbors([normalized_result])
41
-
42
- print(indices) # Print the indices of nearest neighbors
43
-
44
- # Display the nearest neighbor images
45
- for file in indices[0][0:5]:
46
- temp_img = cv2.imread(filenames[file])
47
- cv2.imshow('output', cv2.resize(temp_img, (312, 312)))
48
- cv2.waitKey(0)