我已使用以下Java代码检测图像的文本,但是这里的问题是我无法正确进行身份验证
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());
}
}
}
}
一旦执行此代码行,我将收到以下错误消息
Exception in thread "main" java.io.IOException: The Application Default Credentials are not available. They are available if running on Google App Engine, Google Compute Engine, or Google Cloud Shell. 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.
at com.google.api.client.googleapis.auth.oauth2.DefaultCredentialProvider.getDefaultCredential(DefaultCredentialProvider.java:98)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:213)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:191)
at com.security.management.system.api.google_cloud_api.TextDetect.detectText(TextDetect.java:157)
at com.security.management.system.api.google_cloud_api.TextDetect.main(TextDetect.java:48)
以下库被使用
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文件,该文件也是根据此link给出的说明下载的。>
注意:-我已经在.bashrc文件中设置了GOOGLE_APPLICATION_CREDENTIALS变量
请您帮我解决这个问题
我已经使用以下Java代码检测图像的文本,但是这里的问题是我无法正确地验证公共静态void detectText()引发Exception,IOException {...
我在春季启动中使用了Google Cloud Vision API。并能够从图像中提取文本。