Skip to content

Commit bb82dfb

Browse files
committed
Upload
1 parent 0519b90 commit bb82dfb

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
VGG_Model = load_model('vgg_4096.h5',compile=False)
2+
3+
def pre_process_data(the_path,model):
4+
images_features = []
5+
img_array = []
6+
Rgb_Colors = []
7+
for path, _, files in os.walk(the_path):
8+
for file in files:
9+
img_path = path + str('\\') + file
10+
img = cv2.imread(img_path)
11+
img = cv2.resize(img,(224,224))
12+
gimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13+
img_data = img
14+
img_array.append(gimg)
15+
Rgb_Colors.append(img_data)
16+
img_data = np.expand_dims(img_data, axis=0)
17+
img_data = preprocess_input(img_data)
18+
19+
images_features.append(model.predict(img_data))
20+
21+
return img_array,images_features,Rgb_Colors
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def process_query_image(img_path,images_features,img_array,Rgb_Colors,VGG_Model): # lut
2+
windowsize_r = 28
3+
windowsize_c = 28
4+
yy = np.array(images_features)
5+
yy = yy.reshape((len(images_features),4096))
6+
7+
img = cv2.imread(img_path)
8+
img = cv2.resize(img, (224,224))
9+
gimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
10+
11+
img_data = img
12+
new = []
13+
test_image = gimg
14+
test_image = test_image.reshape(224,224,)
15+
# Histogram of Query image
16+
for r in range(0,test_image.shape[0] - windowsize_r, windowsize_r):
17+
for c in range(0,test_image.shape[1] - windowsize_c, windowsize_c):
18+
window = test_image[r:r+windowsize_r,c:c+windowsize_c]
19+
new.append(cv2.calcHist(window,[0,1,2],None,[8,8,8],[0, 256, 0, 256, 0, 256]))
20+
21+
22+
img_data = np.expand_dims(img_data, axis=0)
23+
gimg = np.expand_dims(gimg, axis=0)
24+
25+
Rgb = np.append(Rgb_Colors,img_data, axis=0)
26+
the_images = np.append(img_array, gimg, axis=0)
27+
28+
img_data = preprocess_input(img_data)
29+
30+
# Extract Features Query
31+
new_im_pred = VGG_Model.predict(img_data)
32+
preds1 = np.append(yy, new_im_pred, axis=0)
33+
return preds1,new,the_images,Rgb

Get_Similar_Images/Display Results.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def display_results_after_LCH(the_images,results,nearest_neighbors):
2+
imgg = the_images[results[0][1]]
3+
plt.title('Query Image')
4+
plt.imshow(cv2.cvtColor(imgg, cv2.COLOR_BGR2RGB))
5+
fig = plt.figure("Re")
6+
fig.set_figheight(25)
7+
fig.set_figwidth(25)
8+
k=1
9+
for i in range(1,5):
10+
imgg = the_images[results[i][1]]
11+
imgg = imgg.reshape(224,224,3)
12+
ax = fig.add_subplot(1, 6, k)
13+
plt.imshow(cv2.cvtColor(imgg, cv2.COLOR_BGR2RGB))
14+
plt.axis("off")
15+
k=k+1
16+
17+
def display_results_before_LCH(the_images,results,nearest_neighbors):
18+
imgg = the_images[nearest_neighbors[0]]
19+
plt.title('Query Image')
20+
plt.imshow(cv2.cvtColor(imgg, cv2.COLOR_BGR2RGB))
21+
zfig = plt.figure("Re")
22+
zfig.set_figheight(25)
23+
zfig.set_figwidth(25)
24+
k=1
25+
for i in range(1,5):
26+
imgg = the_images[nearest_neighbors[i]]
27+
imgg = imgg.reshape(224,224,3)
28+
ax = zfig.add_subplot(1, 6, k)
29+
plt.imshow(cv2.cvtColor(imgg, cv2.COLOR_BGR2RGB))
30+
plt.axis("off")
31+
k=k+1
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def get_nearest_neighbor_and_similarity(preds1, K):
2+
dims = 4096
3+
n_nearest_neighbors = K+1
4+
# Specify no of trees
5+
trees = 1000
6+
file_index_to_file_vector = {}
7+
8+
# build ann index
9+
t = AnnoyIndex(dims)
10+
for i in range(preds1.shape[0]):
11+
12+
file_vector = preds1[i]
13+
file_index_to_file_vector[i] = file_vector
14+
t.add_item(i, file_vector)
15+
# Build trees
16+
t.build(trees)
17+
18+
# Get NN
19+
for i in range(preds1.shape[0]):
20+
master_vector = file_index_to_file_vector[i]
21+
22+
named_nearest_neighbors = []
23+
nearest_neighbors = t.get_nns_by_item(i, n_nearest_neighbors)
24+
25+
return nearest_neighbors
26+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def get_similar_images(nearest_neighbors,images1,query_hist):
2+
histr = []
3+
temp = []
4+
windowsize_r = 28
5+
windowsize_c = 28
6+
for i in nearest_neighbors:
7+
test_image = images1[i]
8+
test_image = test_image.reshape(224,224,)
9+
for r in range(0,test_image.shape[0] - windowsize_r, windowsize_r):
10+
for c in range(0,test_image.shape[1] - windowsize_c, windowsize_c):
11+
window = test_image[r:r+windowsize_r,c:c+windowsize_c]
12+
temp.append(cv2.calcHist(window,[0,1,2],None,[8,8,8],[0, 256, 0, 256, 0, 256]))
13+
histr.append(temp)
14+
temp = []
15+
the_sum = 0
16+
results = {}
17+
print(len(histr[0]))
18+
for y in range(0,5):
19+
the_sum = 0
20+
for u in range(0,49):
21+
f = np.array(query_hist[u])
22+
h = np.array(histr[y][u])
23+
24+
f = f.flatten()
25+
h = h.flatten()
26+
27+
s = dist.euclidean(f,h)
28+
the_sum = the_sum + s
29+
results[nearest_neighbors[y]] = the_sum
30+
results = sorted([(v, k) for (k, v) in results.items()], reverse = False)
31+
print(results)
32+
return results

Runner.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from keras.preprocessing import image
2+
from keras.applications.vgg16 import VGG16
3+
from keras.applications.vgg16 import preprocess_input
4+
import numpy as np
5+
import cv2
6+
import os
7+
import annoy
8+
from annoy import AnnoyIndex
9+
from keras.models import load_model
10+
from scipy.spatial import distance as dist
11+
import matplotlib.pyplot as plt
12+
13+
14+
from Data_preprocessing import *
15+
16+
from Get_Similar_Images import *
17+
18+
19+
img_array,images_features,Rgb_colors = pre_process_data('C:\\Users\\khali\\101_ObjectCategories\\random',VGG_Model)
20+
preds1,new,the_images,Rgb = process_query_image('C:\\Users\\khali\\cam.jpg',images_features,img_array,Rgb_colors,VGG_Model)
21+
nearest_neighbors = get_nearest_neighbor_and_similarity(preds1,15)
22+
results = get_similar_images(nearest_neighbors,the_images,new)
23+
display_results_before_LCH(Rgb,results,nearest_neighbors)
24+
display_results_after_LCH(Rgb,results,nearest_neighbors)

0 commit comments

Comments
 (0)