3 from scipy.spatial
import distance
7 """Nearest Neighbor matching of description vectors
14 dim (int, optional): Global descriptor size. Defaults to None.
21 self.
datadata = np.zeros((1000, dim), dtype=
'float32')
24 """Add item to the matching list
27 vector (np.array): descriptor
28 item: identification info (e.g., int)
30 assert vector.ndim == 1
31 if self.
nn >= len(self.
datadata):
32 if self.
dimdim
is None:
33 self.
dimdim = len(vector)
34 self.
datadata = np.zeros((1000, self.
dimdim), dtype=
'float32')
36 self.
datadata.resize((2 * len(self.
datadata), self.
dimdim),
38 self.
itemsitems[self.
nn] = item
39 self.
datadata[self.
nn] = vector
43 """Search for nearest neighbors
46 query (np.array): descriptor to match
47 k (int): number of best matches to return
50 list(int, np.array): best matches
52 if len(self.
datadata) == 0:
55 similarities = np.zeros(self.
nn)
57 for i
in range(self.
nn):
58 similarities[i] = 1 - distance.cosine(query, self.
datadata[i,:].squeeze())
60 ns = np.argsort(similarities)[::-1][:k]
61 return [self.
itemsitems[n]
for n
in ns], similarities[ns]
64 """Search for the nearest neighbor
67 query (np.array): descriptor to match
70 int, np.array: best match
72 if len(self.
datadata) == 0:
75 items, similarities = self.
searchsearch(query, 1)
76 return items[0], similarities[0]
def __init__(self, dim=None)
def add_item(self, vector, item)
def search(self, query, k)
def search_best(self, query)