gpt4 book ai didi

java - Google Vision 使用 Java 客户端库批注图像

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:04 24 4
gpt4 key购买 nike

尝试使用提供的 java 客户端 google vision 通过 Google Vision 注释图像时出现异常。

特别是批处理 client.batchAnnotateImages 出现的这段代码:

public void processOCR(byte[] file) 
{
List<AnnotateImageRequest> requests = new ArrayList<>();

ByteString imageByteString = ByteString.copyFrom(file);

Image img = Image.newBuilder().setContent(imageByteString).build();
Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_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();
client.close();

//visionResultsDTO result = new visionResultsDTO();
String ParagraphText = "";


for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
//throw exception.
return;
}

// For full list of available annotations, see http://g.co/cloud/vision/docs
TextAnnotation annotation = res.getFullTextAnnotation();
for (Page page: annotation.getPagesList()) {
String pageText = "";
for (Block block : page.getBlocksList()) {
String blockText = "";
for (Paragraph para : block.getParagraphsList()) {
String paraText = "";
for (Word word: para.getWordsList()) {
String wordText = "";
for (Symbol symbol: word.getSymbolsList()) {
wordText = wordText + symbol.getText();
}
paraText = paraText + wordText;
}
// Output Example using Paragraph:
blockText = blockText + paraText;
}
pageText = pageText + blockText;
}
}
ParagraphText = annotation.getText();
// result.setResultText(ParagraphText);
}
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我看到以下堆栈跟踪/错误:

java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor; at com.google.api.gax.retrying.BasicRetryingFuture.(BasicRetryingFuture.java:77) at com.google.api.gax.retrying.CallbackChainRetryingFuture.(CallbackChainRetryingFuture.java:62) at com.google.api.gax.retrying.ScheduledRetryingExecutor.createFuture(ScheduledRetryingExecutor.java:86) at com.google.api.gax.grpc.RetryingCallable.futureCall(RetryingCallable.java:57) at com.google.api.gax.grpc.RetryingCallable.futureCall(RetryingCallable.java:42) at com.google.api.gax.grpc.AuthCallable.futureCall(AuthCallable.java:57) at com.google.api.gax.grpc.UnaryCallable.futureCall(UnaryCallable.java:282) at com.google.api.gax.grpc.UnaryCallable.futureCall(UnaryCallable.java:293) at com.google.api.gax.grpc.UnaryCallable.call(UnaryCallable.java:321) at com.google.cloud.vision.v1.ImageAnnotatorClient.batchAnnotateImages(ImageAnnotatorClient.java:201) at com.google.cloud.vision.v1.ImageAnnotatorClient.batchAnnotateImages(ImageAnnotatorClient.java:177) at za.co.thumbtribe.core.googlevision.service.impl.GoogleVisionServiceImpl.processOCR(GoogleVisionServiceImpl.java:55)

这是我的 POM 依赖项:

<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>0.20.3-beta</version>
<exclusions>
<exclusion>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
<version>0.7.0</version>
</dependency>

</dependencies>

我尝试排除 guava 并包含多个版本的 API。

显示的代码是来自 google vision 客户端实现的示例代码。

有什么想法吗?

最佳答案

MoreExecutors 类中缺少的方法 directExecutor 在 guava 的源代码中被注释为 @since 18.0(参见 source )。

我猜你的类路径中有一个旧版本的 Guava,出现在版本 19 之前。

您应该运行 mvn dependency:analyze 来追踪罪魁祸首。你可以 mvn dependency:analyze | grep guava 过滤输出。

然后你可以检查哪个包导入了旧的依赖:

mvn dependency:tree -Dverbose

关于java - Google Vision 使用 Java 客户端库批注图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45306016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com