def compute_distances(cls, inst_id):
global feat_nn
global feat_ids
it = cls.objects.annotate(height=F('face__bbox_y2') - F('face__bbox_y1')).filter(
height__gte=0.1).order_by('id')
if feat_nn is None:
_print('Loading features...')
feats = list(it[::5])
feat_ids = np.array([f.id for f in feats])
feat_vectors = [f.load_features() for f in feats]
X = np.vstack(feat_vectors)
_print('Constructing KNN tree...')
feat_nn = NearestNeighbors().fit(X)
_print('Done!')
# Erase distances from previous computation
prev = list(cls.objects.filter(distto__isnull=False))
for feat in prev:
feat.distto = None
cls.objects.bulk_update(prev)
dists, indices = feat_nn.kneighbors([cls.objects.get(face=inst_id).load_features()], 1000)
for dist, feat_id in zip(dists[0], feat_ids[indices[0]]):
feat = cls.objects.get(id=feat_id)
feat.distto = dist
feat.save()
评论列表
文章目录