Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (50 sloc) 2.4 KB
# This code wil pass an image to a trained custom vision service model for a prediction
# This code was created using examples from
# Python SDK Samples
# Pre-requisites
# Trained Custom Vision Service model created using either the GUI at OR
# Using code such as
# Install Custom Vision Service SDK
# pip install azure-cognitiveservices-vision-customvision
from import CustomVisionTrainingClient
from import CustomVisionPredictionClient
import os
# Set these variables based on the settings of your trained project
# You can look up the endpoints and keys by going to and bringing up settings (the gear image in the top menu)
YOUR_PROJECT_NAME = "ElephantNoElephant"
training_endpoint = ""
training_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
prediction_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# function to fetch project object from Custom Vision Service
def find_project():
print("Get trainer")
trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)
print("get project")
for proj in trainer.get_projects():
return proj
except Exception as e:
print("Set path for folder containign image to predict")
IMAGES_FOLDER = os.path.dirname(os.path.realpath(__file__))
# print(str(IMAGES_FOLDER))
# print(str(os.path.join(IMAGES_FOLDER, "ElephantGiraffeTestImages", "testelephant2.jpg")))
# Get predictor and project objects
predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT)
project = find_project()
print("Make prediction")
with open(os.path.join(IMAGES_FOLDER, "ElephantGiraffeTestImages", "testgiraffe1.jpg"), mode="rb") as test_data:
results = predictor.predict_image(,
except Exception as e:
# Display the results.
for prediction in results.predictions:
print(prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100))
You can’t perform that action at this time.