我使用以下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变量
你可以帮我解决这个问题吗
答案 0 :(得分:0)
我在春季启动时使用了谷歌云视觉API。并能够从图像中提取文本。
用于在属性下方添加的凭据。
application.properties 文件:
spring.cloud.gcp.project-id=mycloud-123456
spring.cloud.gcp.credentials.location=file:src/main/resources/service-account.json.
在pom.xml中添加了依赖性
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-vision</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
您的上述功能正在项目中。定义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;
}
注意:创建服务帐户并下载json,步骤Create a service account
注意:我尚未使用GOOGLE_APPLICATION_CREDENTIALS变量。因此删除了库 com.google.api.client.googleapis.auth.oauth2.GoogleCredential 。