Skip to content

Commit d2f6c7c

Browse files
committed
many fixes
1 parent 31563bb commit d2f6c7c

File tree

3 files changed

+195
-132
lines changed

3 files changed

+195
-132
lines changed

deepstack/core.py

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,30 @@ def get_objects_summary(predictions: List[Dict]):
9393

9494
def post_image(
9595
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."""
9898
try:
9999
data["api_key"] = api_key # Insert the api_key
100100
return requests.post(
101101
url, files={"image": image_bytes}, data=data, timeout=timeout
102102
)
103103
except requests.exceptions.Timeout:
104104
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}"
106110
)
107-
except requests.exceptions.ConnectionError as exc:
108-
raise DeepstackException(f"Connection error: {exc}")
109111

110112

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:
112116
"""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+
)
114120
if response.status_code == HTTP_OK:
115121
return response.json()
116122
elif response.status_code == BAD_URL:
@@ -168,96 +174,119 @@ def __init__(
168174
):
169175
if not custom_model:
170176
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,
172182
)
173183
else:
174184
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,
179189
url_detect=URL_CUSTOM.format(custom_model=custom_model),
180190
)
181191

182192
def detect(self, image_bytes: bytes):
183193
"""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,
186199
)
187-
return response_json["predictions"]
200+
return response["predictions"]
188201

189202

190203
class DeepstackScene(DeepstackVision):
191204
"""Work with scenes"""
192205

193206
def __init__(
194207
self,
195-
ip: str,
196-
port: str,
208+
ip: str = DEFAULT_IP,
209+
port: int = DEFAULT_PORT,
197210
api_key: str = DEFAULT_API_KEY,
198211
timeout: int = DEFAULT_TIMEOUT,
199212
):
200213
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,
202219
)
203220

204221
def detect(self, image_bytes: bytes):
205222
"""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
208230

209231

210232
class DeepstackFace(DeepstackVision):
211233
"""Work with objects"""
212234

213235
def __init__(
214236
self,
215-
ip: str,
216-
port: str,
237+
ip: str = DEFAULT_IP,
238+
port: int = DEFAULT_PORT,
217239
api_key: str = DEFAULT_API_KEY,
218240
timeout: int = DEFAULT_TIMEOUT,
219241
):
220242
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,
226250
)
227251

228252
def detect(self, image_bytes: bytes):
229253
"""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"]
232261

233262
def register(self, name: str, image_bytes: bytes):
234263
"""
235264
Register a face name to a file.
236265
"""
237-
238-
response = post_image(
266+
response = process_image(
239267
url=self._url_register,
240268
image_bytes=image_bytes,
241269
api_key=self._api_key,
242270
timeout=self._timeout,
243271
data={"userid": name},
244272
)
245273

246-
if response.status_code == HTTP_OK and response.json()["success"] == True:
247-
return
274+
if response["success"] == True:
275+
return response["message"]
248276

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+
)
252282

253283
def recognize(self, image_bytes: bytes):
254284
"""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,
258290
)
259291

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"]

usage-face-recognition.ipynb

Lines changed: 96 additions & 47 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)