Java 如何使用Google Cloud API-用于文本检测的应用程序默认凭据
我使用了下面的java代码来检测图像的文本,但这里的问题是我无法正确地进行身份验证Java 如何使用Google Cloud API-用于文本检测的应用程序默认凭据,java,google-authentication,google-cloud-vision,Java,Google Authentication,Google Cloud Vision,我使用了下面的java代码来检测图像的文本,但这里的问题是我无法正确地进行身份验证 public static void detectText() throws Exception, IOException { GoogleCredential credential = GoogleCredential.getApplicationDefault(); List<AnnotateImageRequest> requests = new ArrayList<
public static void detectText() throws Exception, IOException {
GoogleCredential credential = GoogleCredential.getApplicationDefault();
List<AnnotateImageRequest> requests = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream("/home/buddika/Desktop/car_number_pate_16.jpeg"));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder()
.addFeatures(feat).setImage(img).build();
requests.add(request);
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.println("Error: %s\n"+ res.getError().getMessage());
return;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
System.out.println("Text: %s\n" + annotation.getDescription());
System.out.println("Position : %s\n" + annotation.getBoundingPoly());
}
}
}
}
使用了以下LIB
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.cloud.vision.spi.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.*;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
注意:我也使用了.json文件,该文件根据本文给出的指令下载
注意:-我已经在.bashrc文件中设置了GOOGLE\u APPLICATION\u CREDENTIALS变量
你能帮我解决这个问题吗我在spring boot中使用了google cloud vision api。并且能够从图像中提取文本 用于在属性下面添加凭据 application.properties文件: 在pom.xml中添加了依赖项
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-vision</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
org.springframework.cloud
注意:我没有使用GOOGLE\u APPLICATION\u CREDENTIALS变量。所以删除了library com.google.api.client.googleapis.auth.oauth2.GoogleCredential。你读了整个消息了吗<代码>应用程序默认凭据不可用。如果在谷歌应用程序引擎、谷歌计算引擎或谷歌云外壳上运行,则可以使用它们。否则,必须定义指向定义凭据的文件的环境变量GOOGLE\u APPLICATION\u CREDENTIALS。看见https://developers.google.com/accounts/docs/application-default-credentials 更多信息。
您是否在GAE上运行?如果没有,您是否将环境变量设置为指向凭据?确实,您说您“使用了.json文件”-请提供有关如何使用它的详细信息。我已将.json文件放在资源目录中,并尝试此代码行以获取默认设置GoogleCredential credential=GoogleCredential.getApplicationDefault()@JonSkeet@JimGarrisonGoogleCredential credential=GoogleCredential.getApplicationDefault();这就是我尝试过的方式it@BuddhikaAlwis:仅将JSON文件放入资源目录不会有任何作用。它需要在执行时是文件系统上的一个文件,然后需要设置GOOGLE\u APPLICATION\u CREDENTIALS环境变量来指定位置。此外,您在创建客户端时没有使用凭据。。。如果默认应用程序凭据正常工作,则根本不需要手动加载它们,但如果确实手动加载,则应在ImageAnnotatorSettings
中指定它们。
@RequestMapping("/gettext")
public String detectText() throws Exception, IOException {
//GoogleCredential credential = GoogleCredential.getApplicationDefault();
String finalText = "Ta Da! No value";
List<AnnotateImageRequest> requests = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream("G:\\files\\type1.jpeg"));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder()
.addFeatures(feat).setImage(img).build();
requests.add(request);
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.println("Error: %s\n"+ res.getError().getMessage());
return "";
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
finalText += annotation.getDescription();
System.out.println("Text: %s\n" + annotation.getDescription());
System.out.println("Position : %s\n" + annotation.getBoundingPoly());
}
}
}
return finalText;
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-vision</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
@RequestMapping("/gettext")
public String detectText() throws Exception, IOException {
//GoogleCredential credential = GoogleCredential.getApplicationDefault();
String finalText = "Ta Da! No value";
List<AnnotateImageRequest> requests = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream("G:\\files\\type1.jpeg"));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder()
.addFeatures(feat).setImage(img).build();
requests.add(request);
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.println("Error: %s\n"+ res.getError().getMessage());
return "";
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
finalText += annotation.getDescription();
System.out.println("Text: %s\n" + annotation.getDescription());
System.out.println("Position : %s\n" + annotation.getBoundingPoly());
}
}
}
return finalText;
}