Skip to content

Commit 7b5db72

Browse files
committed
Catch exceptions
1 parent f42a90a commit 7b5db72

File tree

2 files changed

+67
-38
lines changed

2 files changed

+67
-38
lines changed

deepstack/core.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
HTTP_OK = 200
1010
HTTP_BAD_REQUEST = 400
1111
HTTP_UNAUTHORIZED = 401
12-
TIMEOUT = 20 # seconds
12+
DEFAULT_TIMEOUT = 10 # seconds
1313

1414

1515
def format_confidence(confidence: Union[str, float]) -> float:
@@ -55,21 +55,40 @@ def get_objects_summary(predictions: List[Dict]):
5555
}
5656

5757

58-
def post_image(url: str, image: bytes):
58+
def post_image(url: str, image: bytes, api_key: str, timeout: int):
5959
"""Post an image to Deepstack."""
60-
response = requests.post(url, files={"image": image}, timeout=TIMEOUT)
61-
return response
60+
try:
61+
response = requests.post(
62+
url, files={"image": image}, data={"api_key": api_key}, timeout=timeout
63+
)
64+
return response
65+
except requests.exceptions.Timeout:
66+
raise DeepstackException(
67+
f"Timeout connecting to Deepstack, current timeout is {timeout} seconds"
68+
)
69+
70+
71+
class DeepstackException(Exception):
72+
pass
6273

6374

6475
class DeepstackObject:
6576
"""The object detection API locates and classifies 80
6677
different kinds of objects in a single image.."""
6778

68-
def __init__(self, ip_address: str, port: str):
79+
def __init__(
80+
self,
81+
ip_address: str,
82+
port: str,
83+
api_key: str = "",
84+
timeout: int = DEFAULT_TIMEOUT,
85+
):
6986

7087
self._url_object_detection = "http://{}:{}/v1/vision/detection".format(
7188
ip_address, port
7289
)
90+
self._api_key = api_key
91+
self._timeout = timeout
7392
self._predictions = []
7493

7594
def process_file(self, file_path: str):
@@ -81,10 +100,16 @@ def process_image_bytes(self, image_bytes: bytes):
81100
"""Process an image."""
82101
self._predictions = []
83102

84-
response = post_image(self._url_object_detection, image_bytes)
85-
if response:
86-
if response.status_code == HTTP_OK:
103+
response = post_image(
104+
self._url_object_detection, image_bytes, self._api_key, self._timeout
105+
)
106+
107+
if response.status_code == HTTP_OK:
108+
if response.json()["success"]:
87109
self._predictions = response.json()["predictions"]
110+
else:
111+
error = response.json()["error"]
112+
raise DeepstackException(f"Error from Deepstack: {error}")
88113

89114
@property
90115
def predictions(self):

usage.ipynb

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,36 @@
1515
"source": [
1616
"import deepstack.core as ds\n",
1717
"from PIL import Image\n",
18+
"import pprint\n",
1819
"import matplotlib.pyplot as plt\n",
1920
"%matplotlib inline"
2021
]
2122
},
2223
{
2324
"cell_type": "code",
24-
"execution_count": 2,
25+
"execution_count": 6,
2526
"metadata": {},
2627
"outputs": [],
2728
"source": [
2829
"IP_ADDRESS = 'localhost'\n",
29-
"PORT = '5000'"
30+
"PORT = '5000'\n",
31+
"API_KEY = \"Mysecretkey\"\n",
32+
"# API_KEY = \"BadKey\"\n",
33+
"TIMEOUT = 8"
3034
]
3135
},
3236
{
3337
"cell_type": "code",
34-
"execution_count": 3,
38+
"execution_count": 7,
3539
"metadata": {},
3640
"outputs": [],
3741
"source": [
38-
"dsobject = ds.DeepstackObject(IP_ADDRESS, PORT)"
42+
"dsobject = ds.DeepstackObject(IP_ADDRESS, PORT, API_KEY, TIMEOUT)"
3943
]
4044
},
4145
{
4246
"cell_type": "code",
43-
"execution_count": 4,
47+
"execution_count": 8,
4448
"metadata": {},
4549
"outputs": [
4650
{
@@ -71,21 +75,24 @@
7175
},
7276
{
7377
"cell_type": "code",
74-
"execution_count": 5,
78+
"execution_count": 9,
7579
"metadata": {},
7680
"outputs": [
7781
{
7882
"name": "stdout",
7983
"output_type": "stream",
8084
"text": [
81-
"CPU times: user 7.72 ms, sys: 6.6 ms, total: 14.3 ms\n",
82-
"Wall time: 7.63 s\n"
85+
"[{'confidence': 0.9998661, 'label': 'person', 'y_min': 0, 'x_min': 258, 'y_max': 676, 'x_max': 485}, {'confidence': 0.9996547, 'label': 'person', 'y_min': 0, 'x_min': 405, 'y_max': 652, 'x_max': 639}, {'confidence': 0.99745613, 'label': 'dog', 'y_min': 311, 'x_min': 624, 'y_max': 591, 'x_max': 825}]\n"
8386
]
8487
}
8588
],
8689
"source": [
87-
"%%time\n",
88-
"dsobject.process_file(image_path)"
90+
"#%%time\n",
91+
"try:\n",
92+
" dsobject.process_file(image_path)\n",
93+
" print(dsobject.predictions)\n",
94+
"except ds.DeepstackException as exc:\n",
95+
" print(exc)"
8996
]
9097
},
9198
{
@@ -97,7 +104,7 @@
97104
},
98105
{
99106
"cell_type": "code",
100-
"execution_count": 6,
107+
"execution_count": 10,
101108
"metadata": {},
102109
"outputs": [
103110
{
@@ -123,7 +130,7 @@
123130
" 'x_max': 825}]"
124131
]
125132
},
126-
"execution_count": 6,
133+
"execution_count": 10,
127134
"metadata": {},
128135
"output_type": "execute_result"
129136
}
@@ -142,7 +149,7 @@
142149
},
143150
{
144151
"cell_type": "code",
145-
"execution_count": 20,
152+
"execution_count": 11,
146153
"metadata": {},
147154
"outputs": [],
148155
"source": [
@@ -158,7 +165,7 @@
158165
},
159166
{
160167
"cell_type": "code",
161-
"execution_count": 10,
168+
"execution_count": 12,
162169
"metadata": {},
163170
"outputs": [
164171
{
@@ -167,7 +174,7 @@
167174
"{'dog', 'person'}"
168175
]
169176
},
170-
"execution_count": 10,
177+
"execution_count": 12,
171178
"metadata": {},
172179
"output_type": "execute_result"
173180
}
@@ -185,16 +192,16 @@
185192
},
186193
{
187194
"cell_type": "code",
188-
"execution_count": 11,
195+
"execution_count": 13,
189196
"metadata": {},
190197
"outputs": [
191198
{
192199
"data": {
193200
"text/plain": [
194-
"{'dog': 1, 'person': 2}"
201+
"{'person': 2, 'dog': 1}"
195202
]
196203
},
197-
"execution_count": 11,
204+
"execution_count": 13,
198205
"metadata": {},
199206
"output_type": "execute_result"
200207
}
@@ -212,7 +219,7 @@
212219
},
213220
{
214221
"cell_type": "code",
215-
"execution_count": 16,
222+
"execution_count": 14,
216223
"metadata": {},
217224
"outputs": [
218225
{
@@ -221,7 +228,7 @@
221228
"[0.9998661, 0.9996547]"
222229
]
223230
},
224-
"execution_count": 16,
231+
"execution_count": 14,
225232
"metadata": {},
226233
"output_type": "execute_result"
227234
}
@@ -240,23 +247,20 @@
240247
},
241248
{
242249
"cell_type": "code",
243-
"execution_count": 19,
250+
"execution_count": 15,
244251
"metadata": {},
245252
"outputs": [
246253
{
247-
"data": {
248-
"text/plain": [
249-
"[0.9998661]"
250-
]
251-
},
252-
"execution_count": 19,
253-
"metadata": {},
254-
"output_type": "execute_result"
254+
"name": "stdout",
255+
"output_type": "stream",
256+
"text": [
257+
"1\n"
258+
]
255259
}
256260
],
257261
"source": [
258262
"CONFIDENCE_THRESHOLD = 0.9997\n",
259-
"ds.get_confidences_above_threshold(confidences, CONFIDENCE_THRESHOLD)"
263+
"print(len(ds.get_confidences_above_threshold(confidences, CONFIDENCE_THRESHOLD)))"
260264
]
261265
},
262266
{

0 commit comments

Comments
 (0)