google cloud visionの顔検出のチュートリアル(https://cloud.google.com/vision/docs/face-tutorial)を試しているのですが、表題の通りこのようなエラーでつまづいてます。
Traceback (most recent call last):
File "google_face_detect.py", line 79, in <module>
main(args.input_image, args.output, args.max_results)
File "google_face_detect.py", line 55, in main
faces = detect_face(image, max_results)
File "google_face_detect.py", line 26, in detect_face
return client.face_detection(image=image).face_annotations
File "/Users/kentasuhara/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/google/cloud/vision_helpers/decorators.py", line 111, in inner
response = self.annotate_image(request, retry=retry, timeout=timeout)
File "/Users/kentasuhara/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/google/cloud/vision_helpers/__init__.py", line 67, in annotate_image
r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
File "/Users/kentasuhara/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/google/cloud/vision_v1/gapic/image_annotator_client.py", line 209, in batch_annotate_images
self.transport.batch_annotate_images,
AttributeError: 'str' object has no attribute 'batch_annotate_images'
どこがstrになっていてどう直せばいいのでしょうか?
コードは以下の通りで、githubのコードをほぼそのままコピペしています。
またターミナルで実行しているコードは $python google_face_detect.py(ファイル名) 画像の絶対パス です。
import argparse
# [START vision_face_detection_tutorial_imports]
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image, ImageDraw
# [END vision_face_detection_tutorial_imports]
GOOGLE_APPLICATION_CREDENTIALS="絶対パス"
# [START vision_face_detection_tutorial_send_request]
def detect_face(face_file, max_results=4):
"""Uses the Vision API to detect faces in the given file.
Args:
face_file: A file-like object containing an image with faces.
Returns:
An array of Face objects with information about the picture.
"""
# [START vision_face_detection_tutorial_client]
client = vision.ImageAnnotatorClient(GOOGLE_APPLICATION_CREDENTIALS)
# [END vision_face_detection_tutorial_client]
content = face_file.read()
image = types.Image(content=content)
return client.face_detection(image=image).face_annotations
# [END vision_face_detection_tutorial_send_request]
# [START vision_face_detection_tutorial_process_response]
def highlight_faces(image, faces, output_filename):
"""Draws a polygon around the faces, then saves to output_filename.
Args:
image: a file containing the image with the faces.
faces: a list of faces found in the file. This should be in the format
returned by the Vision API.
output_filename: the name of the image file to be created, where the
faces have polygons drawn around them.
"""
im = Image.open(image)
draw = ImageDraw.Draw(im)
for face in faces:
box = [(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices]
draw.line(box + [box[0]], width=5, fill='#00ff00')
im.save(output_filename)
# [END vision_face_detection_tutorial_process_response]
# [START vision_face_detection_tutorial_run_application]
def main(input_filename, output_filename, max_results):
with open(input_filename, 'rb') as image:
faces = detect_face(image, max_results)
print('Found {} face{}'.format(
len(faces), '' if len(faces) == 1 else 's'))
print('Writing to file {}'.format(output_filename))
# Reset the file pointer, so we can read the file again
image.seek(0)
highlight_faces(image, faces, output_filename)
# [END vision_face_detection_tutorial_run_application]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Detects faces in the given image.')
parser.add_argument(
'input_image', help='the image you\'d like to detect faces in.')
parser.add_argument(
'--out', dest='output', default='out.jpg',
help='the name of the output file.')
parser.add_argument(
'--max-results', dest='max_results', default=4,
help='the max results of face detection.')
args = parser.parse_args()
main(args.input_image, args.output, args.max_results)
よろしくお願いします。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
client = vision.ImageAnnotatorClient(GOOGLE_APPLICATION_CREDENTIALS)
が間違っているのでは? 第一引数はtransportになると思いますが、docstringによると
transport (Union[~.ImageAnnotatorGrpcTransport,
Callable[[~.Credentials, type], ~.ImageAnnotatorGrpcTransport]): A transport
instance, responsible for actually making the API calls.
The default transport uses the gRPC protocol.
This argument may also be a callable which returns a
transport instance. Callables will be sent the credentials
as the first argument and the default transport class as
the second argument.
https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/vision/google/cloud/vision_v1/gapic/image_annotator_client.py#L85
とのことで、とりあえず絶対パスの文字列ではないようです。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.23%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正、ベストアンサー選択の依頼
hayataka2049
2018/09/28 11:09
エラーはtraceback等含めて省略せず全文掲載した方が回答しやすいです
ksuhara
2018/09/28 11:14
ありがとうございます。全文掲載しました。