Google Cloud Vision for .NET 中的内容检测 nothing/hangs 应用

Detecting content in Google Cloud Vision for .NET does nothing/hangs app

我刚开始玩 Google Cloud Vision。我想检测图像中的文本。受官方文档启发(例如https://cloud.google.com/vision/docs/detecting-text and https://cloud.google.com/docs/authentication/production)我

我的代码如下所示:

using System;
using System.Windows;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Grpc.Auth;

//...

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Load an image from a local file.
    var image = Image.FromFile(@"C:\!\myimage.png");
    var credential = GoogleCredential.FromFile(@"C:\!\credentials.json");

    var channel = new Grpc.Core.Channel(@"https://vision.googleapis.com/v1/images:annotate",credential.ToChannelCredentials());

    var client = ImageAnnotatorClient.Create(channel);

    var response = client.DetectText(image); // <-- Nothing happens, app hangs, why?
    foreach (var annotation in response)
    {
        if (annotation.Description != null)
            Console.WriteLine(annotation.Description);
    }
}

//...

在单步执行代码时,应用挂起在 var response = client.DetectText(image);(无异常或任何情况)。如果我使用其他方法(例如 DetectLogos(image)DetectLabels(image)),也会发生同样的情况。在检查 CPU 使用情况和网络流量时,没有发生任何重要的事情(在相关代码行之前或之后)。

我做错了什么?

谢谢!

本教程帮助我成功地让 Vision API 正常工作。我什至尝试过使用标签、文本和面孔。您只需更新一些 JS 和 CS 即可使用您想要使用的任何视觉检测。

您需要更改的另一件事是上传图像而不是使用网络摄像头拍摄图像的选项,这并不难。

http://www.c-sharpcorner.com/article/using-google-vision-api-with-asp-net-mvc/

希望对您有所帮助。

提供的 url 目标似乎与 Vision REST API 有关,但您正在创建 GRPC 频道。您应该将目标更改为:

var channel = new Grpc.Core.Channel(@"http://vision.googleapis.com",credential.ToChannelCredentials());
var client = ImageAnnotatorClient.Create(channel);

或者:

var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.Host, credential.ToChannelCredentials());
var client = ImageAnnotatorClient.Create(channel);

端点信息可以在ImageAnnotatorClient class下找到。

希望对您有所帮助。

将路径传递给 .json 文件以实例化 gRPC 服务远比应该的要棘手。我们正在努力让它变得更容易。与此同时,这个样本是最相关的: https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/master/auth/AuthSample/Program.cs#L337

        var credential = GoogleCredential.FromFile(jsonPath)
            .CreateScoped(LanguageServiceClient.DefaultScopes);
        var channel = new Grpc.Core.Channel(
            LanguageServiceClient.DefaultEndpoint.ToString(),
            credential.ToChannelCredentials());
        var client = LanguageServiceClient.Create(channel);

它用于不同的 API,但它是正确的模式。