Skip to content

Commit 5509880

Browse files
author
Jim Bennett
committed
Moving to a single file
1 parent a9db7c3 commit 5509880

10 files changed

+101
-223
lines changed
Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,103 @@
99
import time
1010
from typing import Optional
1111
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)
14109

15110

16111
def __run_request(url, body, headers):
@@ -23,7 +118,7 @@ def __run_request(url, body, headers):
23118
r = requests.post(url, data=body, headers=headers)
24119

25120
if r.status_code != 200:
26-
raise models.CustomVisionError(r.text)
121+
raise CustomVisionError(r.text)
27122
break
28123
except RuntimeError as runtime_error:
29124
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,
84179
result = __run_request(endpoint, body, headers)
85180
result_text = result.text
86181

87-
return models.ImagePrediction(result_text)
182+
return ImagePrediction(result_text)
88183

89184
def __process_image(
90185
self, route: str, project_id: str, published_name: str, image_data: bytearray, store: bool, application: Optional[str]
@@ -96,7 +191,7 @@ def __process_image(
96191
result = __run_request(endpoint, image_data, headers)
97192
result_text = result.text
98193

99-
return models.ImagePrediction(result_text)
194+
return ImagePrediction(result_text)
100195

101196
def __classify_image_url(self, project_id: str, published_name: str, url: str, store: bool, application: Optional[str]):
102197
return self.__process_image_url(self.__classify_image_url_route, project_id, published_name, url, store, application)

circuitpython_customvision/__init__.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

circuitpython_customvision/prediction/__init__.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

circuitpython_customvision/prediction/models/__init__.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

circuitpython_customvision/prediction/models/bounding_box.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

circuitpython_customvision/prediction/models/customvision_error.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

circuitpython_customvision/prediction/models/image_prediction.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

circuitpython_customvision/prediction/models/prediction.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

circuitpython_customvision/prediction/version.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/circuitpython_customvision_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from circuitpython_customvision.prediction.custom_vision_prediction_client import CustomVisionPredictionClient
1+
from circuitpython_customvision import CustomVisionPredictionClient
22

33
client = CustomVisionPredictionClient("api_key", "endpoint")
44

0 commit comments

Comments
 (0)