<
'''
Code taken from https://github.com/Azure-Samples/cognitive-services-quickstart-code/blob/master/python/CustomVision/ImageClassification/CustomVisionQuickstart.py
Using instructions posted at;- https://docs.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/quickstarts/image-classification?tabs=visual-studio&pivots=programming-language-python
Note;- both the above contain errors !
'''
<snippet_imports>
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry, Region
from msrest.authentication import ApiKeyCredentials
import time
</snippet_imports>
<snippet_creds>
Replace with valid values
ENDPOINT ="https://resource-group-name.cognitiveservices.azure.com/"
training_key = "3589b< deleted from this >f5a8b95"
prediction_key = "c229a2c0e82b4f"
prediction_resource_id = "0261b07a074339" This value was copied from the subscription id , on the overview blade of the prediction resource
this does NOT work
prediction_resource_id="/subscriptions/0261b17d74339/resourceGroups/vision_group/providers/Microsoft.CognitiveServices/accounts/vicegrou-Prediction"
taken from MicrosoftDocs/azure-docs#28445 THIS SEEMS TO WORK
</snippet_creds>
<snippet_auth>
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)
</snippet_auth>
<snippet_create>
publish_iteration_name = "classifyModel"
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)
Create a new project
print ("Creating project...")
project = trainer.create_project("MyProject")
</snippet_create>
<snippet_tags>
Make two tags in the new project
hemlock_tag = trainer.create_tag(project.id, "Hemlock")
cherry_tag = trainer.create_tag(project.id, "Japanese Cherry")
</snippet_tags>
<snippet_upload>
base_image_location = "/cognitive-services-python-sdk-samples/samples/vision/"
base_image_location = "/"
print("Adding images...")
image_list = []
for image_num in range(1, 11):
file_name = "hemlock_{}.jpg".format(image_num)
with open(base_image_location + "images/Hemlock/" + file_name, "rb") as image_contents:
image_list.append(ImageFileCreateEntry(name=file_name, contents=image_contents.read(), tag_ids=[hemlock_tag.id]))
for image_num in range(1, 11):
file_name = "japanese_cherry_{}.jpg".format(image_num)
with open(base_image_location + "images/Japanese Cherry/" + file_name, "rb") as image_contents:
image_list.append(ImageFileCreateEntry(name=file_name, contents=image_contents.read(), tag_ids=[cherry_tag.id]))
upload_result = trainer.create_images_from_files(project.id, ImageFileCreateBatch(images=image_list))
if not upload_result.is_batch_successful:
print("Image batch upload failed.")
for image in upload_result.images:
print("Image status: ", image.status)
exit(-1)
</snippet_upload>
<snippet_train>
print ("Training...")
iteration = trainer.train_project(project.id)
while (iteration.status != "Completed"):
iteration = trainer.get_iteration(project.id, iteration.id)
print ("Training status: " + iteration.status)
time.sleep(1)
The iteration is now trained. Publish it to the project endpoint
trainer.publish_iteration(project.id, iteration.id, publish_iteration_name, prediction_resource_id)
print ("Done!")
'''
THIS IS A DUPLICATE AND CAUSES AN ERROR
The iteration is now trained. Publish it to the project endpoint
trainer.publish_iteration(project.id, iteration.id, publish_iteration_name, prediction_resource_id)
print ("Done!")
'''
Now there is a trained endpoint that can be used to make a prediction
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)
with open(base_image_location + "images/Test/test_image_2.jpg", "rb") as image_contents:
results = predictor.classify_image(
project.id, publish_iteration_name, image_contents.read())
The above line does NOT work. It causes ... Invalid iteration error !!
Display the results.
for prediction in results.predictions:
print("\t" + prediction.tag_name +
": {0:.2f}%".format(prediction.probability * 100))
</snippet_test>
This issue is for a: (mark with an x
)
- [X ] bug report -> please search issues before submitting
- [ ] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)
Minimal steps to reproduce
using Visual Studio 2019 and using python version 3.7, on a windows 10 laptop interfacing to Azure custom vision.
Using the set of example image files provided.
Ran the code in visual studio de-bug mode.
Any log messages given by the failure
The line results = predictor.classify_image(project.id, publish_iteration_name, image_contents.read())
causes the reply;- invalid iteration.
Expected/desired behavior
It should run and send back the prediction results
OS and Version?
Windows 10
Versions
Mention any other details that might be useful
there is also duplicate lines of code ( a different problem ) see the comments in the above code.
Thanks! We'll be in touch soon.