У объекта str нет атрибута пакетного аннотирования изображений - PullRequest
0 голосов
/ 27 мая 2019

Я хочу извлечь текст из изображения и использовать API Google Vision, но получаю сообщение об ошибке "У объекта 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'

Хорошо, если вы предоставите мне ссылку на источник, где я смогу узнать больше об API Google

1 Ответ

0 голосов
/ 31 мая 2019

Измените

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

на

client = vision.ImageAnnotatorClient()

В своем терминале установите переменную среды GOOGLE_APPLICATION_CREDENTIALS.

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

Вот быстрый запуск guide.

...