66from typing import Union , List , Set , Dict
77
88## Const
9- HTTP_OK = 200
9+ DEFAULT_API_KEY = ""
1010DEFAULT_TIMEOUT = 10 # seconds
1111
12+ ## HTTP codes
13+ HTTP_OK = 200
14+ BAD_URL = 404
15+
1216## API urls
1317URL_CUSTOM = "http://{ip}:{port}/v1/vision/custom/{custom_model}"
1418URL_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
2833def 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
5661def 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" ]
0 commit comments