How to use the esper.face_embeddings.knn function in esper

To help you get started, we’ve selected a few esper examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scanner-research / esper-tv / app / esper / queries / other_queries.py View on Github external
3310952, 1168204, 3062342, 1010748, 1275607, 2190958, 2779945, 415610, 1744917,
        5210138, 3288162, 5137166, 4169061, 3774070, 2595170, 382055, 2365443, 712023,
        5214225, 178251, 1039121, 5336597, 525714, 4522167, 3613622, 5161408, 2091095,
        741985, 521, 2589969, 5120596, 284825, 3361576, 1684384, 4437468, 5214225,
        178251]

    from esper.face_embeddings import knn

    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(ids=target_face_ids, max_threshold=max_thresh)

    face_sims_by_bucket = {}
    idx = 0
    max_idx = len(face_sims)
    for t in frange(min_thresh, max_thresh, increment):
        start_idx = idx
        cur_thresh = t + increment
        while idx < max_idx and face_sims[idx][1] < cur_thresh:
            idx += 1
        face_sims_by_bucket[t] = face_sims[start_idx:idx]

    results_by_bucket = {}
    for t in frange(min_thresh, max_thresh, increment):
        face_ids = [x for x, _ in face_sims_by_bucket[t]]
        if len(face_ids) != 0:
            faces = face_qs.filter(
github scanner-research / esper-tv / app / esper / queries / other_queries.py View on Github external
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
github scanner-research / esper-tv / app / esper / identity.py View on Github external
def face_search_by_embeddings(embs, increment=0.05, max_thresh=1.2,
                              exclude_labeled=False):
    min_thresh = 0.
    face_sims = knn(targets=embs, max_threshold=max_thresh)

    face_ids_to_score = {}
    results_by_bucket = {}

    for face_id, score in face_sims:
        if score >= min_thresh and score < max_thresh:
            face_ids_to_score[face_id] = score
            t = min_thresh + int((score - min_thresh) / increment) * increment
            bucket = (t, t + increment)
            if bucket not in results_by_bucket:
                results_by_bucket[bucket] = []
            results_by_bucket[bucket].append(face_id)

    if len(results_by_bucket) == 0:
        raise Exception('No results to show')
github scanner-research / esper-tv / app / esper / queries.py View on Github external
3310952, 1168204, 3062342, 1010748, 1275607, 2190958, 2779945, 415610, 1744917,
        5210138, 3288162, 5137166, 4169061, 3774070, 2595170, 382055, 2365443, 712023,
        5214225, 178251, 1039121, 5336597, 525714, 4522167, 3613622, 5161408, 2091095,
        741985, 521, 2589969, 5120596, 284825, 3361576, 1684384, 4437468, 5214225,
        178251]

    from esper.face_embeddings import knn
    
    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(ids=target_face_ids, max_threshold=max_thresh)

    face_sims_by_bucket = {}
    idx = 0
    max_idx = len(face_sims)
    for t in frange(min_thresh, max_thresh, increment):
        start_idx = idx
        cur_thresh = t + increment
        while idx < max_idx and face_sims[idx][1] < cur_thresh:
            idx += 1
        face_sims_by_bucket[t] = face_sims[start_idx:idx]

    results_by_bucket = {}
    for t in frange(min_thresh, max_thresh, increment):
        face_ids = [x for x, _ in face_sims_by_bucket[t]]
        if len(face_ids) != 0:
            faces = face_qs.filter(
github scanner-research / esper-tv / app / esper / queries.py View on Github external
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
github scanner-research / esper-tv / app / esper / queries / other_queries.py View on Github external
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
        return qs_to_result(face_qs.filter(id__in=kept_ids, shot__in_commercial=False),
github scanner-research / esper-tv / app / esper / queries.py View on Github external
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))
github scanner-research / esper-tv / app / esper / queries.py View on Github external
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))
            ).distinct('frame__video')
            if faces.count() == 0:
                continue
            results = qs_to_result(faces, limit=max_results_per_group, custom_order_by_id=face_ids)
            results_by_bucket[(t, t + increment, len(face_ids))] = results

    if len(results_by_bucket) == 0:
        raise Exception('No results to show')
github scanner-research / esper-tv / app / esper / queries / other_queries.py View on Github external
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))
            ).distinct('frame__video')
            if faces.count() == 0:
                continue
            results = qs_to_result(faces, limit=max_results_per_group, custom_order_by_id=face_ids)
            results_by_bucket[(t, t + increment, len(face_ids))] = results

    if len(results_by_bucket) == 0:
        raise Exception('No results to show')