I am trying to use google cloud vision api text detection.
using System;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Google.Api.Gax.Grpc;
namespace blablabla
{
class Program
{
static void Main(string[] args)
{
string filePath = #"D:\Manisha\Pictures\1.png";
var image = Image.FromFile(filePath);
var client = ImageAnnotatorClient.Create();
var response = client.DetectText(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
Console.WriteLine(annotation.Description);
}
Console.ReadLine();
}
}
}
I am getting the following error at
"var client = ImageAnnotatorClient.Create();"
"The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information."
I have set the GOOGLE_APPLICATION_CREDENTIALS to the json file path.
Where am i actually going wrong. Am i missing some important steps?
There are a few things you could check in this situation.
Is the value of GOOGLE_APPLICATION_CREDENTIALS correct?
Can your application access that environment variable?
Does your app/Visual Studio need to be restarted?
What is the value of credential?
One way to check the credentials that the SDK is using is the following.
var credential = GoogleCredential.GetApplicationDefault();
This appears to be a way to get an instance of the credentials, in case you wanted to test it. It's not necessary for the ImageAnnotatorClient per se.
Related
I am trying from my local web api, to retrieve secret from KeyVault using Azure.Identity lib.
but it throws Invalid Issuer. Giving below the code I am using
My current code
var client = new SecretClient(new Uri("key-vault-url"), new DefaultAzureCredential()); ==> line #1
var secret = client.GetSecret("DicomSecret").Value; ==> line #2
As soon as it parses line#2 it throws the below error.
What I have tried
I have added my Azure credential in the KeyVault thru' Add Access Policy
Tried using ManagedIdentityCredential instead of DefaultAzureCredential in line#1
Also tried using VisualStudioCredential instead of DefaultAzureCredential in line#1
I also read that I can be using EnvironmentCredential for which I need to provide AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET but I am not exactly sure how to and what to include for this - I do not have access to AAD.
Please let me know how to resolve this issue.
Since I was trying to connect to Azure from my local development environment (VS 2019) it was expecting additional credentials.
So from my dev environment (localhost) I had to use
DefaultAzureCredentialOptions VisualStudioTenantId along with SecretClient.
var tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions()
{
VisualStudioTenantId = tenantId,
SharedTokenCacheTenantId = tenantId
};
var client = new SecretClient(
new Uri(key-vault-url),
new DefaultAzureCredential(options)
);
The above helped me to execute from my local but after deploying it to Azure Ap Service the below line of code was sufficient. So I used the above code only for my local testing.
var client = new SecretClient(new Uri("key-vault-url"), new DefaultAzureCredential());
This is my code and it seems that there's no difference with yours.
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace test0430callingapi.Controllers
{
public class HelloController : Controller
{
public async Task<string> IndexAsync()
{
const string secretName = "clientsecret";
var kvUri = "https://keyvaultname.vault.azure.net/";
var a = new DefaultAzureCredential();
var client = new SecretClient(new Uri(kvUri), a);
var secret = await client.GetSecretAsync(secretName);
string secretVaule = secret.Value.Value;
return secretVaule ;
}
}
}
Then I think you may try to check the DefaultAzureCredential. When running the code in visual studio, we need to make sure that you've signed in with the user which has access permission to azure key vault by Add Access Policy in portal. Or maybe you've added the user, then you could check if has added enough permission for the user.
And if it also failed, you may try another way to access key vault by api. More details you can refer to this answer.
I am using Google.Cloud.Vision.V1, Version=2.0.0.0 and the following below code from Google Vision API specify JSON file
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Grpc.Auth;
using Grpc.Core;
var credential = GoogleCredential.FromFile("VisionProject.json");
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = ImageAnnotatorClient.Create(channel);
But its shows me this error No overload for method 'ImageAnnotatorClient.Create' takes 1 arguments.
I have found similar code in documentation https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Vision.V1P2Beta1/api/Google.Cloud.Vision.V1P2Beta1.ImageAnnotatorClient.html
But for some reason, it's not working( unable to see the overload)
It seems that you are using newer version of API. Docs state that now authentication is set up(when needed) via environment variable:
Otherwise, the simplest way of authenticating your API calls is to download a service account JSON file then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to it. The credentials will automatically be used to authenticate. See the Getting Started With Authentication guide for more details.
So you can do something like this:
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "PathTo_VisionProject.json");
var client = ImageAnnotatorClient.Create();
Or set this environment variable some other way.
While setting the environment variable is certainly a simple way of specifying which service account file to use, it's not the only one. You can use the builder to specify the path very easily:
var client = new ImageAnnotatorClientBuilder
{
CredentialsPath = "VisionProject.json"
}.Build();
Another solution if you do not have access to the JSON file directly and you want to stay close to what you previously did with the GoogleCredential and Channel creation is something like:
var credential = GoogleCredential.FromFile("VisionProject.json");
// or if you have access to the content only
// var credential = GoogleCredential.FromJson(json);
var client = await new ImageAnnotatorClientBuilder
{
Endpoint = ImageAnnotatorClient.DefaultEndpoint,
ChannelCredentials = credential.ToChannelCredentials()
}.BuildAsync();
I am using Google Vision API to get associated labels for an image.
var client = ImageAnnotatorClient.Create();
var image = Image.FromFile(#"C:\Users\Scorpio\Desktop\th.jpg");
var response = client.DetectLabels(image); // error
foreach (var annotation in response)
{
if (annotation.Description != null)
Console.WriteLine(annotation.Description);
}
Any idea how can we resolve this issue? I tried using very common images like country flags but still it gives error.
I just replicate this scenario by using your code and it ran successfully. Based on this, it is possible that this issue was related to a temporary and transient error from the service side, as mentioned in the error message; however, I recommend you to verify you are adding the correct libraries.
Below is the code I used to perform the testing which includes the authentication process:
using Google.Cloud.Vision.V1;
using System;
using Grpc.Auth;
using Google.Apis.Auth.OAuth2;
namespace VisionDemo
{
class Program
{
static void Main(string[] args)
{
//Authenticate to the service by using Service Account
var credential = GoogleCredential.FromFile(#"<CREDENTIALS_JSON_FILE_PATH>").CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
// Instantiates a client
var client = ImageAnnotatorClient.Create(channel);
var image = Image.FromFile(#"<IMAGE_PATH>");
var response = client.DetectLabels(image); // error
foreach (var annotation in response)
{
if (annotation.Description != null)
Console.WriteLine(annotation.Description);
}
}
}
}
In case you continue having this issue, you could take a look the Issue Tracker tool that you can use to raise a Vision API in order to verify this scenario with the Google Technical Support Team and check if this behavior can being generated with an issue on your project.
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", #"C:\Users\#YOURUSER#\source\repos\PdfToImage\credentials.json");
I bumped into the same issue.
Found the root cause and fixed it.
Issue is due to Nuget package: Grpc.Core.Api
This package got updated to version: 2.25.0, whereas Grpc.Core is blocked at version 1.22.1 due to Google.Api.Gax.Grpc 2.10.0
Nothing stops you from updating Grpc.Core.Api to 2.25.0 version and then face the weird issue - client.Detect... methods not returning success or error, nothing (I didnt wait for 20 mins).
Solution
Grpc.Core.Api - bring the version number back to 1.22.1
Hope that helps!
I'd like to use a GoogleCredential object (or similar) in order to create a Stackdriver logging client object (an instance of LoggingServiceV2Client class) using some custom credentials rather than the default application credentials.
I cannot see an appropriate overload of the LoggingServiceV2Client.Create method but the docstring for that method states:
Synchronously creates a
Google.Cloud.Logging.V2.LoggingServiceV2Client, applying defaults for
all unspecified settings, and creating a channel connecting to the
given endpoint with application default credentials where necessary.
See the example for how to use custom credentials.
which suggests it's possible somehow?
I have been unable to find a custom credentials example in the documentation anywhere. The only examples I see (eg this) read only the default application credentials from the GOOGLE_APPLICATION_CREDENTIALS environment variable which I'd prefer to avoid
It's possible, but far from obvious.
Add these two using statements to the top of your .cs:
using Google.Apis.Auth.OAuth2;
using Grpc.Auth;
Then instantiate the client like this:
var credential = GoogleCredential.FromFile(jsonPath)
.CreateScoped(LoggingServiceV2Client.DefaultScopes);
var channel = new Grpc.Core.Channel(
LoggingServiceV2Client.DefaultEndpoint.ToString(),
credential.ToChannelCredentials());
var client = LoggingServiceV2Client.Create(channel);
Other solutions didn't work for me using Google.Cloud.Logging.V2 - Version: 3.4.0, because of this line:
var client = LoggingServiceV2Client.Create(channel);
In version 3.4.0 there is no constructor that takes a channel as a parameter.
So I checked google documentation : LoggingServiceV2Client Create(), and it has this small note:
To specify custom credentials or other settings, use LoggingServiceV2ClientBuilder
So here is my working code using this approach:
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(LoggingServiceV2Client.DefaultScopes);
var client = new LoggingServiceV2ClientBuilder { ChannelCredentials = credential.ToChannelCredentials() }.Build();
I already apreciated to #Jeffrey Rennie. In my case, I am using Cloud Text-to-Speech and I had to use following code:
Usings:
using Google.Apis.Auth.OAuth2;
using Google.Cloud.TextToSpeech.V1;
using Grpc.Auth;
Code:
// Setting up credentials
string jsonPath = #"D:\my-test-project-0078ca7c0f8c.json";
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(TextToSpeechClient.DefaultScopes);
var channel = new Grpc.Core.Channel(TextToSpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
// Instantiate a client
TextToSpeechClient client = TextToSpeechClient.Create(channel);
// Perform the Text-to-Speech request, passing the text input with the selected voice parameters and audio file type
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
Input = new SynthesisInput() { Text = "My test sentence" },
Voice = new VoiceSelectionParams() { LanguageCode = "en-US", SsmlGender = SsmlVoiceGender.Male },
AudioConfig = new AudioConfig { AudioEncoding = AudioEncoding.Mp3 };
});
Installed NuGet Packages:
Google.Cloud.TextToSpeech.V1 -Pre
Google.Apis.Auth
Now a days i am implementing sentiment analysis through google cloud library,my code is
string text = "Feeling Not Well";
var client = LanguageServiceClient.Create();
var response = client.AnalyzeSentiment(new Document()
{
Content = text,
Type = Document.Types.Type.PlainText
});
var sentiment = response.DocumentSentiment;
var Score = sentiment.Score;
var magnitude = sentiment.Magnitude;
but it gives an error on
var client = LanguageServiceClient.Create();.
the error is
The Application Default Credentials are not available.
They are available if running in Google Compute Engine.
Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS
must be defined pointing to a file defining the credentials.
See https://developers.google.com/accounts/docs/application-default-credentials for more information.
please give me solution
You can either use
gcloud auth application-default login
from the command line (assuming you have the Cloud SDK installed), or generate and download a service account JSON file and then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to that file.
The Create method call will first check the environment variable, and then look for application default credentials from gcloud if the environment variable isn't set.
Basically, the credential options are:
Explicitly create one from a service account file, e.g. GoogleCredential.FromStream(stream) and use that to create a Channel which you can pass into Create, as described in the FAQ
Call create without any arguments (or passing in null) in which case:
If you've set the GOOGLE_APPLICATION_CREDENTIALS environment variable, it is assumed that's where the service account JSON file is
Otherwise, if you've run gcloud auth application-default login those credentials will be used
Otherwise, if you're running on Google Cloud Platform (e.g. Compute Engine or AppEngine Flexible) you will get the default credentials for the project
Otherwise, the call will fail
Additionally, you can use the Document.FromPlainText call to simplify your code:
string text = "Feeling Not Well";
var client = LanguageServiceClient.Create();
var response = client.AnalyzeSentiment(Document.FromPlainText(text));
var sentiment = response.DocumentSentiment;
var Score = sentiment.Score;
var magnitude = sentiment.Magnitude;