Google Cloud Platform provides a wide range of APIs, one of which is Cloud Vision API that allows you to detect faces in images, extract sentiments, detect landmark, OCR and etc.
One of available annotators is “Logo Detection” that allows you to find company logo in your image and recognize it.
.NET is not the part of mainstream Google Cloud SDK. Google maintains google-api-dotnet-client that should allow you to authenticate to and call all available services. API design looks slightly not intuitive for .NET world (at least from my point of view).
I spent some time on Google/SO/Github trying to understand how to use OAuth2 in server-to-server authentication scenario with ServiceAccount.json file generated by Google API Manager.
You cannot use this API without billing account, so you have to put your credit card info, if you want to play with this API.
Also, note that you need to have two NuGet packages Google.Apis.Vision.v1 & Google.Apis.Oauth2.v2 (and a lot of their dependencies)
So, here is the full sample:
#I __SOURCE_DIRECTORY__ #load "Scripts/load-references-debug.fsx" open System.IO open Google.Apis.Auth.OAuth2 open Google.Apis.Services open Google.Apis.Vision.v1 open Google.Apis.Vision.v1.Data // OAuth2 authentication using service account JSON file let credentials = let jsonServiceAccount = @"d:\ServiceAccount.json" use stream = new FileStream(jsonServiceAccount, FileMode.Open, FileAccess.Read) GoogleCredential.FromStream(stream) .CreateScoped(VisionService.Scope.CloudPlatform) let visionService = // Google Cloud Vision Service BaseClientService.Initializer( ApplicationName = "my-cool-app", HttpClientInitializer = credentials) |> VisionService // Logo detection request for one image let createRequest content = let wrap (xs:'a list) = System.Collections.Generic.List(xs) BatchAnnotateImagesRequest( Requests = wrap [AnnotateImageRequest( Features = wrap [Feature(Type = "LOGO_DETECTION")], Image = Image(Content = content)) ]) |> visionService.Images.Annotate let call fileName = // Call and interpret results let request = File.ReadAllBytes fileName |> System.Convert.ToBase64String |> createRequest let response = request.Execute() [ for x in response.Responses do for y in x.LogoAnnotations do yield y.Description ] |> List.toArray let x = call "D:\\fsharp256.png" // val x : string  = [|"F#"|]
5 thoughts on “Google Cloud Vision API from .NET\F# (OAuth2 with ServiceAccount.json)”
Hi Sir, do you have sample in c#?
What part is not readable for you? Here you can find auth tips in C# http://stackoverflow.com/questions/28825860/google-oauth-error-at-least-one-client-secrets-installed-or-web-should-be-set/37068732#37068732