[英]how to use Google Cloud API - Application Default Credentials for text detection
i have used following java code to detect a text of a image, but here problem is i couldn't able to authenticate correctly我使用以下 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());
}
}
}
}
i am having below error message once execute this code lines执行此代码行后,我收到以下错误消息
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)
following libs were used使用了以下库
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;
note i have used the .json file also which downloaded accring to the instruction given from this link请注意,我也使用了 .json 文件,该文件根据此链接给出的说明下载
note :- i have set the GOOGLE_APPLICATION_CREDENTIALS variable in .bashrc file注意:- 我在 .bashrc 文件中设置了 GOOGLE_APPLICATION_CREDENTIALS 变量
could you please help me on this issue你能帮我解决这个问题吗
I used google cloud vision api with spring boot.我在 Spring Boot 中使用了谷歌云视觉 API。 And able to extract text from images.
并且能够从图像中提取文本。
for credentials added below properties.对于在属性下面添加的凭据。
application.properties file: application.properties文件:
spring.cloud.gcp.project-id=mycloud-123456
spring.cloud.gcp.credentials.location=file:src/main/resources/service-account.json.
added dependency in pom.xml在 pom.xml 中添加依赖项
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-vision</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
Your above function is working in the project.您的上述功能正在项目中运行。 commented line which were defining GoogleCredential.
定义 GoogleCredential 的注释行。
@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;
}
Note: Created service account and downloaded json, steps Create a service account注意:创建服务账号并下载json,步骤创建服务账号
Note: I haven't used the GOOGLE_APPLICATION_CREDENTIALS variable.注意:我没有使用 GOOGLE_APPLICATION_CREDENTIALS 变量。 so deleted library com.google.api.client.googleapis.auth.oauth2.GoogleCredential .
所以删除了库com.google.api.client.googleapis.auth.oauth2.GoogleCredential 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.