Detecting content in Google Cloud Vision for .NET

2019-08-18 18:40发布

问题:

I just started playing around with Google Cloud Vision a bit. I wanted to detect text in an image. Inspired by the official docs (e.g. https://cloud.google.com/vision/docs/detecting-text and https://cloud.google.com/docs/authentication/production) I

  • created a new project,
  • attached the Vision API to it,
  • created a service account and downloaded the credentials/key-JSON file,
  • set up an VS project and got all relevant packages from NuGET.

My code looks like this:

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);
    }
}

//...

While stepping through the code, the app hangs at var response = client.DetectText(image); (no exception or anything). The same happens, if I use other methods (e.g. DetectLogos(image) or DetectLabels(image)). When checking CPU usage and network traffic nothing important happens (before or after the relevant line of code).

What am I doing wrong here?

Thanks!

回答1:

The url target provided seems to be related to Vision REST API but you are creating a GRPC channel. You should change the target to:

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

Or:

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

The endpoint information can be found under the ImageAnnotatorClient class.

Hope this is helpful.



回答2:

This tutorial helped me successfully get Vision API to work correctly. I even tried it with Labels, Text and Faces. You just have to update some of the JS along with the CS to work with whatever Vision Detection you want to use.

The other thing you'll need to change is the option to upload an image rather than take one with the webcam, which isn't too difficult.

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

Hope it helps.



回答3:

It's far trickier than it should be to pass a path to a .json file to instantiate a gRPC service. We're working on making it easier. In the meantime, this sample is most relevant: 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);

It's for a different API, but is the correct pattern.