Skip to content

Commit 4730cca

Browse files
committed
Updates for 3.0 SDK
1 parent 114f6f9 commit 4730cca

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ We provide several meta-packages to help you install several packages at a time.
9898
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute WebSearch tests.
9999
4. Set up the environment variable `COMPUTERVISION_SUBSCRIPTION_KEY` with your key if you want to execute Computer Vision tests. You might override too `COMPUTERVISION_LOCATION` (westcentralus by default).
100100
4. Set up the environment variable `CONTENTMODERATOR_SUBSCRIPTION_KEY` with your key if you want to execute Content Moderator tests. You might override too `CONTENTMODERATOR_LOCATION` (westcentralus by default).
101-
4. Set up the environment variable `CUSTOMVISION_TRAINING_KEY` with your key if you want to execute CustomVision Training tests.
102-
4. Set up the environment variable `CUSTOMVISION_PREDICTION_KEY` with your key if you want to execute CustomVision Prediction tests.
101+
4. Set up the environment variable `CUSTOMVISION_TRAINING_KEY` with your key and `CUSTOMVISION_PREDICTION_ID` with a valid prediction resource id if you want to execute CustomVision Training tests.
102+
4. Set up the environment variable `CUSTOMVISION_PREDICTION_KEY` with your key and `CUSTOMVISION_PREDICTION_ID` with a valid prediction resource id if you want to execute CustomVision Prediction tests.
103103
104104
## Demo
105105

samples/vision/custom_vision_object_detection_sample.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
# Replace with a valid key
1010
SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMVISION_TRAINING_KEY"
11+
PREDICTION_RESOURCE_ID_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_ID"
1112
PREDICTION_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_KEY"
1213

14+
PUBLISH_ITERATION_NAME = "classifyModel"
15+
1316
ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
1417

1518
# Add this directory to the path so that custom_vision_training_samples can be found
@@ -27,10 +30,15 @@ def run_sample(subscription_key):
2730
predict_project(prediction_key, project, iteration)
2831

2932
def train_project(training_key):
33+
try:
34+
prediction_resource_id = os.environ[PREDICTION_RESOURCE_ID_KEY_ENV_NAME]
35+
except KeyError:
36+
raise PredictionResourceMissingError("Didn't find a prediction resource to publish to. Please set the {} environment variable".format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME))
37+
3038
trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)
3139

3240
# Find the object detection domain
33-
obj_detection_domain = next(domain for domain in trainer.get_domains() if domain.type == "ObjectDetection")
41+
obj_detection_domain = next(domain for domain in trainer.get_domains() if domain.type == "ObjectDetection" and domain.name == "General")
3442

3543
# Create a new project
3644
print ("Creating project...")
@@ -104,7 +112,6 @@ def train_project(training_key):
104112
with open(os.path.join(IMAGES_FOLDER,"scissors", file_name + ".jpg"), mode="rb") as image_contents:
105113
tagged_images_with_regions.append(ImageFileCreateEntry(name=file_name, contents=image_contents.read(), regions=regions))
106114

107-
108115
trainer.create_images_from_files(project.id, images=tagged_images_with_regions)
109116

110117
print ("Training...")
@@ -114,8 +121,8 @@ def train_project(training_key):
114121
print ("Training status: " + iteration.status)
115122
time.sleep(1)
116123

117-
# The iteration is now trained. Make it the default project endpoint
118-
trainer.update_iteration(project.id, iteration.id, is_default=True)
124+
# The iteration is now trained. Name and publish this iteration to a prediciton endpoint
125+
trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id)
119126
print ("Done!")
120127
return project, iteration
121128

@@ -124,11 +131,11 @@ def predict_project(prediction_key, project, iteration):
124131

125132
# Open the sample image and get back the prediction results.
126133
with open(os.path.join(IMAGES_FOLDER, "Test", "test_od_image.jpg"), mode="rb") as test_data:
127-
results = predictor.predict_image(project.id, test_data, iteration.id)
134+
results = predictor.detect_image(project.id, PUBLISH_ITERATION_NAME, test_data)
128135

129136
# Display the results.
130137
for prediction in results.predictions:
131-
print ("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100), prediction.bounding_box.left, prediction.bounding_box.top, prediction.bounding_box.width, prediction.bounding_box.height)
138+
print("\t" + prediction.tag_name + ": {0:.2f}% bbox.left = {1:.2f}, bbox.top = {2:.2f}, bbox.width = {3:.2f}, bbox.height = {4:.2f}".format(prediction.probability * 100, prediction.bounding_box.left, prediction.bounding_box.top, prediction.bounding_box.width, prediction.bounding_box.height))
132139

133140

134141
if __name__ == "__main__":

samples/vision/custom_vision_prediction_samples.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
TRAINING_KEY_ENV_NAME = "CUSTOMVISION_TRAINING_KEY"
88
SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_KEY"
99

10+
PUBLISH_ITERATION_NAME = "classifyModel"
11+
1012
ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
1113

1214
# Add this directory to the path so that custom_vision_training_samples can be found
@@ -19,7 +21,7 @@ def find_or_train_project():
1921
training_key = os.environ[TRAINING_KEY_ENV_NAME]
2022
except KeyError:
2123
raise SubscriptionKeyError("You need to set the {} env variable.".format(TRAINING_KEY_ENV_NAME))
22-
24+
2325
# Use the training API to find the SDK sample project created from the training example.
2426
from custom_vision_training_samples import train_project, SAMPLE_PROJECT_NAME
2527
trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)
@@ -38,7 +40,7 @@ def predict_project(subscription_key):
3840
project = find_or_train_project()
3941

4042
with open(os.path.join(IMAGES_FOLDER, "Test", "test_image.jpg"), mode="rb") as test_data:
41-
results = predictor.predict_image(project.id, test_data.read())
43+
results = predictor.classify_image(project.id, PUBLISH_ITERATION_NAME, test_data.read())
4244

4345
# Display the results.
4446
for prediction in results.predictions:

samples/vision/custom_vision_training_multiclass_samples.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,27 @@
55
from azure.cognitiveservices.vision.customvision.training.models import Classifier
66

77
SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMVISION_TRAINING_KEY"
8+
PREDICTION_RESOURCE_ID_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_ID"
9+
810
SAMPLE_PROJECT_NAME = "Python SDK Sample"
911

12+
# The prediction resource can be found with your keys and is tied to the Prediction Key
13+
PREDICTION_RESOURCE_ID = "enter your prediction resource"
14+
15+
PUBLISH_ITERATION_NAME = "classifyModel"
16+
1017
ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
1118

1219
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images")
1320

21+
class PredictionResourceMissingError(Exception):
22+
pass
23+
1424
def train_project(subscription_key):
25+
try:
26+
prediction_resource_id = os.environ[PREDICTION_RESOURCE_ID_KEY_ENV_NAME]
27+
except KeyError:
28+
raise PredictionResourceMissingError("Didn't find a prediction resource to publish to. Please set the {} environment variable".format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME))
1529

1630
trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT)
1731

@@ -43,8 +57,8 @@ def train_project(subscription_key):
4357
print ("Training status: " + iteration.status)
4458
time.sleep(1)
4559

46-
# The iteration is now trained. Make it the default project endpoint
47-
trainer.update_iteration(project.id, iteration.id, is_default=True)
60+
# The iteration is now trained. Name and publish this iteration to a prediciton endpoint
61+
trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id)
4862
print ("Done!")
4963
return project
5064

samples/vision/custom_vision_training_samples.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
55

66
SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMVISION_TRAINING_KEY"
7+
PREDICTION_RESOURCE_ID_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_ID"
8+
79
SAMPLE_PROJECT_NAME = "Python SDK Sample"
10+
PUBLISH_ITERATION_NAME = "classifyModel"
811

912
ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
1013

1114
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images")
1215

16+
class PredictionResourceMissingError(Exception):
17+
pass
18+
1319
def train_project(subscription_key):
20+
try:
21+
prediction_resource_id = os.environ[PREDICTION_RESOURCE_ID_KEY_ENV_NAME]
22+
except KeyError:
23+
raise PredictionResourceMissingError("Didn't find a prediction resource to publish to. Please set the {} environment variable".format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME))
1424

1525
trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT)
1626

@@ -40,8 +50,8 @@ def train_project(subscription_key):
4050
print ("Training status: " + iteration.status)
4151
time.sleep(1)
4252

43-
# The iteration is now trained. Make it the default project endpoint
44-
trainer.update_iteration(project.id, iteration.id, is_default=True)
53+
# The iteration is now trained. Name and publish this iteration to a prediciton endpoint
54+
trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id)
4555
print ("Done!")
4656
return project
4757

0 commit comments

Comments
 (0)