Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def face_search():
from esper.embed_google_images import name_to_embedding
from esper.face_embeddings import knn
emb = name_to_embedding('Wolf Blitzer')
face_ids = [x for x, _ in knn(targets=[emb], max_threshold=0.4)][::10]
return qs_to_result(
Face.objects.filter(id__in=face_ids), custom_order_by_id=face_ids, limit=len(face_ids))
def exclude_faces(face_ids, exclude_ids, exclude_thresh):
excluded_face_ids = set()
for exclude_id in exclude_ids:
excluded_face_ids.update([x for x, _ in knn(ids=[exclude_id], max_threshold=exclude_thresh)])
face_ids = set(face_ids)
return face_ids - excluded_face_ids, face_ids & excluded_face_ids
# Some params
exclude_labeled = False
show_excluded = False
face_qs = UnlabeledFace.objects if exclude_labeled else Face.objects
name = 'Wolf Blitzer'
emb = name_to_embedding(name)
face_ids = [x for x, _ in knn(features=emb, max_threshold=0.6)]
kept_ids, excluded_ids = exclude_faces(
face_ids,
[1634585, 531076, 3273872, 2586010, 921211, 3176879, 3344886, 3660089, 249499, 2236580],
0.4)
if show_excluded:
# Show the furthest faces that we kept and the faces that were excluded
kept_results = qs_to_result(face_qs.filter(id__in=kept_ids, shot__in_commercial=False),
custom_order_by_id=face_ids[::-1])
excluded_results = qs_to_result(face_qs.filter(id__in=excluded_ids, shot__in_commercial=False))
return group_results([('excluded', excluded_results), (name, kept_results)])
else:
# Show all of the faces that were kept
def face_search():
from esper.embed_google_images import name_to_embedding
from esper.face_embeddings import knn
emb = name_to_embedding('Wolf Blitzer')
face_ids = [x for x, _ in knn(targets=[emb], max_threshold=0.4)][::10]
return qs_to_result(
Face.objects.filter(id__in=face_ids), custom_order_by_id=face_ids, limit=len(face_ids))
def exclude_faces(face_ids, exclude_ids, exclude_thresh):
excluded_face_ids = set()
for exclude_id in exclude_ids:
excluded_face_ids.update([x for x, _ in knn(ids=[exclude_id], max_threshold=exclude_thresh)])
face_ids = set(face_ids)
return face_ids - excluded_face_ids, face_ids & excluded_face_ids
# Some params
exclude_labeled = False
show_excluded = False
face_qs = UnlabeledFace.objects if exclude_labeled else Face.objects
name = 'Wolf Blitzer'
emb = name_to_embedding(name)
face_ids = [x for x, _ in knn(features=emb, max_threshold=0.6)]
kept_ids, excluded_ids = exclude_faces(
face_ids,
[1634585, 531076, 3273872, 2586010, 921211, 3176879, 3344886, 3660089, 249499, 2236580],
0.4)
if show_excluded:
# Show the furthest faces that we kept and the faces that were excluded
kept_results = qs_to_result(face_qs.filter(id__in=kept_ids, shot__in_commercial=False),
custom_order_by_id=face_ids[::-1])
excluded_results = qs_to_result(face_qs.filter(id__in=excluded_ids, shot__in_commercial=False))
return group_results([('excluded', excluded_results), (name, kept_results)])
else:
# Show all of the faces that were kept
def groups_of_faces_by_distance_threshold():
from esper.embed_google_images import name_to_embedding
from esper.face_embeddings import knn
emb = name_to_embedding('Wolf Blitzer')
increment = 0.05
max_thresh = 1.0
max_results_per_group = 50
exclude_labeled = False
face_qs = UnlabeledFace.objects if exclude_labeled else Face.objects
face_sims = knn(targets=[emb], max_threshold=max_thresh)
results_by_bucket = {}
for t in frange(min_thresh, max_thresh, increment):
face_ids = [x for x, _ in filter(lambda z: z[1] >= t and z[1] < t + increment, face_sims)]
if len(face_ids) != 0:
faces = face_qs.filter(
id__in=random.sample(face_ids, k=min(len(face_ids), max_results_per_group))
def groups_of_faces_by_distance_threshold():
from esper.embed_google_images import name_to_embedding
from esper.face_embeddings import knn
emb = name_to_embedding('Wolf Blitzer')
increment = 0.05
max_thresh = 1.0
max_results_per_group = 50
exclude_labeled = False
face_qs = UnlabeledFace.objects if exclude_labeled else Face.objects
face_sims = knn(targets=[emb], max_threshold=max_thresh)
results_by_bucket = {}
for t in frange(min_thresh, max_thresh, increment):
face_ids = [x for x, _ in filter(lambda z: z[1] >= t and z[1] < t + increment, face_sims)]
if len(face_ids) != 0:
faces = face_qs.filter(
id__in=random.sample(face_ids, k=min(len(face_ids), max_results_per_group))