Skip to content

Commit b569a02

Browse files
committed
more formatting
1 parent c234c06 commit b569a02

File tree

2 files changed

+144
-31
lines changed

2 files changed

+144
-31
lines changed

deepstack/core.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
from typing import Union, List, Set, Dict
77

88
## Const
9-
HTTP_OK = 200
9+
DEFAULT_API_KEY = ""
1010
DEFAULT_TIMEOUT = 10 # seconds
1111

12+
## HTTP codes
13+
HTTP_OK = 200
14+
BAD_URL = 404
15+
1216
## API urls
1317
URL_CUSTOM = "http://{ip}:{port}/v1/vision/custom/{custom_model}"
1418
URL_OBJECT_DETECTION = "http://{ip}:{port}/v1/vision/detection"
@@ -22,7 +26,8 @@ def format_confidence(confidence: Union[str, float]) -> float:
2226
"""Takes a confidence from the API like
2327
0.55623 and returns 55.6 (%).
2428
"""
25-
return round(float(confidence) * 100, 1)
29+
DECIMALS = 1
30+
return round(float(confidence) * 100, DECIMALS)
2631

2732

2833
def get_confidences_above_threshold(confidences: List[float], confidence_threshold: float) -> List[float]:
@@ -50,7 +55,7 @@ def get_objects(predictions: List[Dict]) -> List[str]:
5055
Get a list of the unique objects predicted.
5156
"""
5257
labels = [pred["label"] for pred in predictions]
53-
return list(set(labels))
58+
return sorted(list(set(labels)))
5459

5560

5661
def get_object_confidences(predictions: List[Dict], target_object: str):
@@ -74,7 +79,12 @@ def post_image(url: str, image_bytes: bytes, api_key: str, timeout: int, data: d
7479
try:
7580
data["api_key"] = api_key # Insert the api_key
7681
response = requests.post(url, files={"image": image_bytes}, data=data, timeout=timeout)
77-
return response
82+
if response.status_code == HTTP_OK:
83+
return response
84+
elif response.status_code == BAD_URL:
85+
raise DeepstackException(f"Bad url supplied, url {url} raised error {BAD_URL}")
86+
else:
87+
raise DeepstackException(f"Error from Deepstack request, status code: {response.status_code}")
7888
except requests.exceptions.Timeout:
7989
raise DeepstackException(f"Timeout connecting to Deepstack, current timeout is {timeout} seconds")
8090
except requests.exceptions.ConnectionError as exc:
@@ -107,13 +117,7 @@ def __init__(
107117
def detect(self, image_bytes: bytes):
108118
"""Process image_bytes and detect."""
109119
self._response = None
110-
response = post_image(self._url_detect, image_bytes, self._api_key, self._timeout)
111-
112-
if not response.status_code == HTTP_OK:
113-
raise DeepstackException(f"Error from request, status code: {response.status_code}")
114-
return
115-
116-
self._response = response.json()
120+
self._response = post_image(self._url_detect, image_bytes, self._api_key, self._timeout).json()
117121
if not self._response["success"]:
118122
error = self._response["error"]
119123
raise DeepstackException(f"Error from Deepstack: {error}")
@@ -136,7 +140,12 @@ class DeepstackObject(Deepstack):
136140
"""Work with objects"""
137141

138142
def __init__(
139-
self, ip: str, port: str, api_key: str = "", timeout: int = DEFAULT_TIMEOUT, custom_model: str = None,
143+
self,
144+
ip: str,
145+
port: str,
146+
api_key: str = DEFAULT_API_KEY,
147+
timeout: int = DEFAULT_TIMEOUT,
148+
custom_model: str = None,
140149
):
141150
if not custom_model:
142151
super().__init__(
@@ -157,7 +166,7 @@ class DeepstackScene(Deepstack):
157166
"""Work with scenes"""
158167

159168
def __init__(
160-
self, ip: str, port: str, api_key: str = "", timeout: int = DEFAULT_TIMEOUT,
169+
self, ip: str, port: str, api_key: str = DEFAULT_API_KEY, timeout: int = DEFAULT_TIMEOUT,
161170
):
162171
super().__init__(
163172
api_key, timeout, url_detect=URL_SCENE_DETECTION.format(ip=self._ip, port=self._port),
@@ -173,7 +182,7 @@ class DeepstackFace(Deepstack):
173182
"""Work with objects"""
174183

175184
def __init__(
176-
self, ip: str, port: str, api_key: str = "", timeout: int = DEFAULT_TIMEOUT,
185+
self, ip: str, port: str, api_key: str = DEFAULT_API_KEY, timeout: int = DEFAULT_TIMEOUT,
177186
):
178187
super().__init__(
179188
api_key,
@@ -213,10 +222,6 @@ def recognize(self, image_bytes: bytes):
213222

214223
response = post_image(self._url_recognize, image_bytes, self._api_key, self._timeout)
215224

216-
if not response.status_code == HTTP_OK:
217-
raise DeepstackException(f"Error from request, status code: {response.status_code}")
218-
return
219-
220225
self._response = response.json()
221226
if not self._response["success"]:
222227
error = self._response["error"]

usage-object-detection.ipynb

Lines changed: 121 additions & 13 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)