File size: 972 Bytes
5d3f355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from annoy import AnnoyIndex
from face_recognition import FacenetPytorch
import numpy as np 
from numpy.linalg import norm

class PhotoSearch:
    def __init__ (self, tree_path, data_path, index_size = 512):
        self.tree_path = tree_path
        self.data_path = data_path

        self.t = AnnoyIndex(index_size, 'angular')
        self.t.load(tree_path)

        data = np.load(data_path, allow_pickle=True)
        self.face_grive_id = data.f.id
        self.extractor = FacenetPytorch()

    def search(self, image):
        face_emb = self.extractor.get_embedding(image)
        nns = self.t.get_nns_by_vector(face_emb, 20)
        result = []
        for n in nns:
            face_origin = self.t.get_item_vector(n)
            print(self.face_grive_id[n])
            if self.cosine(face_emb, face_origin) < 0.7: continue

            result.append('get..')

        return result
    
    def cosine(self, x, y):
        return np.dot(x,y)/(norm(x)*norm(y))