6
6
from typing import Union , List , Set , Dict
7
7
8
8
## Const
9
- HTTP_OK = 200
9
+ DEFAULT_API_KEY = ""
10
10
DEFAULT_TIMEOUT = 10 # seconds
11
11
12
+ ## HTTP codes
13
+ HTTP_OK = 200
14
+ BAD_URL = 404
15
+
12
16
## API urls
13
17
URL_CUSTOM = "http://{ip}:{port}/v1/vision/custom/{custom_model}"
14
18
URL_OBJECT_DETECTION = "http://{ip}:{port}/v1/vision/detection"
@@ -22,7 +26,8 @@ def format_confidence(confidence: Union[str, float]) -> float:
22
26
"""Takes a confidence from the API like
23
27
0.55623 and returns 55.6 (%).
24
28
"""
25
- return round (float (confidence ) * 100 , 1 )
29
+ DECIMALS = 1
30
+ return round (float (confidence ) * 100 , DECIMALS )
26
31
27
32
28
33
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]:
50
55
Get a list of the unique objects predicted.
51
56
"""
52
57
labels = [pred ["label" ] for pred in predictions ]
53
- return list (set (labels ))
58
+ return sorted ( list (set (labels ) ))
54
59
55
60
56
61
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
74
79
try :
75
80
data ["api_key" ] = api_key # Insert the api_key
76
81
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 } " )
78
88
except requests .exceptions .Timeout :
79
89
raise DeepstackException (f"Timeout connecting to Deepstack, current timeout is { timeout } seconds" )
80
90
except requests .exceptions .ConnectionError as exc :
@@ -107,13 +117,7 @@ def __init__(
107
117
def detect (self , image_bytes : bytes ):
108
118
"""Process image_bytes and detect."""
109
119
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 ()
117
121
if not self ._response ["success" ]:
118
122
error = self ._response ["error" ]
119
123
raise DeepstackException (f"Error from Deepstack: { error } " )
@@ -136,7 +140,12 @@ class DeepstackObject(Deepstack):
136
140
"""Work with objects"""
137
141
138
142
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 ,
140
149
):
141
150
if not custom_model :
142
151
super ().__init__ (
@@ -157,7 +166,7 @@ class DeepstackScene(Deepstack):
157
166
"""Work with scenes"""
158
167
159
168
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 ,
161
170
):
162
171
super ().__init__ (
163
172
api_key , timeout , url_detect = URL_SCENE_DETECTION .format (ip = self ._ip , port = self ._port ),
@@ -173,7 +182,7 @@ class DeepstackFace(Deepstack):
173
182
"""Work with objects"""
174
183
175
184
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 ,
177
186
):
178
187
super ().__init__ (
179
188
api_key ,
@@ -213,10 +222,6 @@ def recognize(self, image_bytes: bytes):
213
222
214
223
response = post_image (self ._url_recognize , image_bytes , self ._api_key , self ._timeout )
215
224
216
- if not response .status_code == HTTP_OK :
217
- raise DeepstackException (f"Error from request, status code: { response .status_code } " )
218
- return
219
-
220
225
self ._response = response .json ()
221
226
if not self ._response ["success" ]:
222
227
error = self ._response ["error" ]
0 commit comments