@@ -93,24 +93,30 @@ def get_objects_summary(predictions: List[Dict]):
93
93
94
94
def post_image (
95
95
url : str , image_bytes : bytes , api_key : str , timeout : int , data : dict = {}
96
- ):
97
- """Post an image to Deepstack. Only handles excpetions ."""
96
+ ) -> requests . models . Response :
97
+ """Post an image to Deepstack. Only handles exceptions ."""
98
98
try :
99
99
data ["api_key" ] = api_key # Insert the api_key
100
100
return requests .post (
101
101
url , files = {"image" : image_bytes }, data = data , timeout = timeout
102
102
)
103
103
except requests .exceptions .Timeout :
104
104
raise DeepstackException (
105
- f"Timeout connecting to Deepstack, current timeout is { timeout } seconds"
105
+ f"Timeout connecting to Deepstack, the current timeout is { timeout } seconds, try increasing this value"
106
+ )
107
+ except requests .exceptions .ConnectionError or requests .exceptions .MissingSchema as exc :
108
+ raise DeepstackException (
109
+ f"Deepstack connection error, check your IP and port: { exc } "
106
110
)
107
- except requests .exceptions .ConnectionError as exc :
108
- raise DeepstackException (f"Connection error: { exc } " )
109
111
110
112
111
- def process_image (url : str , image_bytes : bytes , api_key : str , timeout : int ):
113
+ def process_image (
114
+ url : str , image_bytes : bytes , api_key : str , timeout : int , data : dict = {}
115
+ ) -> Dict :
112
116
"""Process image_bytes and detect. Handles common status codes"""
113
- response = post_image (url , image_bytes , api_key , timeout )
117
+ response = post_image (
118
+ url = url , image_bytes = image_bytes , api_key = api_key , timeout = timeout , data = data
119
+ )
114
120
if response .status_code == HTTP_OK :
115
121
return response .json ()
116
122
elif response .status_code == BAD_URL :
@@ -168,96 +174,119 @@ def __init__(
168
174
):
169
175
if not custom_model :
170
176
super ().__init__ (
171
- ip , port , api_key , timeout , url_detect = URL_OBJECT_DETECTION ,
177
+ ip = ip ,
178
+ port = port ,
179
+ api_key = api_key ,
180
+ timeout = timeout ,
181
+ url_detect = URL_OBJECT_DETECTION ,
172
182
)
173
183
else :
174
184
super ().__init__ (
175
- ip ,
176
- port ,
177
- api_key ,
178
- timeout ,
185
+ ip = ip ,
186
+ port = port ,
187
+ api_key = api_key ,
188
+ timeout = timeout ,
179
189
url_detect = URL_CUSTOM .format (custom_model = custom_model ),
180
190
)
181
191
182
192
def detect (self , image_bytes : bytes ):
183
193
"""Process image_bytes and detect."""
184
- response_json = process_image (
185
- self ._url_detect , image_bytes , self ._api_key , self ._timeout
194
+ response = process_image (
195
+ url = self ._url_detect ,
196
+ image_bytes = image_bytes ,
197
+ api_key = self ._api_key ,
198
+ timeout = self ._timeout ,
186
199
)
187
- return response_json ["predictions" ]
200
+ return response ["predictions" ]
188
201
189
202
190
203
class DeepstackScene (DeepstackVision ):
191
204
"""Work with scenes"""
192
205
193
206
def __init__ (
194
207
self ,
195
- ip : str ,
196
- port : str ,
208
+ ip : str = DEFAULT_IP ,
209
+ port : int = DEFAULT_PORT ,
197
210
api_key : str = DEFAULT_API_KEY ,
198
211
timeout : int = DEFAULT_TIMEOUT ,
199
212
):
200
213
super ().__init__ (
201
- api_key , timeout , url_detect = URL_SCENE_DETECTION ,
214
+ ip = ip ,
215
+ port = port ,
216
+ api_key = api_key ,
217
+ timeout = timeout ,
218
+ url_detect = URL_SCENE_DETECTION ,
202
219
)
203
220
204
221
def detect (self , image_bytes : bytes ):
205
222
"""Process image_bytes and detect."""
206
- response_json = process_image (self , image_bytes , self ._api_key , self ._timeout )
207
- return response_json
223
+ response = process_image (
224
+ url = self ._url_detect ,
225
+ image_bytes = image_bytes ,
226
+ api_key = self ._api_key ,
227
+ timeout = self ._timeout ,
228
+ )
229
+ return response
208
230
209
231
210
232
class DeepstackFace (DeepstackVision ):
211
233
"""Work with objects"""
212
234
213
235
def __init__ (
214
236
self ,
215
- ip : str ,
216
- port : str ,
237
+ ip : str = DEFAULT_IP ,
238
+ port : int = DEFAULT_PORT ,
217
239
api_key : str = DEFAULT_API_KEY ,
218
240
timeout : int = DEFAULT_TIMEOUT ,
219
241
):
220
242
super ().__init__ (
221
- api_key ,
222
- timeout ,
223
- url_detect = URL_FACE_DETECTION .format (ip = self ._ip , port = self ._port ),
224
- url_register = URL_FACE_REGISTER .format (ip = self ._ip , port = self ._port ),
225
- url_recognize = URL_FACE_RECOGNIZE .format (ip = self ._ip , port = self ._port ),
243
+ ip = ip ,
244
+ port = port ,
245
+ api_key = api_key ,
246
+ timeout = timeout ,
247
+ url_detect = URL_FACE_DETECTION ,
248
+ url_register = URL_FACE_REGISTER ,
249
+ url_recognize = URL_FACE_RECOGNIZE ,
226
250
)
227
251
228
252
def detect (self , image_bytes : bytes ):
229
253
"""Process image_bytes and detect."""
230
- response_json = process_image (self , image_bytes , self ._api_key , self ._timeout )
231
- return response_json
254
+ response = process_image (
255
+ url = self ._url_detect ,
256
+ image_bytes = image_bytes ,
257
+ api_key = self ._api_key ,
258
+ timeout = self ._timeout ,
259
+ )
260
+ return response ["predictions" ]
232
261
233
262
def register (self , name : str , image_bytes : bytes ):
234
263
"""
235
264
Register a face name to a file.
236
265
"""
237
-
238
- response = post_image (
266
+ response = process_image (
239
267
url = self ._url_register ,
240
268
image_bytes = image_bytes ,
241
269
api_key = self ._api_key ,
242
270
timeout = self ._timeout ,
243
271
data = {"userid" : name },
244
272
)
245
273
246
- if response . status_code == HTTP_OK and response . json () ["success" ] == True :
247
- return
274
+ if response ["success" ] == True :
275
+ return response [ "message" ]
248
276
249
- elif response .status_code == HTTP_OK and response .json ()["success" ] == False :
250
- error = response .json ()["error" ]
251
- raise DeepstackException (f"Error from Deepstack: { error } " )
277
+ elif response ["success" ] == False :
278
+ error = response ["error" ]
279
+ raise DeepstackException (
280
+ f"Deepstack raised an error registering a face: { error } "
281
+ )
252
282
253
283
def recognize (self , image_bytes : bytes ):
254
284
"""Process image_bytes, performing recognition."""
255
-
256
- response = post_image (
257
- self ._url_recognize , image_bytes , self ._api_key , self ._timeout
285
+ response = process_image (
286
+ url = self ._url_recognize ,
287
+ image_bytes = image_bytes ,
288
+ api_key = self ._api_key ,
289
+ timeout = self ._timeout ,
258
290
)
259
291
260
- self ._response = response .json ()
261
- if not self ._response ["success" ]:
262
- error = self ._response ["error" ]
263
- raise DeepstackException (f"Error from Deepstack: { error } " )
292
+ return response ["predictions" ]
0 commit comments