Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google vision API load credentials from file

I want to use a different credentials file when creating my Annotator client. I am currently able to do it with the transalte API like so:

 Credentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("path/to/credentials.json"));
 return TranslateOptions.newBuilder().setCredentials(creds).build().getService();  

Is there an equivalent way of doing it with the ImageAnnotatorClient?

Edit: I am working with google cloud java sdk version: 1.16.0

like image 931
Zaid Amir Avatar asked Mar 07 '23 21:03

Zaid Amir


1 Answers

Credentials 🔗 -> CredentialsProvider 🔗

ImageAnnotatorSettings.Builder -> ImageAnnotatorSettings -> ImageAnnotatorClient

Example (mostly copied from the docs):

Credentials myCredentials = ServiceAccountCredentials.fromStream(
    new FileInputStream("path/to/credentials.json"));

ImageAnnotatorSettings imageAnnotatorSettings =
    ImageAnnotatorSettings.newBuilder()
    .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
    .build();

ImageAnnotatorClient imageAnnotatorClient =
    ImageAnnotatorClient.create(imageAnnotatorSettings);
  • ImageAnnotatorClient: com.google.cloud.vision.v1.ImageAnnotatorClient 🔗
  • ImageAnnotatorSettings: com.google.cloud.vision.v1.ImageAnnotatorSettings 🔗
  • ClientSettings.Builder: com.google.api.gax.rpc.ClientSettings.Builder 🔗
  • FixedCredentialsProvider: com.google.api.gax.core.FixedCredentialsProvider 🔗
  • ServiceAccountCredentials: com.google.auth.oauth2.ServiceAccountCredentials 🔗

Note: The above is for Java. The API for C# is different.

like image 139
AnOccasionalCashew Avatar answered Mar 09 '23 10:03

AnOccasionalCashew