如果仅仅用来预测,可以使用如下函数。

 

桌面端的预测程序

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 创建图像分类项目

https://docs.microsoft.com/zh-cn/azure/cognitive-services/Custom-Vision-Service/quickstarts/image-classification?tabs=visual-studio&pivots=programming-language-python

 

微软MVP精选 | 1小时快速搭建基于Azure Custom Vision和树莓派的鸟类识别应用

https://mp.weixin.qq.com/s/VaM89abLmUnhpxI3o_TPHQ

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐