Python クライアント ライブラリ v0.25.1 への移行

Python v0.25.1 用のクライアント ライブラリでは、以前のクライアント ライブラリの設計方法にいくつかの重要な変更が加えられています。この変更を、次にまとめます。

  • モジュールを統合して型の数を少なくした

  • 型指定なしのパラメータを、厳密に型指定されたクラスと列挙型に置き換えた

このトピックでは、v0.25.1 Python クライアント ライブラリを使用するために、Cloud Vision API クライアント ライブラリの Python コードに加える必要のある変更について詳しく説明します。

旧バージョンのクライアント ライブラリの実行

Python クライアント ライブラリを v0.25.1 にアップグレードする必要はありません。Python クライアント ライブラリの旧バージョンを引き続き使用し、コードを移行しない場合は、アプリで使用する Python クライアント ライブラリのバージョンを指定する必要があります。ライブラリの特定バージョンを指定するには、requirements.txt ファイルを次のように編集します。

google-cloud-vision==0.25

削除されたモジュール

以下のモジュールは、Python Client Library v0.25.1 パッケージで削除されました。

  • google.cloud.vision.annotations

  • google.cloud.vision.batch

  • google.cloud.vision.client

  • google.cloud.vision.color

  • google.cloud.vision.crop_hint

  • google.cloud.vision.entity

  • google.cloud.vision.face

  • google.cloud.vision.feature

  • google.cloud.vision.geometry

  • google.cloud.vision.image

  • google.cloud.vision.likelihood

  • google.cloud.vision.safe_search

  • google.cloud.vision.text

  • google.cloud.vision.web

必要なコードの変更

インポート

Python クライアント ライブラリ v0.25.1 の新しい型にアクセスするには、新しい google.cloud.vision.types モジュールをインクルードします。

types モジュールには、リクエストを作成するために必要な新しいクラス(types.Image など)が含まれています。

from google.cloud import vision

加えて、新しい google.cloud.vision.enums モジュールには、API レスポンスの解析と理解に役立つ列挙型(enums.Likelihood.UNLIKELYenums.FaceAnnotation.Landmark.Type.LEFT_EYE など)が含まれています。

クライアントの作成

Client クラスは、ImageAnnotatorClient クラスに置き換えられました。Client クラスへの参照を ImageAnnotatorClient に置き換えてください。

旧バージョンのクライアント ライブラリ:

old_client = vision.Client()

Python クライアント ライブラリ v0.25.1:

client = vision.ImageAnnotatorClient()

画像コンテンツを表すオブジェクトの作成

ローカル ファイル、Google Cloud Storage URI、またはウェブ URI から画像コンテンツを指定するには、新しい Image クラスを使用します。

ローカル ファイルからの画像コンテンツを表すオブジェクトの作成

以下の例は、ローカル ファイルからの画像コンテンツを表す新しい方法を示しています。

旧バージョンのクライアント ライブラリ:

with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = old_client.image(content=content)

Python クライアント ライブラリ v0.25.1:

with open(path, "rb") as image_file:
    content = image_file.read()

image = vision.Image(content=content)

URI からの画像コンテンツを表すオブジェクトの作成

次の例は、Google Cloud Storage URI やウェブ URI の画像コンテンツを表す新しい方法を示しています。uri は、Google Cloud Storage またはウェブ上にある画像ファイルの URI です。

旧バージョンのクライアント ライブラリ:

image = old_client.image(source_uri=uri)

Python クライアント ライブラリ v0.25.1:

image = vision.Image()
image.source.image_uri = uri

リクエストの作成とレスポンスの処理

Python クライアント ライブラリ v.0.25.1 において、face_detection などの API メソッドは、Image オブジェクトではなく、ImageAnnotatorClient オブジェクトに属します。

以下に説明されているように、いくつかのメソッドでは戻り値が異なります。

特に、境界ボックスの頂点は、bounds.vertices ではなく bounding_poly.vertices に格納されるようになりました。各頂点の座標は vertex.x_coordinatevertex.y_coordinate ではなく vertex.xvertex.y に格納されます。

境界ボックスの変更は、face_detectionlogo_detectiontext_detectiondocument_text_detectioncrop_hints に影響します。

顔検出リクエストの作成とレスポンスの処理

感情の可能性レベルは、face.emotions.surprise ではなく face.surprise_likelihood に格納された列挙型として返されるようになりました。可能性レベルのラベルの名前は、google.cloud.vision.enums.Likelihood をインポートすることによって回復できます。

クライアント ライブラリの旧バージョン:

with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = old_client.image(content=content)

faces = image.detect_faces()

for face in faces:
    print('anger: {}'.format(face.emotions.anger))
    print('joy: {}'.format(face.emotions.joy))
    print('surprise: {}'.format(face.emotions.surprise))

    vertices = (['({},{})'.format(bound.x_coordinate, bound.y_coordinate)
                for bound in face.bounds.vertices])

    print('face bounds: {}'.format(','.join(vertices)))

Python クライアント ライブラリ v0.25.1:

with open(path, "rb") as image_file:
    content = image_file.read()

image = vision.Image(content=content)

response = client.face_detection(image=image)
faces = response.face_annotations

# Names of likelihood from google.cloud.vision.enums
likelihood_name = (
    "UNKNOWN",
    "VERY_UNLIKELY",
    "UNLIKELY",
    "POSSIBLE",
    "LIKELY",
    "VERY_LIKELY",
)
print("Faces:")

for face in faces:
    print(f"anger: {likelihood_name[face.anger_likelihood]}")
    print(f"joy: {likelihood_name[face.joy_likelihood]}")
    print(f"surprise: {likelihood_name[face.surprise_likelihood]}")

    vertices = [
        f"({vertex.x},{vertex.y})" for vertex in face.bounding_poly.vertices
    ]

    print("face bounds: {}".format(",".join(vertices)))

if response.error.message:
    raise Exception(
        "{}\nFor more info on error messages, check: "
        "https://cloud.google.com/apis/design/errors".format(response.error.message)
    )

ラベル検出リクエストの作成とレスポンスの処理

クライアント ライブラリの旧バージョン:

with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = old_client.image(content=content)

labels = image.detect_labels()

for label in labels:
    print(label.description)

Python クライアント ライブラリ v0.25.1:

with open(path, "rb") as image_file:
    content = image_file.read()

image = vision.Image(content=content)

response = client.label_detection(image=image)
labels = response.label_annotations
print("Labels:")

for label in labels:
    print(label.description)

if response.error.message:
    raise Exception(
        "{}\nFor more info on error messages, check: "
        "https://cloud.google.com/apis/design/errors".format(response.error.message)
    )

ランドマーク検出リクエストの作成とレスポンスの処理

クライアント ライブラリの旧バージョン:

ランドマークの場所の緯度と経度は、location.latitudelocation.longitude ではなく、location.lat_lng.latitudelocation.lat_lng.longitude に格納されるようになりました。

with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = old_client.image(content=content)

landmarks = image.detect_landmarks()

for landmark in landmarks:
    print(landmark.description, landmark.score)
    for location in landmark.locations:
        print('Latitude'.format(location.latitude))
        print('Longitude'.format(location.longitude))

Python クライアント ライブラリ v0.25.1:

with open(path, "rb") as image_file:
    content = image_file.read()

image = vision.Image(content=content)

response = client.landmark_detection(image=image)
landmarks = response.landmark_annotations
print("Landmarks:")

for landmark in landmarks:
    print(landmark.description)
    for location in landmark.locations:
        lat_lng = location.lat_lng
        print(f"Latitude {lat_lng.latitude}")
        print(f"Longitude {lat_lng.longitude}")

if response.error.message:
    raise Exception(
        "{}\nFor more info on error messages, check: "
        "https://cloud.google.com/apis/design/errors".format(response.error.message)
    )

ロゴ検出リクエストの作成とレスポンスの処理

クライアント ライブラリの旧バージョン:

with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = old_client.image(content=content)

logos = image.detect_logos()

for logo in logos:
    print(logo.description, logo.score)

Python クライアント ライブラリ v0.25.1:

with open(path, "rb")