File size: 8,703 Bytes
9e1ec94
7fbe9b9
520449a
9e1ec94
fae5be7
61c9bd4
a3296a3
 
9e1ec94
2b7c418
9e1ec94
 
 
 
 
ece198d
cdddf52
9e1ec94
 
 
 
 
 
 
 
40a4317
 
9e1ec94
 
b2d74ab
 
 
 
 
 
 
 
 
 
 
 
 
9e1ec94
aad61cc
cc08c34
b2d74ab
 
 
61c9bd4
 
 
 
 
520449a
7fbe9b9
520449a
 
 
 
 
 
 
 
7fbe9b9
61c9bd4
 
 
 
 
7fbe9b9
 
 
61c9bd4
 
7fbe9b9
61c9bd4
 
 
 
 
 
 
b2d74ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55bf202
 
 
9e1ec94
55bf202
 
9e1ec94
 
 
fae5be7
9e1ec94
 
 
 
55bf202
 
9e1ec94
 
 
 
 
 
 
55bf202
9e1ec94
 
 
fae5be7
 
4e44849
fae5be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3296a3
 
 
 
 
 
 
 
7fbe9b9
a3296a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2d74ab
 
9e1ec94
916120c
 
 
9e1ec94
aa25995
9e1ec94
 
b2d74ab
 
61c9bd4
 
 
916120c
 
 
61c9bd4
aa25995
61c9bd4
 
 
 
b2d74ab
 
 
916120c
 
 
b2d74ab
aa25995
b2d74ab
 
 
 
a3296a3
 
 
 
 
 
 
 
 
1bfb960
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
import io
import time
import json
import math
import base64
from uuid import uuid4
from PIL import Image as PILImage

os.environ["DEEPFACE_HOME"] = "."

import pyzipper
import numpy as np
import gradio as gr
from annoy import AnnoyIndex

from deepface import DeepFace


index = AnnoyIndex(512, "euclidean")
index.load(f"face.db")

ANNOY_INDEX = json.load(open(f"face.json"))

with pyzipper.AESZipFile('persons.zip') as zf:
    password = os.getenv("VISAGE_KEY","").encode('ascii')
    zf.setpassword(password)
    PERFORMER_DB = json.loads(zf.read('performers.json'))

## Prediction functions


def image_search_performer(image, threshold=20.0, results=3):
    """Search for a performer in an image

    Returns a list of performers with at least following keys:
    - id: the performer's id
    - distance: the distance between the face in the image and the performer's face
    - confidence: a confidence score between 0 and 100
    - hits: the number of times the performer was found in our database
    """
    
    image_array = np.array(image)

    face = DeepFace.represent(img_path = image_array, detector_backend='retinaface', model_name='Facenet512', normalization="Facenet2018")[0]['embedding']
    return search_performer(face, threshold, results)



def image_search_performers(image, threshold=20.0, results=3):
    image_array = np.array(image)

    response = []
    
    t = time.time()
    try:
        faces = DeepFace.represent(img_path = image_array, detector_backend='retinaface', model_name='Facenet512', normalization="Facenet2018")
        # faces = DeepFace.represent(img_path = image_array, detector_backend='yolov8', model_name='Facenet512', normalization="Facenet2018")
        # faces = DeepFace.represent(img_path = image_array, detector_backend='mtcnn', model_name='Facenet512', normalization="Facenet2018")
    except ValueError as e:
        print(e)
        raise gr.Error("No faces found in the image")
    
    print(f"Time to find faces: {time.time() - t}")
    for face in faces:
        embedding = face['embedding']
        area = face['facial_area']
        confidence = face['face_confidence']
        cimage = image.crop((area['x'], area['y'], area['x'] + area['w'], area['y'] + area['h']))
        buf = io.BytesIO()
        cimage.save(buf, format='JPEG')
        im_b64 = base64.b64encode(buf.getvalue()).decode('ascii')

        response.append({
            'image': im_b64,
            'confidence': confidence,
            'performers': search_performer(embedding, threshold, results)
        })
    
    return response


def vector_search_performer(vector_json, threshold=20.0, results=3):
    """Search for a performer from a vector

    The vector should be created with Deepface and should be a 512 vector.

    For best results use the following settings:
    - detector_backend: retinaface
    - model: Facenet512
    - normalization: Facenet2018

    Returns a list of performers with at least following keys:
    - id: the performer's id
    - distance: the distance between the face in the image and the performer's face
    - confidence: a confidence score between 0 and 100
    - hits: the number of times the performer was found in our database
    """

    vector = np.array(json.loads(vector_json))
    return search_performer(vector, threshold, results)
    

def search_performer(vector, threshold=20.0, results=3):
    threshold = threshold or 20.0
    results = results or 3
    
    ids, distances = index.get_nns_by_vector(
        vector, 50, search_k=100000, include_distances=True
    )
    persons = {}
    for p, distance in zip(ids, distances):
        id = ANNOY_INDEX[p]
        if id in persons:
            persons[id]["hits"] += 1
            persons[id]["distance"] -= 0.5
            persons[id]["confidence"] = normalize_confidence_from_distance(persons[id]["distance"], threshold)
            continue

        persons[id] = {
            "id": id,
            "distance": round(distance, 2),
            "confidence": normalize_confidence_from_distance(distance, threshold),
            "hits": 1,
        }

        if id in PERFORMER_DB:
            persons[id].update(PERFORMER_DB.get(id))

    persons = sorted(persons.values(), key=lambda x: x["distance"])
    # persons = [p for p in persons if p["distance"] < threshold]
    return persons[:results]


def normalize_confidence_from_distance(distance, threshold=20.0):
    """Normalize confidence to 0-100 scale"""
    confidence = face_distance_to_conf(distance,threshold)
    return int(((confidence - 0.0) / (1.0 - 0.0)) * (100.0 - 0.0) + 0.0)


def face_distance_to_conf(face_distance, face_match_threshold=20.0):
    """Using a face distance, calculate a similarity confidence value"""
    if face_distance > face_match_threshold:
        # The face is far away, so give it a low confidence
        range = (1.0 - face_match_threshold)
        linear_val = (1.0 - face_distance) / (range * 2.0)
        return linear_val
    else:
        # The face is close, so give it a high confidence
        range = face_match_threshold
        linear_val = 1.0 - (face_distance / (range * 2.0))
        # But adjust this value by a curve so that we don't get a linear
        # transition from close to far. We want it to be more confident
        # the closer it is.
        return linear_val + ((1.0 - linear_val) * math.pow((linear_val - 0.5) * 2, 0.2))


def find_faces_in_sprite(image, vtt):
    vtt = base64.b64decode(vtt.replace("data:text/vtt;base64,", ""))
    sprite = PILImage.fromarray(image)

    results = []
    for i, (left, top, right, bottom, time_seconds) in enumerate(getVTToffsets(vtt)):
        cut_frame = sprite.crop((left, top, left + right, top + bottom))
        faces = DeepFace.extract_faces(np.asarray(cut_frame), detector_backend="mediapipe", enforce_detection=False, align=False)
        faces = [face for face in faces if face['confidence'] > 0.6]
        if faces:
            size = faces[0]['facial_area']['w'] * faces[0]['facial_area']['h']
            data = {'id': str(uuid4()), "offset": (left, top, right, bottom), "frame": i, "time": time_seconds, 'size': size}
            results.append(data)

    return results


def getVTToffsets(vtt):
    time_seconds = 0
    left = top = right = bottom = None
    for line in vtt.decode("utf-8").split("\n"):
        line = line.strip()

        if "-->" in line:
            # grab the start time
            # 00:00:00.000 --> 00:00:41.000
            start = line.split("-->")[0].strip().split(":")
            # convert to seconds
            time_seconds = (
                int(start[0]) * 3600
                + int(start[1]) * 60
                + float(start[2])
            )
            left = top = right = bottom = None
        elif "xywh=" in line:
            left, top, right, bottom = line.split("xywh=")[-1].split(",")
            left, top, right, bottom = (
                int(left),
                int(top),
                int(right),
                int(bottom),
            )
        else:
            continue

        if not left:
            continue

        yield left, top, right, bottom, time_seconds


image_search = gr.Interface(
    fn=image_search_performer,
    inputs=[
        gr.Image(),
        gr.Slider(label="threshold",minimum=0.0, maximum=30.0, value=20.0),
        gr.Slider(label="results", minimum=0, maximum=50, value=3, step=1),
    ],
    outputs=gr.JSON(label=""),
    title="Who is in the photo?",
    description="Upload an image of a person and we'll tell you who it is.",
)

image_search_multiple = gr.Interface(
    fn=image_search_performers,
    inputs=[
        gr.Image(type="pil"),
        gr.Slider(label="threshold",minimum=0.0, maximum=30.0, value=20.0),
        gr.Slider(label="results", minimum=0, maximum=50, value=3, step=1),
    ],
    outputs=gr.JSON(label=""),
    title="Who is in the photo?",
    description="Upload an image of a person(s) and we'll tell you who it is.",
)

vector_search = gr.Interface(
    fn=vector_search_performer,
    inputs=[
        gr.Textbox(),
        gr.Slider(label="threshold",minimum=0.0, maximum=30.0, value=20.0),
        gr.Slider(label="results", minimum=0, maximum=50, value=3, step=1),
    ],
    outputs=gr.JSON(label=""),
    title="Who is in the photo?",
    description="512 vector created with deepface of a person and we'll tell you who it is.",
)

faces_in_sprite = gr.Interface(
    fn=find_faces_in_sprite,
    inputs=[
        gr.Image(),
        gr.Textbox(label="VTT file")
    ],
    outputs=gr.JSON(label=""),
)

gr.TabbedInterface([image_search, image_search_multiple, vector_search, faces_in_sprite]).queue().launch(server_name="0.0.0.0")