GCP vision AI에서'구글'.cloud.vision' has no attribute 'types'

개요


2021년 10월 30일 현재 확인된 곳도 교정이 수정된 것 같다.
GCP의 물체 감지 API 튜토리얼을 실행하면 오류가 발생했기 때문에 해결 방법을 집필합니다.
API에 대한 인증 메서드를 생략합니다.
사용 언어는 Python입니다.

오류 정보


다음 자습서 코드를 실행하면 AttributeError가 생성됩니다.
(이하 코드는 튜토리얼에서 복사)
def localize_objects(path):
    from google。cloud import vision
    client = vision.ImageAnnotatorClient()

    with open(path, 'rb') as image_file:
        content = image_file.read()
    image = vision.types.Image(content=content)

    objects = client.object_localization(
        image=image).localized_object_annotations

    print('Number of objects found: {}'.format(len(objects)))
    for object_ in objects:
        print('\n{} (confidence: {})'.format(object_.name, object_.score))
        print('Normalized bounding polygon vertices: ')
        for vertex in object_.bounding_poly.normalized_vertices:
            print(' - ({}, {})'.format(vertex.x, vertex.y))

오류 내용은 다음과 같습니다.
일곱 번째 줄에서 발생합니다.google.cloud.vision에서 types를 삭제한 것 같아서 오류가 발생했습니다.
AttributeError: module 'google.cloud.vision' has no attribute 'types'

해결책


비젼에서 Image를 바로 사용하면 OK.
image = vision.types.Image(content=content) # エラー
image = vision.Image(content=content) # 解決
전체는 다음과 같이 11줄만 변경되었다
    """Localize objects in the local image.

    Args:
    path: The path to the local file.
    """
    from google.cloud import vision
    client = vision.ImageAnnotatorClient()

    with open(uri, 'rb') as image_file:
        content = image_file.read()
    image = vision.Image(content=content) # 変更箇所

    objects = client.object_localization(
        image=image).localized_object_annotations

    print('Number of objects found: {}'.format(len(objects)))
    for object_ in objects:
        print('\n{} (confidence: {})'.format(object_.name, object_.score))
        print('Normalized bounding polygon vertices: ')
        for vertex in object_.bounding_poly.normalized_vertices:
            print(' - ({}, {})'.format(vertex.x, vertex.y))

잡담


정부 강좌에 의외로 결함이 있다.
그리고 GCP의 Image와 Pillow의 Image를 끼면 좀 안 좋아요.

참고 자료


https://cloud.google.com/vision/docs/object-localizer?hl=ja

좋은 웹페이지 즐겨찾기