str对象没有属性批注图像

时间:2019-05-27 12:26:21

标签: python-3.x google-cloud-vision

我想从图像中提取文本并使用google vision API,但出现错误“ str对象没有属性批处理图像批注”。

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

# Instantiates a client
client = vision.ImageAnnotatorClient('OCR and voice-bd78adad8bd9.json')

# The name of the image file to annotate
file_name = 'b1.jpg'

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)

# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations

print('Labels:')
for label in labels:
    print(label.description)

向我显示的错误如下

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-c65657163dd3> in <module>
     19 
     20 # Performs label detection on the image file
---> 21 response = client.label_detection(image=image)
     22 labels = response.label_annotations
     23 

~\Anaconda3\lib\site-packages\google\cloud\vision_helpers\decorators.py in inner(self, image, max_results, retry, timeout, **kwargs)
     99             copied_features["max_results"] = max_results
    100         request = dict(image=image, features=[copied_features], **kwargs)
--> 101         response = self.annotate_image(request, retry=retry, timeout=timeout)
    102         return response
    103 

~\Anaconda3\lib\site-packages\google\cloud\vision_helpers\__init__.py in annotate_image(self, request, retry, timeout)
     70         # of them.
     71         protobuf.setdefault(request, "features", self._get_all_features())
---> 72         r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
     73         return r.responses[0]
     74 

~\Anaconda3\lib\site-packages\google\cloud\vision_v1\gapic\image_annotator_client.py in batch_annotate_images(self, requests, retry, timeout, metadata)
    224                 "batch_annotate_images"
    225             ] = google.api_core.gapic_v1.method.wrap_method(
--> 226                 self.transport.batch_annotate_images,
    227                 default_retry=self._method_configs["BatchAnnotateImages"].retry,
    228                 default_timeout=self._method_configs["BatchAnnotateImages"].timeout,

AttributeError: 'str' object has no attribute 'batch_annotate_images'

如果您能向我提供一些源链接,以便我可以详细了解GOOGLE API,那么我会很好

1 个答案:

答案 0 :(得分:0)

更改

client = vision.ImageAnnotatorClient('OCR and voice-bd78adad8bd9.json')

client = vision.ImageAnnotatorClient()

在您的终端中,设置环境变量GOOGLE_APPLICATION_CREDENTIALS。

export GOOGLE_APPLICATION_CREDENTIALS='path/to/your/service_account.json'

这是quickstart指南。