Skip to content

Commit 1f08a93

Browse files
authored
Merge pull request #37 from areddish/update-3.0
Updates for 3.0 SDK
2 parents 76a0b4a + c9a10c9 commit 1f08a93

File tree

5 files changed

+62
-26
lines changed

5 files changed

+62
-26
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ We provide several meta-packages to help you install several packages at a time.
104104
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute WebSearch tests.
105105
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).
106106
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).
107-
4. Set up the environment variable `CUSTOMVISION_TRAINING_KEY` with your key if you want to execute CustomVision Training tests.
108-
4. Set up the environment variable `CUSTOMVISION_PREDICTION_KEY` with your key if you want to execute CustomVision Prediction tests.
109-
4. Set up the environment variable `INK_RECOGNIZER_SUBSCRIPTION_KEY` with your key if you want to execute ink recognition tests.
107+
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.
108+
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.
109+
110110
111111
## Demo
112112

samples/vision/custom_vision_object_detection_sample.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
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

13-
# Add your Custom Vision endpoint to your environment variables.
14-
ENDPOINT = os.environ["CUSTOM_VISION_ENDPOINT"]
14+
PUBLISH_ITERATION_NAME = "classifyModel"
15+
16+
ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
1517

1618
# Add this directory to the path so that custom_vision_training_samples can be found
1719
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "."))
@@ -32,11 +34,16 @@ def run_sample(subscription_key):
3234

3335

3436
def train_project(training_key):
37+
try:
38+
prediction_resource_id = os.environ[PREDICTION_RESOURCE_ID_KEY_ENV_NAME]
39+
except KeyError:
40+
raise PredictionResourceMissingError("Didn't find a prediction resource to publish to. Please set the {} environment variable".format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME))
41+
3542
trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)
3643

3744
# Find the object detection domain
38-
obj_detection_domain = next(
39-
domain for domain in trainer.get_domains() if domain.type == "ObjectDetection")
45+
46+
obj_detection_domain = next(domain for domain in trainer.get_domains() if domain.type == "ObjectDetection" and domain.name == "General")
4047

4148
# Create a new project
4249
print("Creating project...")
@@ -115,19 +122,25 @@ def train_project(training_key):
115122
tagged_images_with_regions.append(ImageFileCreateEntry(
116123
name=file_name, contents=image_contents.read(), regions=regions))
117124

118-
trainer.create_images_from_files(
119-
project.id, images=tagged_images_with_regions)
125+
trainer.create_images_from_files(project.id, images=tagged_images_with_regions)
126+
127+
print ("Training...")
120128

121-
print("Training...")
122129
iteration = trainer.train_project(project.id)
123130
while (iteration.status != "Completed"):
124131
iteration = trainer.get_iteration(project.id, iteration.id)
125132
print("Training status: " + iteration.status)
126133
time.sleep(1)
127134

135+
136+
# The iteration is now trained. Name and publish this iteration to a prediciton endpoint
137+
trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id)
138+
print ("Done!")
139+
128140
# The iteration is now trained. Make it the default project endpoint
129141
trainer.update_iteration(project.id, iteration.id, is_default=True)
130142
print("Done!")
143+
131144
return project, iteration
132145

133146

@@ -136,12 +149,11 @@ def predict_project(prediction_key, project, iteration):
136149

137150
# Open the sample image and get back the prediction results.
138151
with open(os.path.join(IMAGES_FOLDER, "Test", "test_od_image.jpg"), mode="rb") as test_data:
139-
results = predictor.predict_image(project.id, test_data, iteration.id)
152+
results = predictor.detect_image(project.id, PUBLISH_ITERATION_NAME, test_data)
140153

141154
# Display the results.
142155
for prediction in results.predictions:
143-
print("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100), prediction.bounding_box.left,
144-
prediction.bounding_box.top, prediction.bounding_box.width, prediction.bounding_box.height)
156+
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))
145157

146158

147159
if __name__ == "__main__":

samples/vision/custom_vision_prediction_samples.py

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

10-
# Add your Custom Vision endpoint to your environment variables.
11-
ENDPOINT = os.environ['CUSTOM_VISION_ENDPOINT']
10+
PUBLISH_ITERATION_NAME = "classifyModel"
11+
12+
ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
1213

1314
# Add this directory to the path so that custom_vision_training_samples can be found
1415
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "."))
@@ -21,8 +22,7 @@ def find_or_train_project():
2122
try:
2223
training_key = os.environ[TRAINING_KEY_ENV_NAME]
2324
except KeyError:
24-
raise SubscriptionKeyError(
25-
"You need to set the {} env variable.".format(TRAINING_KEY_ENV_NAME))
25+
raise SubscriptionKeyError("You need to set the {} env variable.".format(TRAINING_KEY_ENV_NAME))
2626

2727
# Use the training API to find the SDK sample project created from the training example.
2828
from custom_vision_training_samples import train_project, SAMPLE_PROJECT_NAME
@@ -44,7 +44,7 @@ def predict_project(subscription_key):
4444
project = find_or_train_project()
4545

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

4949
# Display the results.
5050
for prediction in results.predictions:

samples/vision/custom_vision_training_multiclass_samples.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,28 @@
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

10-
# Add your Custom Vision endpoint to your environment variables.
11-
ENDPOINT = os.environ["CUSTOM_VISION_ENDPOINT"]
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+
17+
ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
1218

1319
IMAGES_FOLDER = os.path.join(os.path.dirname(
1420
os.path.realpath(__file__)), "images")
1521

22+
class PredictionResourceMissingError(Exception):
23+
pass
1624

1725
def train_project(subscription_key):
26+
try:
27+
prediction_resource_id = os.environ[PREDICTION_RESOURCE_ID_KEY_ENV_NAME]
28+
except KeyError:
29+
raise PredictionResourceMissingError("Didn't find a prediction resource to publish to. Please set the {} environment variable".format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME))
1830

1931
trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT)
2032

@@ -49,9 +61,10 @@ def train_project(subscription_key):
4961
print("Training status: " + iteration.status)
5062
time.sleep(1)
5163

52-
# The iteration is now trained. Make it the default project endpoint
53-
trainer.update_iteration(project.id, iteration.id, is_default=True)
54-
print("Done!")
64+
# The iteration is now trained. Name and publish this iteration to a prediciton endpoint
65+
trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id)
66+
print ("Done!")
67+
5568
return project
5669

5770

samples/vision/custom_vision_training_samples.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
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
# Add your Custom Vision endpoint to your environment variables.
1013
ENDPOINT = os.environ["CUSTOM_VISION_ENDPOINT"]
@@ -13,7 +16,14 @@
1316
os.path.realpath(__file__)), "images")
1417

1518

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

1828
trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT)
1929

@@ -45,9 +55,10 @@ def train_project(subscription_key):
4555
print("Training status: " + iteration.status)
4656
time.sleep(1)
4757

48-
# The iteration is now trained. Make it the default project endpoint
49-
trainer.update_iteration(project.id, iteration.id, is_default=True)
50-
print("Done!")
58+
# The iteration is now trained. Name and publish this iteration to a prediciton endpoint
59+
trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id)
60+
print ("Done!")
61+
5162
return project
5263

5364

0 commit comments

Comments
 (0)