1010DEFAULT_TIMEOUT = 10 # seconds
1111DEFAULT_IP = "localhost"
1212DEFAULT_PORT = 80
13+ DEFAULT_MIN_CONFIDENCE = 0.45
1314
1415## HTTP codes
1516HTTP_OK = 200
@@ -92,11 +93,10 @@ def get_objects_summary(predictions: List[Dict]):
9293
9394
9495def post_image (
95- url : str , image_bytes : bytes , api_key : str , timeout : int , data : dict = {}
96+ url : str , image_bytes : bytes , timeout : int , data : dict
9697) -> requests .models .Response :
9798 """Post an image to Deepstack. Only handles exceptions."""
9899 try :
99- data ["api_key" ] = api_key # Insert the api_key
100100 return requests .post (
101101 url , files = {"image" : image_bytes }, data = data , timeout = timeout
102102 )
@@ -111,12 +111,17 @@ def post_image(
111111
112112
113113def process_image (
114- url : str , image_bytes : bytes , api_key : str , timeout : int , data : dict = {}
114+ url : str ,
115+ image_bytes : bytes ,
116+ api_key : str ,
117+ min_confidence : float ,
118+ timeout : int ,
119+ data : dict = {},
115120) -> Dict :
116121 """Process image_bytes and detect. Handles common status codes"""
117- response = post_image (
118- url = url , image_bytes = image_bytes , api_key = api_key , timeout = timeout , data = data
119- )
122+ data [ "api_key" ] = api_key
123+ data [ "min_confidence" ] = min_confidence
124+ response = post_image ( url = url , image_bytes = image_bytes , timeout = timeout , data = data )
120125 if response .status_code == HTTP_OK :
121126 return response .json ()
122127 elif response .status_code == BAD_URL :
@@ -136,17 +141,18 @@ def __init__(
136141 port : int = DEFAULT_PORT ,
137142 api_key : str = DEFAULT_API_KEY ,
138143 timeout : int = DEFAULT_TIMEOUT ,
144+ min_confidence : float = DEFAULT_MIN_CONFIDENCE ,
139145 url_detect : str = "" ,
140146 url_recognize : str = "" ,
141147 url_register : str = "" ,
142148 ):
143-
149+ self ._api_key = api_key
150+ self ._timeout = timeout
151+ self ._min_confidence = min_confidence
144152 self ._url_base = URL_BASE_VISION .format (ip = ip , port = port )
145153 self ._url_detect = self ._url_base + url_detect
146154 self ._url_recognize = self ._url_base + url_recognize
147155 self ._url_register = self ._url_base + url_register
148- self ._api_key = api_key
149- self ._timeout = timeout
150156
151157 def detect (self ):
152158 """Process image_bytes and detect."""
@@ -170,14 +176,20 @@ def __init__(
170176 port : int = DEFAULT_PORT ,
171177 api_key : str = DEFAULT_API_KEY ,
172178 timeout : int = DEFAULT_TIMEOUT ,
179+ min_confidence : float = DEFAULT_MIN_CONFIDENCE ,
173180 custom_model : str = "" ,
174181 ):
175182 if custom_model :
176183 url_detect = URL_CUSTOM .format (custom_model = custom_model )
177184 else :
178185 url_detect = URL_OBJECT_DETECTION
179186 super ().__init__ (
180- ip = ip , port = port , api_key = api_key , timeout = timeout , url_detect = url_detect ,
187+ ip = ip ,
188+ port = port ,
189+ api_key = api_key ,
190+ timeout = timeout ,
191+ min_confidence = min_confidence ,
192+ url_detect = url_detect ,
181193 )
182194
183195 def detect (self , image_bytes : bytes ):
@@ -186,6 +198,7 @@ def detect(self, image_bytes: bytes):
186198 url = self ._url_detect ,
187199 image_bytes = image_bytes ,
188200 api_key = self ._api_key ,
201+ min_confidence = self ._min_confidence ,
189202 timeout = self ._timeout ,
190203 )
191204 return response ["predictions" ]
@@ -200,12 +213,14 @@ def __init__(
200213 port : int = DEFAULT_PORT ,
201214 api_key : str = DEFAULT_API_KEY ,
202215 timeout : int = DEFAULT_TIMEOUT ,
216+ min_confidence : float = DEFAULT_MIN_CONFIDENCE ,
203217 ):
204218 super ().__init__ (
205219 ip = ip ,
206220 port = port ,
207221 api_key = api_key ,
208222 timeout = timeout ,
223+ min_confidence = min_confidence ,
209224 url_recognize = URL_SCENE_RECOGNIZE ,
210225 )
211226
@@ -215,6 +230,7 @@ def recognize(self, image_bytes: bytes):
215230 url = self ._url_recognize ,
216231 image_bytes = image_bytes ,
217232 api_key = self ._api_key ,
233+ min_confidence = self ._min_confidence ,
218234 timeout = self ._timeout ,
219235 )
220236 del response ["success" ]
@@ -230,12 +246,14 @@ def __init__(
230246 port : int = DEFAULT_PORT ,
231247 api_key : str = DEFAULT_API_KEY ,
232248 timeout : int = DEFAULT_TIMEOUT ,
249+ min_confidence : float = DEFAULT_MIN_CONFIDENCE ,
233250 ):
234251 super ().__init__ (
235252 ip = ip ,
236253 port = port ,
237254 api_key = api_key ,
238255 timeout = timeout ,
256+ min_confidence = min_confidence ,
239257 url_detect = URL_FACE_DETECTION ,
240258 url_register = URL_FACE_REGISTER ,
241259 url_recognize = URL_FACE_RECOGNIZE ,
@@ -247,6 +265,7 @@ def detect(self, image_bytes: bytes):
247265 url = self ._url_detect ,
248266 image_bytes = image_bytes ,
249267 api_key = self ._api_key ,
268+ min_confidence = self ._min_confidence ,
250269 timeout = self ._timeout ,
251270 )
252271 return response ["predictions" ]
@@ -259,6 +278,7 @@ def register(self, name: str, image_bytes: bytes):
259278 url = self ._url_register ,
260279 image_bytes = image_bytes ,
261280 api_key = self ._api_key ,
281+ min_confidence = self ._min_confidence ,
262282 timeout = self ._timeout ,
263283 data = {"userid" : name },
264284 )
@@ -278,6 +298,7 @@ def recognize(self, image_bytes: bytes):
278298 url = self ._url_recognize ,
279299 image_bytes = image_bytes ,
280300 api_key = self ._api_key ,
301+ min_confidence = self ._min_confidence ,
281302 timeout = self ._timeout ,
282303 )
283304
0 commit comments