如果仅仅用来预测,可以使用如下函数。
桌面端的预测程序
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 os, time, uuid
'''
ENDPOINT = "PASTE_YOUR_CUSTOM_VISION_TRAINING_ENDPOINT_HERE"
training_key = "PASTE_YOUR_CUSTOM_VISION_TRAINING_SUBSCRIPTION_KEY_HERE"
prediction_key = "PASTE_YOUR_CUSTOM_VISION_PREDICTION_SUBSCRIPTION_KEY_HERE"
prediction_resource_id = "PASTE_YOUR_CUSTOM_VISION_PREDICTION_RESOURCE_ID_HERE"
'''
# Replace with valid values
ENDPOINT = "https://birdcustom0609.cognitiveservices.azure.com/"
training_key = "c9192ddfe8d8411d8bfaa72ae1e4741e"
prediction_key = "c58af1aa063b455b892836b2dbae9d27"
prediction_resource_id = "birdcustom0609-prediction"
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)
publish_iteration_name = "Iteration1"
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)
base_image_location = os.path.join (os.path.dirname(__file__), "Images")
projectID = "0568b68f-5b92-43e7-9449-741bcb343c12"
# 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(os.path.join (base_image_location, "Test/test_image.jpg"), "rb") as image_contents:BX7vjSt8KMtcBHyisvcSPK-1200-80.jpg
with open(os.path.join (base_image_location, "Test/BX7vjSt8KMtcBHyisvcSPK-1200-80.jpg"), "rb") as image_contents:
results = predictor.classify_image(
projectID, publish_iteration_name, image_contents.read())
# Display the results.
for prediction in results.predictions:
print("\t" + prediction.tag_name +
": {0:.2f}%".format(prediction.probability * 100))
相关材料:
使用自定义视觉对濒危鸟类进行分类
https://docs.microsoft.com/zh-cn/learn/modules/cv-classify-bird-species/5-deploy-model
快速入门:使用自定义视觉客户端库或 REST API 创建图像分类项目
微软MVP精选 | 1小时快速搭建基于Azure Custom Vision和树莓派的鸟类识别应用
https://mp.weixin.qq.com/s/VaM89abLmUnhpxI3o_TPHQ