Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 在表单中使用VisionServiceClient时出现各种错误_Xamarin_Xamarin.forms_Azure Cognitive Services_Microsoft Custom Vision - Fatal编程技术网

Xamarin 在表单中使用VisionServiceClient时出现各种错误

Xamarin 在表单中使用VisionServiceClient时出现各种错误,xamarin,xamarin.forms,azure-cognitive-services,microsoft-custom-vision,Xamarin,Xamarin.forms,Azure Cognitive Services,Microsoft Custom Vision,我正在尝试创建一个简单的Xamarin表单应用程序,允许用户浏览或拍照,并让azure认知服务使用自定义视觉模型标记照片 我无法让客户端根据VisionServiceClient生成的异常中的错误消息成功进行身份验证或找到资源。我错过什么了吗?VisionServiceClient的参数使用的正确值是什么 所有键都已从下图中删除,它们已填充 VS2017中引发异常: System.Private.CoreLib.dll中的“Microsoft.ProjectOxford.Vision.Clien

我正在尝试创建一个简单的Xamarin表单应用程序,允许用户浏览或拍照,并让azure认知服务使用自定义视觉模型标记照片

我无法让客户端根据VisionServiceClient生成的异常中的错误消息成功进行身份验证或找到资源。我错过什么了吗?VisionServiceClient的参数使用的正确值是什么

所有键都已从下图中删除,它们已填充

VS2017中引发异常:

System.Private.CoreLib.dll中的“Microsoft.ProjectOxford.Vision.ClientException”

调用VisionServiceClient:

private const string endpoint = @"https://eastus2.api.cognitive.microsoft.com/vision/prediction/v1.0";
private const string key = "";

VisionServiceClient visionClient = new VisionServiceClient(key, endpoint);
VisualFeature[] features = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
try
{
     AnalysisResult temp = await visionClient.AnalyzeImageAsync(imageStream,                    
     features.ToList(), null);

     return temp;
}
catch(Exception ex)
{
     return null;
}
VS异常错误:

Azure认知服务门户:

自定义Vision门户:


看起来您混淆了计算机视觉和自定义视觉API。您正试图使用后者的API密钥为前者使用客户端SDK

对于.NET语言,您需要NuGet包

您的代码最终将显示如下所示:

ICustomVisionPredictionClient client = new CustomVisionPredictionClient()
{
    ApiKey = PredictionKey,
    Endpoint = "https://southcentralus.api.cognitive.microsoft.com"
};
ImagePrediction prediction = await client.PredictImageAsync(ProjectId, stream, IterationId);

感谢CTHRAH提供的长期帮助,并在聊天中与我交谈。通过他的帖子和一些小故障排除,我已经找到了适合我的方法。代码是超级笨重,但它只是为了测试和确保我能够做到这一点。要回答这个问题:

Nuget软件包和类

通过使用cthrah的帖子,我能够安装培训和预测nuget软件包,这两个软件包对于这个特定的应用程序都是正确的。我需要以下课程:

Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction
Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models
Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training
Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models

端点根

按照一些步骤,我确定端点URL只需要是根,而不是自定义Vision门户中提供的完整URL。比如说,

改为

我使用了自定义Vision门户中的键和端点,并且进行了更改,我能够使用培训和预测客户机来拉动项目和迭代

获取项目Id

要使用
CustomVisionPredictionClient.PredictImageAsync
,您需要项目id的
Guid
,如果门户中未设置默认迭代,则需要迭代id

我测试了两种获取项目id的方法

使用门户中的项目id字符串

  • 从“项目设置”下的门户获取项目id字符串
  • 对于
    PredictImageAsync
    pass的第一个参数
    Guid.Parse(projectId)

  • 使用培训客户端

  • 创建新的
    CustomVisionTrainingClient
  • 要获取
    的列表,请使用

    TrainingClient.GetProjects().ToList()

  • 在我的例子中,我只有一个项目,所以我只需要第一个元素

    Guid projectId=projects[0]。Id

  • 获取迭代Id

    要获取项目的迭代id,您需要
    CustomVisionTrainingClient

  • 创建客户端
  • 要获取
    的列表,请使用
    client.GetIterations(projectId).ToList()

  • 在我的例子中,我只有一个迭代,所以我只需要第一个元素。
    Guid iterationId=iterations[0]。Id

  • 我现在能够使用我的模型对图像进行分类。在下面的代码中,fileStream是传递给模型的图像流

    public async Task<string> Predict(Stream fileStream)
    {
        string projectId = "";
        //string trainingEndpoint = "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.2/Training/";
        string trainingEndpoint = "https://southcentralus.api.cognitive.microsoft.com/";
        string trainingKey = "";
        //string predictionEndpoint = "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/";
        string predictionEndpoint = "https://southcentralus.api.cognitive.microsoft.com";
        string predictionKey = "";
    
        CustomVisionTrainingClient trainingClient = new CustomVisionTrainingClient
        {
            ApiKey = trainingKey,
            Endpoint = trainingEndpoint
        };
    
        List<Project> projects = new List<Project>();
    
        try
        {
            projects = trainingClient.GetProjects().ToList();
        }
        catch(Exception ex)
        {
            Debug.WriteLine("Unable to get projects:\n\n" + ex.Message);
            return "Unable to obtain projects.";
        }
    
        Guid ProjectId = Guid.Empty;
    
        if(projects.Count > 0)
        {
            ProjectId = projects[0].Id;
        }
    
        if (ProjectId == Guid.Empty)
        {
            Debug.WriteLine("Unable to obtain project ID");
            return "Unable to obtain project id.";
        }
    
        List<Iteration> iterations = new List<Iteration>();
    
        try
        {
            iterations = trainingClient.GetIterations(ProjectId).ToList();
        }
        catch(Exception ex)
        {
            Debug.WriteLine("Unable to obtain iterations.");
            return "Unable to obtain iterations.";
        }
    
        foreach(Iteration itr in iterations)
        {
            Debug.WriteLine(itr.Name + "\t" + itr.Id + "\n");
        }
    
        Guid iteration = Guid.Empty;
    
        if(iterations.Count > 0)
        {
            iteration = iterations[0].Id;
        }
    
        if(iteration == Guid.Empty)
        {
            Debug.WriteLine("Unable to obtain project iteration.");
            return "Unable to obtain project iteration";
        }
    
        CustomVisionPredictionClient predictionClient = new CustomVisionPredictionClient
        {
            ApiKey = predictionKey,
            Endpoint = predictionEndpoint
        };
    
        var result = await predictionClient.PredictImageAsync(Guid.Parse(projectId), fileStream, iteration);
    
        string resultStr = string.Empty;
    
        foreach(PredictionModel pred in result.Predictions)
        {
            if(pred.Probability >= 0.85)
                resultStr += pred.TagName + " ";
        }
    
        return resultStr;
    }
    
    公共异步任务预测(流文件流)
    {
    字符串projectd=“”;
    //字符串trainingEndpoint=”https://southcentralus.api.cognitive.microsoft.com/customvision/v2.2/Training/";
    字符串trainingEndpoint=”https://southcentralus.api.cognitive.microsoft.com/";
    字符串trainingKey=“”;
    //字符串predictionEndpoint=”https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/";
    字符串predictionEndpoint=”https://southcentralus.api.cognitive.microsoft.com";
    字符串predictionKey=“”;
    CustomVisionTrainingClient trainingClient=新CustomVisionTrainingClient
    {
    ApiKey=培训钥匙,
    端点=训练点
    };
    列表项目=新列表();
    尝试
    {
    projects=trainingClient.GetProjects().ToList();
    }
    捕获(例外情况除外)
    {
    Debug.WriteLine(“无法获取项目:\n\n”+ex.Message);
    返回“无法获取项目”;
    }
    Guid ProjectId=Guid.Empty;
    如果(projects.Count>0)
    {
    ProjectId=projects[0].Id;
    }
    if(ProjectId==Guid.Empty)
    {
    Debug.WriteLine(“无法获取项目ID”);
    返回“无法获取项目id”;
    }
    列表迭代次数=新列表();
    尝试
    {
    迭代次数=trainingClient.GetIterations(ProjectId.ToList();
    }
    捕获(例外情况除外)
    {
    Debug.WriteLine(“无法获取迭代”);
    返回“无法获得迭代。”;
    }
    foreach(迭代中的迭代itr)
    {
    Debug.WriteLine(itr.Name+“\t”+itr.Id+“\n”);
    }
    Guid迭代=Guid.Empty;
    如果(迭代次数.计数>0)
    {
    迭代次数=迭代次数[0]。Id;
    }
    if(迭代==Guid.Empty)
    {
    Debug.WriteLine(“无法获取项目迭代”);
    返回“无法获取项目迭代”;
    }
    CustomVisionPredictionClient predictionClient=新的CustomVisionPredictionClient
    {
    ApiKey=predictionKey,
    端点=预测端点
    };
    var结果