-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search Engine.py
96 lines (53 loc) · 1.89 KB
/
Search Engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 20 08:03:07 2021
@author: tech crusaders
"""
import cv2 as cv
import matplotlib.image as img
import matplotlib.pyplot as plt
import csv
import numpy as np
#histogram feature vector
def rgb_histo(image):
histo= cv.calcHist([image],[0],None,[256],[0,256])
fv=np.array(histo)
for i in range(1,3):
histo= cv.calcHist([image],[i],None,[256],[0,256])
fv=np.append(fv,histo,0)
return fv
#upload image in theproject folder before reading
print("Input name of the image to be searched: ")
name=input()+'.jpg'
q_img= img.imread(name)
plt.imshow(q_img)
plt.show()
q_vec=rgb_histo(q_img)
plt.plot(q_vec)
plt.show()
print(q_vec[0])
with open("Database.csv",'r') as csvfile:
sim_im={}
csvreader= csv.reader(csvfile)
c_vec= next(csvreader)
c_vec=[float(s[1:-1]) for s in c_vec]
c_vec=np.array(c_vec)
c_vec=c_vec.astype(np.float32)
c_vec.shape=(768,1)
p = cv.compareHist(c_vec, q_vec ,cv.HISTCMP_BHATTACHARYYA)
q = cv.compareHist(q_vec, c_vec ,cv.HISTCMP_CORREL)
if p<0.23 or q>0.88:
sim_im[q]=c_vec
for row in csvreader:
c_vec=row
c_vec=[float(s[1:-1]) for s in c_vec]
c_vec=np.array(c_vec)
c_vec=c_vec.astype(np.float32)
c_vec.shape=(768,1)
p = cv.compareHist(c_vec, q_vec ,cv.HISTCMP_BHATTACHARYYA)
q = cv.compareHist(q_vec, c_vec ,cv.HISTCMP_CORREL)
if p<0.238 or q>0.88:
sim_im[q]=c_vec
csvfile.close()
#dictionary of similar images
print(sim_im.keys())