# print(result) # # retrieve by gabor # method = Gabor() # samples = method.make_samples(db) # query = samples[query_idx] # _, result = infer(query, samples=samples, depth=depth, d_type=d_type) # print(result) # # retrieve by HOG # method = HOG() # samples = method.make_samples(db) # query = samples[query_idx] # _, result = infer(query, samples=samples, depth=depth, d_type=d_type) # print(result) # # retrieve by VGG # method = VGGNetFeat() # samples = method.make_samples(db) # query = samples[query_idx] # _, result = infer(query, samples=samples, depth=depth, d_type=d_type) # print(result) # retrieve by resnet method = ResNetFeat() samples = method.make_samples(db) query_idx = 1569 query = samples[query_idx] _, result = infer(query, samples=samples, depth=depth, d_type=d_type) print(result)
class NGT(object): def __init__(self, db, f_class=None, d_type='L1'): self.NGT_dir = 'NGT_{}_{}'.format(f_class,d_type) self.NGT_path = b'' self.fearure = f_class self.SQLdb = SQLite() if f_class == 'daisy': self.f_c = Daisy() self.NGT_path = b'NGT/NGT_daisy_'+d_type.encode() elif f_class == 'edge': self.f_c = Edge() self.NGT_path = b'NGT/NGT_edge_'+d_type.encode() elif f_class == 'hog': self.f_c = HOG() self.NGT_path = b'NGT/NGT_hog_'+d_type.encode() elif f_class == 'vgg': self.f_c = VGGNetFeat() self.NGT_path = b'NGT/NGT_vgg_'+d_type.encode() elif f_class == 'res': self.f_c = ResNetFeat() self.NGT_path = b'NGT/NGT_res_'+d_type.encode() if not os.path.exists(os.path.join(NGT_dir,self.NGT_dir)): samples = self.f_c.make_samples(db, verbose=False) dim = 0 try: dim = samples[0]['hist'].shape[0] except: pass images= [] objects = [] for i, row in enumerate(samples): vector = row['hist'] link = row['img'] lable = row['cls'] data = {'index':i,'link':link,'lable':lable} images.append(data) objects.append(vector) self.SQLdb.updateMuti(f_class,images) # cPickle.dump(images, open(os.path.join(NGT_dir, sample_cache), "wb", True)) ngtpy.create(path=self.NGT_path, dimension=dim, distance_type=d_type) self.index = ngtpy.Index(self.NGT_path) self.index.batch_insert(objects) self.index.save() self.index = ngtpy.Index(self.NGT_path) def search (self, link ,depth=5): query=self.f_c.get_featInput(link) r = self.index.search(query, depth)# result[index,square] results = [] for item in r : id = item[0] results.append(self.SQLdb.select(self.fearure,id)) return results def add (self, objects): index = self.index ids = index.insert(objects) index.build_index() index.save() index.close() return ids def remove (self, id): index = self.index index.remove(id) index.save() index.close() return 0