9
9
import time
10
10
from typing import Optional
11
11
import adafruit_requests as requests
12
- from .version import VERSION
13
- from . import models
12
+
13
+ VERSION = "3.0"
14
+
15
+
16
+ class CustomVisionError (Exception ):
17
+ """
18
+ An error from the custom vision service
19
+ """
20
+
21
+ def __init__ (self , message ):
22
+ super (CustomVisionError , self ).__init__ (message )
23
+ self .message = message
24
+
25
+
26
+ class BoundingBox :
27
+ """Bounding box that defines a region of an image.
28
+
29
+ All required parameters must be populated in order to send to Azure.
30
+
31
+ :param left: Required. Coordinate of the left boundary.
32
+ :type left: float
33
+ :param top: Required. Coordinate of the top boundary.
34
+ :type top: float
35
+ :param width: Required. Width.
36
+ :type width: float
37
+ :param height: Required. Height.
38
+ :type height: float
39
+ """
40
+
41
+ def __init__ (self , left : float , top : float , width : float , height : float ) -> None :
42
+ self .left = left
43
+ self .top = top
44
+ self .width = width
45
+ self .height = height
46
+
47
+
48
+ class Prediction :
49
+ """Prediction result.
50
+
51
+ Variables are only populated by the server, and will be ignored when
52
+ sending a request.
53
+
54
+ :ivar probability: Probability of the tag.
55
+ :vartype probability: float
56
+ :ivar tag_id: Id of the predicted tag.
57
+ :vartype tag_id: str
58
+ :ivar tag_name: Name of the predicted tag.
59
+ :vartype tag_name: str
60
+ :ivar bounding_box: Bounding box of the prediction.
61
+ :vartype bounding_box:
62
+ ~circuitpython_customvision.prediction.models.BoundingBox
63
+ """
64
+
65
+ def __init__ (self , probability : float , tag_id : str , tag_name : str , bounding_box : BoundingBox ) -> None :
66
+ self .probability = probability
67
+ self .tag_id = tag_id
68
+ self .tag_name = tag_name
69
+ self .bounding_box = bounding_box
70
+
71
+
72
+ class ImagePrediction :
73
+ """Result of an image prediction request.
74
+
75
+ Variables are only populated by the server, and will be ignored when
76
+ sending a request.
77
+
78
+ :ivar id: Prediction Id.
79
+ :vartype id: str
80
+ :ivar project: Project Id.
81
+ :vartype project: str
82
+ :ivar iteration: Iteration Id.
83
+ :vartype iteration: str
84
+ :ivar created: Date this prediction was created.
85
+ :vartype created: datetime
86
+ :ivar predictions: List of predictions.
87
+ :vartype predictions:
88
+ list[~_customvision.prediction.models.Prediction]
89
+ """
90
+
91
+ def __init__ (self , response ) -> None :
92
+
93
+ if not isinstance (response , dict ):
94
+ response = json .loads (response )
95
+
96
+ self .prediction_id = response ["id" ]
97
+ self .project = response ["project" ]
98
+ self .iteration = response ["iteration" ]
99
+ self .created = response ["created" ]
100
+ self .predictions = []
101
+
102
+ for pred in response ["predictions" ]:
103
+ box = pred ["boundingBox" ]
104
+ bounding_box = BoundingBox (left = box ["left" ], top = box ["top" ], width = box ["width" ], height = box ["height" ])
105
+ prediction = Prediction (
106
+ probability = pred ["probability" ], tag_id = pred ["tagId" ], tag_name = pred ["tagName" ], bounding_box = bounding_box
107
+ )
108
+ self .predictions .append (prediction )
14
109
15
110
16
111
def __run_request (url , body , headers ):
@@ -23,7 +118,7 @@ def __run_request(url, body, headers):
23
118
r = requests .post (url , data = body , headers = headers )
24
119
25
120
if r .status_code != 200 :
26
- raise models . CustomVisionError (r .text )
121
+ raise CustomVisionError (r .text )
27
122
break
28
123
except RuntimeError as runtime_error :
29
124
print ("Could not send data, retrying after 5 seconds: " , runtime_error )
@@ -84,7 +179,7 @@ def __process_image_url(self, route: str, project_id: str, published_name: str,
84
179
result = __run_request (endpoint , body , headers )
85
180
result_text = result .text
86
181
87
- return models . ImagePrediction (result_text )
182
+ return ImagePrediction (result_text )
88
183
89
184
def __process_image (
90
185
self , route : str , project_id : str , published_name : str , image_data : bytearray , store : bool , application : Optional [str ]
@@ -96,7 +191,7 @@ def __process_image(
96
191
result = __run_request (endpoint , image_data , headers )
97
192
result_text = result .text
98
193
99
- return models . ImagePrediction (result_text )
194
+ return ImagePrediction (result_text )
100
195
101
196
def __classify_image_url (self , project_id : str , published_name : str , url : str , store : bool , application : Optional [str ]):
102
197
return self .__process_image_url (self .__classify_image_url_route , project_id , published_name , url , store , application )
0 commit comments