赞
踩
分享一个用基于Pytorch和Flask实现的花卉图像检索系统(Flowers-Retrieval),算法采用的是深度哈希,距离度量的方式利用的是汉明距离。以下是检索系统的界面:
以下是实现该系统的匹配代码的关键方法:
- def match(self, uri, top_k=100):
- print(uri)
- uri = uri.replace('\\', '/')
- f = open('Data/search.txt', 'w')
- f.write(uri + ' 0')
- f.close()
- searchloader = load_data('Data/search.txt')
- search_binary = binary_output(searchloader)
- search_binary = search_binary.cpu().numpy()
- search_binary = np.asarray(search_binary, np.int32)
- print(search_binary)
- # tensor([[0., 1., 1., 0., 1., 0., 1., 0., 0., 1., 0., 0., 1., 1., 1., 0., 1., 1.,
- # 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 1.,
- # 1., 1., 0., 1., 1., 0., 0., 0., 1., 1., 1., 1.]])
- # 这里计算与train_binary最相似的一组,查询数据库
- query_result = np.count_nonzero(search_binary != self.binary, axis=1) # don't need to divide binary length
- sort_indices = np.argsort(query_result)
- imlist = [self.imNames[index] for i, index in enumerate(sort_indices[0:top_k])]
- rank_dists = [query_result[index] for i, index in enumerate(sort_indices[0:top_k])]
- # print(imlist)
- print(rank_dists)
- for i in range(len(imlist)):
- # 获取文件路径后把把反斜杠替换为正斜杠
- imlist[i]='Data/Raw/'+imlist[i].replace('\n','')+'.jpg'
- print(imlist)
- return imlist, rank_dists
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。