5
5
from PIL import Image
6
6
from typing import Union , List , Set , Dict
7
7
8
+ from requests .models import Response
9
+
8
10
## Const
9
11
DEFAULT_API_KEY = ""
10
12
DEFAULT_TIMEOUT = 10 # seconds
23
25
URL_FACE_DETECTION = "/face"
24
26
URL_FACE_REGISTER = "/face/register"
25
27
URL_FACE_RECOGNIZE = "/face/recognize"
28
+ URL_FACE_LIST = "/face/list"
26
29
URL_SCENE_RECOGNIZE = "/scene"
27
30
28
31
@@ -132,6 +135,21 @@ def process_image(
132
135
)
133
136
134
137
138
+ def get_stored_faces (url , api_key , timeout ) -> List :
139
+ """Posts a request and get the stored faces as a list"""
140
+ try :
141
+ data = requests .post (url , timeout = timeout , data = {"api_key" : api_key })
142
+ except requests .exceptions .Timeout :
143
+ raise DeepstackException (
144
+ f"Timeout connecting to Deepstack, the current timeout is { timeout } seconds, try increasing this value"
145
+ )
146
+ except requests .exceptions .ConnectionError or requests .exceptions .MissingSchema as exc :
147
+ raise DeepstackException (
148
+ f"Deepstack connection error, check your IP and port: { exc } "
149
+ )
150
+ return data .json ()
151
+
152
+
135
153
class DeepstackVision :
136
154
"""Base class for Deepstack vision."""
137
155
@@ -145,6 +163,7 @@ def __init__(
145
163
url_detect : str = "" ,
146
164
url_recognize : str = "" ,
147
165
url_register : str = "" ,
166
+ url_face_list : str = "" ,
148
167
):
149
168
self ._api_key = api_key
150
169
self ._timeout = timeout
@@ -153,6 +172,7 @@ def __init__(
153
172
self ._url_detect = self ._url_base + url_detect
154
173
self ._url_recognize = self ._url_base + url_recognize
155
174
self ._url_register = self ._url_base + url_register
175
+ self ._url_face_list = self ._url_base + url_face_list
156
176
157
177
def detect (self ):
158
178
"""Process image_bytes and detect."""
@@ -257,6 +277,7 @@ def __init__(
257
277
url_detect = URL_FACE_DETECTION ,
258
278
url_register = URL_FACE_REGISTER ,
259
279
url_recognize = URL_FACE_RECOGNIZE ,
280
+ url_face_list = URL_FACE_LIST ,
260
281
)
261
282
262
283
def detect (self , image_bytes : bytes ):
@@ -303,3 +324,10 @@ def recognize(self, image_bytes: bytes):
303
324
)
304
325
305
326
return response ["predictions" ]
327
+
328
+ def get_registered_faces (self ):
329
+ """Get the name of the registered faces"""
330
+ response = get_stored_faces (
331
+ url = self ._url_face_list , api_key = self ._api_key , timeout = self ._timeout
332
+ )
333
+ return response ["faces" ]
0 commit comments