我尝试使用提供的Java客户端谷歌愿景通过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; 在com.google.api.gax.retrying.BasicRetryingFuture。(BasicRetryingFuture.java:77) 在com.google.api.gax.retrying.CallbackChainRetryingFuture。(CallbackChainRetryingFuture.java:62) 在com.google.api.gax.retrying.ScheduledRetryingExecutor.createFuture(ScheduledRetryingExecutor.java:86) 在com.google.api.gax.grpc.RetryingCallable.futureCall(RetryingCallable.java:57) 在com.google.api.gax.grpc.RetryingCallable.futureCall(RetryingCallable.java:42) 在com.google.api.gax.grpc.AuthCallable.futureCall(AuthCallable.java:57) 在com.google.api.gax.grpc.UnaryCallable.futureCall(UnaryCallable.java:282) 在com.google.api.gax.grpc.UnaryCallable.futureCall(UnaryCallable.java:293) 在com.google.api.gax.grpc.UnaryCallable.call(UnaryCallable.java:321) 在com.google.cloud.vision.v1.ImageAnnotatorClient.batchAnnotateImages(ImageAnnotatorClient.java:201) 在com.google.cloud.vision.v1.ImageAnnotatorClient.batchAnnotateImages(ImageAnnotatorClient.java:177) 在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客户端实施的示例代码。
任何想法?
答案 0 :(得分:5)
MoreExecutors
类中缺少的方法@since 18.0
在番石榴来源中注释mvn dependency:analyze
(参见source)。
我猜你在你的类路径中有一个旧版本的Guava,它出现在版本19之前。
您应该运行mvn dependency:analyze | grep guava
来追踪罪魁祸首。
您可以mvn dependency:tree -Dverbose
过滤输出。
然后,您可以使用以下命令检查哪个包导入旧的依赖项:
import maker
from operators import op_morph
import unittest
from unittest.mock import Mock
import cv2
img = cv2.imread("/img_tests/unitTestBaseImage.png")
def my_side_effect(*args, **kwargs):
global img
print(args)
print(kwargs)
if args!=None:
if len(args)>0:
if args[0] == img:
return 0
return 3
else:
return 2
return 1
class TestCalls(unittest.TestCase):
def test_dispatch(self):
global img
makerMock = Mock()
makerMock.deal_with_result.side_effect = my_side_effect
#calling the dispatch function
maker.dispatch(["json_example.json"])
#instead of passing this mock I want to get the real parameters passed
#when the function deal_with_result is called in dispatch.
temp=makerMock.deal_with_result("img")
print("image return code: "+str(temp))
if __name__ == '__main__':
unittest.main(exit=False)
答案 1 :(得分:1)
基本上,删除番石榴重复依赖。
为了获得最佳内存使用,我建议重写此代码的一部分以使用StringBuffer ou StringBuilder而不是String concat StringBuffer ou StringBuilder。
答案 2 :(得分:1)
You excluded guava from the google-cloud-vision
dependency (because the version may be old i guess) but you are not adding the version you need as a dependency :
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
google-auth-library-oauth2-http
also is including a version of guava :com.google.guava:guava:jar:19.0:compile
. I you are adding a version of guava as a dependency, you may want to exclude the one from