7
7
8
8
import json
9
9
import time
10
- from typing import Optional
10
+ import gc
11
11
import adafruit_requests as requests
12
+ import adafruit_logging as logging
12
13
13
14
VERSION = "3.0"
14
15
@@ -111,17 +112,19 @@ def __init__(self, response) -> None:
111
112
def __run_request (url , body , headers ):
112
113
retry = 0
113
114
r = None
115
+ logger = logging .getLogger ("log" )
114
116
115
117
while retry < 10 :
118
+ gc .collect ()
116
119
try :
117
- print ("Trying to send..." )
120
+ logger . debug ("Trying to send..." )
118
121
r = requests .post (url , data = body , headers = headers )
119
122
120
123
if r .status_code != 200 :
121
124
raise CustomVisionError (r .text )
122
125
break
123
126
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 ) )
125
128
retry = retry + 1
126
129
127
130
if retry >= 10 :
@@ -130,6 +133,7 @@ def __run_request(url, body, headers):
130
133
time .sleep (0.5 )
131
134
continue
132
135
136
+ gc .collect ()
133
137
return r
134
138
135
139
@@ -142,10 +146,10 @@ class CustomVisionPredictionClient:
142
146
:type endpoint: str
143
147
"""
144
148
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"
149
153
150
154
def __init__ (self , api_key , endpoint ):
151
155
@@ -160,7 +164,7 @@ def __init__(self, api_key, endpoint):
160
164
self .__base_endpoint = endpoint
161
165
self .api_version = VERSION
162
166
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 ):
164
168
endpoint = self .__base_endpoint + url_format .format (projectId = project_id , publishedName = published_name )
165
169
if not store :
166
170
endpoint = endpoint + "/nostore"
@@ -170,7 +174,7 @@ def __format_endpoint(self, url_format: str, project_id: str, published_name: st
170
174
171
175
return endpoint
172
176
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 ):
174
178
endpoint = self .__format_endpoint (route , project_id , published_name , store , application )
175
179
176
180
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,
181
185
182
186
return ImagePrediction (result_text )
183
187
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 ):
187
189
endpoint = self .__format_endpoint (route , project_id , published_name , store , application )
188
190
189
191
headers = {"Content-Type" : "application/octet-stream" , "Prediction-Key" : self .__api_key }
@@ -193,19 +195,19 @@ def __process_image(
193
195
194
196
return ImagePrediction (result_text )
195
197
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 ):
197
199
return self .__process_image_url (self .__classify_image_url_route , project_id , published_name , url , store , application )
198
200
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 ):
200
202
return self .__process_image (self .__classify_image_route , project_id , published_name , image_data , store , application )
201
203
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 ):
203
205
return self .__process_image_url (self .__detect_image_url_route , project_id , published_name , url , store , application )
204
206
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 ):
206
208
return self .__process_image (self .__detect_image_route , project_id , published_name , image_data , store , application )
207
209
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 :
209
211
"""Classify an image url and saves the result.
210
212
211
213
:param project_id: The project id.
@@ -226,7 +228,7 @@ def classify_image_url(self, project_id: str, published_name: str, url: str, app
226
228
"""
227
229
return self .__classify_image_url (project_id , published_name , url , True , application )
228
230
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 :
230
232
"""Classify an image url without saving the result.
231
233
232
234
:param project_id: The project id.
@@ -247,7 +249,7 @@ def classify_image_url_with_no_store(self, project_id: str, published_name: str,
247
249
"""
248
250
return self .__classify_image_url (project_id , published_name , url , False , application )
249
251
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 :
251
253
"""Classify an image and saves the result.
252
254
253
255
:param project_id: The project id.
@@ -269,7 +271,9 @@ def classify_image(self, project_id: str, published_name: str, image_data: bytea
269
271
"""
270
272
return self .__classify_image (project_id , published_name , image_data , True , application )
271
273
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 :
273
277
"""Classify an image without saving the result.
274
278
275
279
:param project_id: The project id.
@@ -291,7 +295,7 @@ def classify_image_with_no_store(self, project_id: str, published_name: str, ima
291
295
"""
292
296
return self .__classify_image (project_id , published_name , image_data , False , application )
293
297
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 :
295
299
"""Detect objects in an image url and saves the result.
296
300
297
301
:param project_id: The project id.
@@ -312,7 +316,7 @@ def detect_image_url(self, project_id: str, published_name: str, url: str, appli
312
316
"""
313
317
return self .__detect_image_url (project_id , published_name , url , True , application )
314
318
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 :
316
320
"""Detect objects in an image url without saving the result.
317
321
318
322
: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
333
337
"""
334
338
return self .__detect_image_url (project_id , published_name , url , False , application )
335
339
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 :
337
341
"""Detect objects in an image and saves the result.
338
342
339
343
:param project_id: The project id.
@@ -355,7 +359,7 @@ def detect_image(self, project_id: str, published_name: str, image_data: bytearr
355
359
"""
356
360
return self .__detect_image (project_id , published_name , image_data , True , application )
357
361
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 :
359
363
"""Detect objects in an image without saving the result.
360
364
361
365
:param project_id: The project id.
0 commit comments