File size: 1,146 Bytes
d9dc084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# import the necessary packages
import numpy as np
import csv

class Searcher:
	def __init__(self, indexPath):
		#store our index path
		self.indexPath = indexPath

	def search(self, queryFeatures, limit=101):
		#initialize dictionary of results
		results = {}

		#open the index file
		with open(self.indexPath) as f:
			#initialize the CSV reader
			reader = csv.reader(f)

			#loop over the rows in the index
			for row in reader:
				#parse out the image ID and features, then compute the
				#chi-squared dist. b/w the features in our index
				#and our query features
				features = [float(x) for x in row[1:]]
				d = self.chi2_distance(features, queryFeatures)

				#Update dictionsary
				#key is image id, value is similarity
				results[row[0]] = d

			#close reader
			f.close()

		#sort in order of relevance
		results = sorted([(v,k) for (k,v) in results.items()])

		#return our (limited) results
		return results[:limit]

	def chi2_distance(self, histA, histB, eps=1e-10):
		#compute chi-squared distance
		d = 0.5 * np.sum([((a-b)**2)/(a+b+eps)
			for (a,b) in zip(histA, histB)])

		# return the chi-squared distance
		return d