Spaces:
Running
Running
Add new API endpoint for vector lookups
Browse files
app.py
CHANGED
@@ -28,7 +28,19 @@ with pyzipper.AESZipFile('persons.zip') as zf:
|
|
28 |
PERFORMER_DB = json.loads(zf.read('performers.json'))
|
29 |
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
image_array = np.array(image)
|
33 |
|
34 |
img = functions.preprocess_face(
|
@@ -41,8 +53,36 @@ def predict(image, threshold=20.0, results=3):
|
|
41 |
|
42 |
img = functions.normalize_input(img, normalization="Facenet2018")
|
43 |
face = model.predict(img)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
ids, distances = index.get_nns_by_vector(
|
45 |
-
|
46 |
)
|
47 |
persons = {}
|
48 |
for p, distance in zip(ids, distances):
|
@@ -91,8 +131,8 @@ def face_distance_to_conf(face_distance, face_match_threshold=20.0):
|
|
91 |
return linear_val + ((1.0 - linear_val) * math.pow((linear_val - 0.5) * 2, 0.2))
|
92 |
|
93 |
|
94 |
-
gr.Interface(
|
95 |
-
fn=
|
96 |
inputs=[
|
97 |
gr.components.Image(),
|
98 |
gr.components.Slider(label="threshold",minimum=0.0, maximum=30.0, value=20.0),
|
@@ -101,4 +141,18 @@ gr.Interface(
|
|
101 |
outputs=gr.outputs.JSON(label=""),
|
102 |
title="Who is in the photo?",
|
103 |
description="Upload an image of a person and we'll tell you who it is.",
|
104 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
PERFORMER_DB = json.loads(zf.read('performers.json'))
|
29 |
|
30 |
|
31 |
+
## Prediction functions
|
32 |
+
|
33 |
+
|
34 |
+
def image_search_performer(image, threshold=20.0, results=3):
|
35 |
+
"""Search for a performer in an image
|
36 |
+
|
37 |
+
Returns a list of performers with at least following keys:
|
38 |
+
- id: the performer's id
|
39 |
+
- distance: the distance between the face in the image and the performer's face
|
40 |
+
- confidence: a confidence score between 0 and 100
|
41 |
+
- hits: the number of times the performer was found in our database
|
42 |
+
"""
|
43 |
+
|
44 |
image_array = np.array(image)
|
45 |
|
46 |
img = functions.preprocess_face(
|
|
|
53 |
|
54 |
img = functions.normalize_input(img, normalization="Facenet2018")
|
55 |
face = model.predict(img)[0]
|
56 |
+
return search_performer(face, threshold, results)
|
57 |
+
|
58 |
+
|
59 |
+
def vector_search_performer(vector_json, threshold=20.0, results=3):
|
60 |
+
"""Search for a performer from a vector
|
61 |
+
|
62 |
+
The vector should be created with Deepface and should be a 512 vector.
|
63 |
+
|
64 |
+
For best results use the following settings:
|
65 |
+
- detector_backend: retinaface
|
66 |
+
- model: Facenet512
|
67 |
+
- normalization: Facenet2018
|
68 |
+
|
69 |
+
Returns a list of performers with at least following keys:
|
70 |
+
- id: the performer's id
|
71 |
+
- distance: the distance between the face in the image and the performer's face
|
72 |
+
- confidence: a confidence score between 0 and 100
|
73 |
+
- hits: the number of times the performer was found in our database
|
74 |
+
"""
|
75 |
+
|
76 |
+
vector = np.array(json.loads(vector_json))
|
77 |
+
return search_performer(vector, threshold, results)
|
78 |
+
|
79 |
+
|
80 |
+
def search_performer(vector, threshold=20.0, results=3):
|
81 |
+
threshold = threshold or 20.0
|
82 |
+
results = results or 3
|
83 |
+
|
84 |
ids, distances = index.get_nns_by_vector(
|
85 |
+
vector, 50, search_k=10000, include_distances=True
|
86 |
)
|
87 |
persons = {}
|
88 |
for p, distance in zip(ids, distances):
|
|
|
131 |
return linear_val + ((1.0 - linear_val) * math.pow((linear_val - 0.5) * 2, 0.2))
|
132 |
|
133 |
|
134 |
+
image_search = gr.Interface(
|
135 |
+
fn=image_search_performer,
|
136 |
inputs=[
|
137 |
gr.components.Image(),
|
138 |
gr.components.Slider(label="threshold",minimum=0.0, maximum=30.0, value=20.0),
|
|
|
141 |
outputs=gr.outputs.JSON(label=""),
|
142 |
title="Who is in the photo?",
|
143 |
description="Upload an image of a person and we'll tell you who it is.",
|
144 |
+
)
|
145 |
+
|
146 |
+
vector_search = gr.Interface(
|
147 |
+
fn=vector_search_performer,
|
148 |
+
inputs=[
|
149 |
+
gr.components.Textbox(),
|
150 |
+
gr.components.Slider(label="threshold",minimum=0.0, maximum=30.0, value=20.0),
|
151 |
+
gr.components.Slider(label="results", minimum=0, maximum=50, value=3, step=1),
|
152 |
+
],
|
153 |
+
outputs=gr.outputs.JSON(label=""),
|
154 |
+
title="Who is in the photo?",
|
155 |
+
description="512 vector created with deepface of a person and we'll tell you who it is.",
|
156 |
+
)
|
157 |
+
|
158 |
+
gr.TabbedInterface([image_search, vector_search]).launch(enable_queue=True, server_name="0.0.0.0")
|