Skip to content

Make samples work with azure-cognitiveservcies-vision-customvision 2.0.0 #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ azure-cognitiveservices-search-visualsearch>=0.2.0 # sample won't work with pre
azure-cognitiveservices-search-websearch
azure-cognitiveservices-vision-computervision>=0.3.0 # sample won't work with previous versions
azure-cognitiveservices-vision-contentmoderator>=1.0.0 # sample won't work with previous versions
azure-cognitiveservices-vision-customvision>=0.4.0 # sample won't work with previous versions
azure-cognitiveservices-vision-customvision>=2.0.0 # sample won't work with previous versions
azure-cognitiveservices-vision-face
azure-cognitiveservices-anomalydetector>=0.2.0 # sample won't work with previous versions
azure-cognitiveservices-inkrecognizer>=1.0.0b1
Expand Down
11 changes: 5 additions & 6 deletions samples/vision/custom_vision_object_detection_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from azure.cognitiveservices.vision.customvision.training import training_api
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateEntry, Region
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from msrest.authentication import ApiKeyCredentials

sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..", "..")))

Expand Down Expand Up @@ -41,7 +42,8 @@ def train_project(training_key):
except KeyError:
raise PredictionResourceMissingError("Didn't find a prediction resource to publish to. Please set the {} environment variable".format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME))

trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

# Find the object detection domain

Expand Down Expand Up @@ -139,15 +141,12 @@ def train_project(training_key):
trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id)
print ("Done!")

# The iteration is now trained. Make it the default project endpoint
trainer.update_iteration(project.id, iteration.id, is_default=True)
print("Done!")

return project, iteration


def predict_project(prediction_key, project, iteration):
predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT)
credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, credentials)

# Open the sample image and get back the prediction results.
with open(os.path.join(IMAGES_FOLDER, "Test", "test_od_image.jpg"), mode="rb") as test_data:
Expand Down
8 changes: 5 additions & 3 deletions samples/vision/custom_vision_prediction_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from msrest.authentication import ApiKeyCredentials

TRAINING_KEY_ENV_NAME = "CUSTOMVISION_TRAINING_KEY"
SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_KEY"
Expand All @@ -26,7 +27,8 @@ def find_or_train_project():

# Use the training API to find the SDK sample project created from the training example.
from custom_vision_training_samples import train_project, SAMPLE_PROJECT_NAME
trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

for proj in trainer.get_projects():
if (proj.name == SAMPLE_PROJECT_NAME):
Expand All @@ -37,8 +39,8 @@ def find_or_train_project():


def predict_project(subscription_key):
predictor = CustomVisionPredictionClient(
subscription_key, endpoint=ENDPOINT)
credentials = ApiKeyCredentials(in_headers={"Prediction-key": subscription_key})
predictor = CustomVisionPredictionClient(ENDPOINT, credentials)

# Find or train a new project to use for prediction.
project = find_or_train_project()
Expand Down
44 changes: 17 additions & 27 deletions samples/vision/custom_vision_training_multiclass_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,64 @@

from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.training.models import Classifier
from msrest.authentication import ApiKeyCredentials

SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMVISION_TRAINING_KEY"
PREDICTION_RESOURCE_ID_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_ID"

SAMPLE_PROJECT_NAME = "Python SDK Sample"

# The prediction resource can be found with your keys and is tied to the Prediction Key
PREDICTION_RESOURCE_ID = "enter your prediction resource"
PREDICTION_RESOURCE_ID_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_ID"

PUBLISH_ITERATION_NAME = "classifyModel"

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

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

class PredictionResourceMissingError(Exception):
pass
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images")

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

trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT)
credentials = ApiKeyCredentials(in_headers={"Training-key": subscription_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

# Create a new project
print("Creating project...")
project = trainer.create_project(
SAMPLE_PROJECT_NAME, classification_type=Classifier.multiclass)
print ("Creating project...")
project = trainer.create_project(SAMPLE_PROJECT_NAME, classification_type=Classifier.multiclass)

# Make two tags in the new project
hemlock_tag = trainer.create_tag(project.id, "Hemlock")
cherry_tag = trainer.create_tag(project.id, "Japanese Cherry")
pine_needle_tag = trainer.create_tag(project.id, "Pine Needle Leaves")
pine_needle_tag = trainer.create_tag(project.id, "Pine Needle Leaves")
flat_leaf_tag = trainer.create_tag(project.id, "Flat Leaves")

print("Adding images...")
print ("Adding images...")
hemlock_dir = os.path.join(IMAGES_FOLDER, "Hemlock")
for image in os.listdir(hemlock_dir):
with open(os.path.join(hemlock_dir, image), mode="rb") as img_data:
trainer.create_images_from_data(project.id, img_data.read(), [
hemlock_tag.id, pine_needle_tag.id])
with open(os.path.join(hemlock_dir, image), mode="rb") as img_data:
trainer.create_images_from_data(project.id, img_data.read(), [ hemlock_tag.id, pine_needle_tag.id ])

cherry_dir = os.path.join(IMAGES_FOLDER, "Japanese Cherry")
for image in os.listdir(cherry_dir):
with open(os.path.join(cherry_dir, image), mode="rb") as img_data:
trainer.create_images_from_data(project.id, img_data.read(), [
cherry_tag.id, flat_leaf_tag.id])
with open(os.path.join(cherry_dir, image), mode="rb") as img_data:
trainer.create_images_from_data(project.id, img_data.read(), [ cherry_tag.id, flat_leaf_tag.id ])

print("Training...")
print ("Training...")
iteration = trainer.train_project(project.id)
while (iteration.status == "Training"):
iteration = trainer.get_iteration(project.id, iteration.id)
print("Training status: " + iteration.status)
print ("Training status: " + iteration.status)
time.sleep(1)

# The iteration is now trained. Name and publish this iteration to a prediciton endpoint
trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id)
print ("Done!")

return project


if __name__ == "__main__":
import sys
import os.path
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
from tools import execute_samples
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
4 changes: 3 additions & 1 deletion samples/vision/custom_vision_training_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time

from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from msrest.authentication import ApiKeyCredentials

SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMVISION_TRAINING_KEY"
PREDICTION_RESOURCE_ID_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_ID"
Expand All @@ -25,7 +26,8 @@ def train_project(subscription_key):
except KeyError:
raise PredictionResourceMissingError("Didn't find a prediction resource to publish to. Please set the {} environment variable".format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME))

trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT)
credentials = ApiKeyCredentials(in_headers={"Training-key": subscription_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

# Create a new project
print("Creating project...")
Expand Down