77
88import json
99import time
10- from typing import Optional
10+ import gc
1111import adafruit_requests as requests
12+ import adafruit_logging as logging
1213
1314VERSION = "3.0"
1415
@@ -111,17 +112,19 @@ def __init__(self, response) -> None:
111112def __run_request (url , body , headers ):
112113 retry = 0
113114 r = None
115+ logger = logging .getLogger ("log" )
114116
115117 while retry < 10 :
118+ gc .collect ()
116119 try :
117- print ("Trying to send..." )
120+ logger . debug ("Trying to send..." )
118121 r = requests .post (url , data = body , headers = headers )
119122
120123 if r .status_code != 200 :
121124 raise CustomVisionError (r .text )
122125 break
123126 except RuntimeError as runtime_error :
124- print ("Could not send data, retrying after 5 seconds: " , runtime_error )
127+ logger . info ("Could not send data, retrying after 5 seconds: " + str ( runtime_error ) )
125128 retry = retry + 1
126129
127130 if retry >= 10 :
@@ -130,6 +133,7 @@ def __run_request(url, body, headers):
130133 time .sleep (0.5 )
131134 continue
132135
136+ gc .collect ()
133137 return r
134138
135139
@@ -142,10 +146,10 @@ class CustomVisionPredictionClient:
142146 :type endpoint: str
143147 """
144148
145- __classify_image_url_route = "customvision/v3.0 /Prediction/{projectId}/classify/iterations/{publishedName}/url"
146- __classify_image_route = "customvision/v3.0 /Prediction/{projectId}/classify/iterations/{publishedName}/image"
147- __detect_image_url_route = "customvision/v3.0 /Prediction/{projectId}/classify /iterations/{publishedName}/url"
148- __detect_image_route = "customvision/v3.0 /Prediction/{projectId}/classify /iterations/{publishedName}/image"
149+ __classify_image_url_route = "customvision/v" + VERSION + " /Prediction/{projectId}/classify/iterations/{publishedName}/url"
150+ __classify_image_route = "customvision/v" + VERSION + " /Prediction/{projectId}/classify/iterations/{publishedName}/image"
151+ __detect_image_url_route = "customvision/v" + VERSION + " /Prediction/{projectId}/detect /iterations/{publishedName}/url"
152+ __detect_image_route = "customvision/v" + VERSION + " /Prediction/{projectId}/detect /iterations/{publishedName}/image"
149153
150154 def __init__ (self , api_key , endpoint ):
151155
@@ -160,7 +164,7 @@ def __init__(self, api_key, endpoint):
160164 self .__base_endpoint = endpoint
161165 self .api_version = VERSION
162166
163- def __format_endpoint (self , url_format : str , project_id : str , published_name : str , store : bool , application : Optional [ str ] ):
167+ def __format_endpoint (self , url_format : str , project_id : str , published_name : str , store : bool , application ):
164168 endpoint = self .__base_endpoint + url_format .format (projectId = project_id , publishedName = published_name )
165169 if not store :
166170 endpoint = endpoint + "/nostore"
@@ -170,7 +174,7 @@ def __format_endpoint(self, url_format: str, project_id: str, published_name: st
170174
171175 return endpoint
172176
173- def __process_image_url (self , route : str , project_id : str , published_name : str , url : str , store : bool , application : Optional [ str ] ):
177+ def __process_image_url (self , route : str , project_id : str , published_name : str , url : str , store : bool , application ):
174178 endpoint = self .__format_endpoint (route , project_id , published_name , store , application )
175179
176180 headers = {"Content-Type" : "application/json" , "Prediction-Key" : self .__api_key }
@@ -181,9 +185,7 @@ def __process_image_url(self, route: str, project_id: str, published_name: str,
181185
182186 return ImagePrediction (result_text )
183187
184- def __process_image (
185- self , route : str , project_id : str , published_name : str , image_data : bytearray , store : bool , application : Optional [str ]
186- ):
188+ def __process_image (self , route : str , project_id : str , published_name : str , image_data : bytearray , store : bool , application ):
187189 endpoint = self .__format_endpoint (route , project_id , published_name , store , application )
188190
189191 headers = {"Content-Type" : "application/octet-stream" , "Prediction-Key" : self .__api_key }
@@ -193,19 +195,19 @@ def __process_image(
193195
194196 return ImagePrediction (result_text )
195197
196- def __classify_image_url (self , project_id : str , published_name : str , url : str , store : bool , application : Optional [ str ] ):
198+ def __classify_image_url (self , project_id : str , published_name : str , url : str , store : bool , application ):
197199 return self .__process_image_url (self .__classify_image_url_route , project_id , published_name , url , store , application )
198200
199- def __classify_image (self , project_id : str , published_name : str , image_data : bytearray , store : bool , application : Optional [ str ] ):
201+ def __classify_image (self , project_id : str , published_name : str , image_data : bytearray , store : bool , application ):
200202 return self .__process_image (self .__classify_image_route , project_id , published_name , image_data , store , application )
201203
202- def __detect_image_url (self , project_id : str , published_name : str , url : str , store : bool , application : Optional [ str ] ):
204+ def __detect_image_url (self , project_id : str , published_name : str , url : str , store : bool , application ):
203205 return self .__process_image_url (self .__detect_image_url_route , project_id , published_name , url , store , application )
204206
205- def __detect_image (self , project_id : str , published_name : str , image_data : bytearray , store : bool , application : Optional [ str ] ):
207+ def __detect_image (self , project_id : str , published_name : str , image_data : bytearray , store : bool , application ):
206208 return self .__process_image (self .__detect_image_route , project_id , published_name , image_data , store , application )
207209
208- def classify_image_url (self , project_id : str , published_name : str , url : str , application : Optional [ str ] = None ):
210+ def classify_image_url (self , project_id : str , published_name : str , url : str , application = None ) -> ImagePrediction :
209211 """Classify an image url and saves the result.
210212
211213 :param project_id: The project id.
@@ -226,7 +228,7 @@ def classify_image_url(self, project_id: str, published_name: str, url: str, app
226228 """
227229 return self .__classify_image_url (project_id , published_name , url , True , application )
228230
229- def classify_image_url_with_no_store (self , project_id : str , published_name : str , url : str , application : Optional [ str ] = None ):
231+ def classify_image_url_with_no_store (self , project_id : str , published_name : str , url : str , application = None ) -> ImagePrediction :
230232 """Classify an image url without saving the result.
231233
232234 :param project_id: The project id.
@@ -247,7 +249,7 @@ def classify_image_url_with_no_store(self, project_id: str, published_name: str,
247249 """
248250 return self .__classify_image_url (project_id , published_name , url , False , application )
249251
250- def classify_image (self , project_id : str , published_name : str , image_data : bytearray , application : Optional [ str ] = None ):
252+ def classify_image (self , project_id : str , published_name : str , image_data : bytearray , application = None ) -> ImagePrediction :
251253 """Classify an image and saves the result.
252254
253255 :param project_id: The project id.
@@ -269,7 +271,9 @@ def classify_image(self, project_id: str, published_name: str, image_data: bytea
269271 """
270272 return self .__classify_image (project_id , published_name , image_data , True , application )
271273
272- def classify_image_with_no_store (self , project_id : str , published_name : str , image_data : bytearray , application : Optional [str ] = None ):
274+ def classify_image_with_no_store (
275+ self , project_id : str , published_name : str , image_data : bytearray , application = None
276+ ) -> ImagePrediction :
273277 """Classify an image without saving the result.
274278
275279 :param project_id: The project id.
@@ -291,7 +295,7 @@ def classify_image_with_no_store(self, project_id: str, published_name: str, ima
291295 """
292296 return self .__classify_image (project_id , published_name , image_data , False , application )
293297
294- def detect_image_url (self , project_id : str , published_name : str , url : str , application : Optional [ str ] = None ):
298+ def detect_image_url (self , project_id : str , published_name : str , url : str , application = None ) -> ImagePrediction :
295299 """Detect objects in an image url and saves the result.
296300
297301 :param project_id: The project id.
@@ -312,7 +316,7 @@ def detect_image_url(self, project_id: str, published_name: str, url: str, appli
312316 """
313317 return self .__detect_image_url (project_id , published_name , url , True , application )
314318
315- def detect_image_url_with_no_store (self , project_id : str , published_name : str , url : str , application : Optional [ str ] = None ):
319+ def detect_image_url_with_no_store (self , project_id : str , published_name : str , url : str , application = None ) -> ImagePrediction :
316320 """Detect objects in an image url without saving the result.
317321
318322 :param project_id: The project id.
@@ -333,7 +337,7 @@ def detect_image_url_with_no_store(self, project_id: str, published_name: str, u
333337 """
334338 return self .__detect_image_url (project_id , published_name , url , False , application )
335339
336- def detect_image (self , project_id : str , published_name : str , image_data : bytearray , application : Optional [ str ] = None ):
340+ def detect_image (self , project_id : str , published_name : str , image_data : bytearray , application = None ) -> ImagePrediction :
337341 """Detect objects in an image and saves the result.
338342
339343 :param project_id: The project id.
@@ -355,7 +359,7 @@ def detect_image(self, project_id: str, published_name: str, image_data: bytearr
355359 """
356360 return self .__detect_image (project_id , published_name , image_data , True , application )
357361
358- def detect_image_with_no_store (self , project_id : str , published_name : str , image_data : bytearray , application : Optional [ str ] = None ):
362+ def detect_image_with_no_store (self , project_id : str , published_name : str , image_data : bytearray , application = None ) -> ImagePrediction :
359363 """Detect objects in an image without saving the result.
360364
361365 :param project_id: The project id.
0 commit comments