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.annotationsgoogle.cloud.vision.batchgoogle.cloud.vision.clientgoogle.cloud.vision.colorgoogle.cloud.vision.crop_hintgoogle.cloud.vision.entitygoogle.cloud.vision.facegoogle.cloud.vision.featuregoogle.cloud.vision.geometrygoogle.cloud.vision.imagegoogle.cloud.vision.likelihoodgoogle.cloud.vision.safe_searchgoogle.cloud.vision.textgoogle.cloud.vision.web
必要なコードの変更
インポート
Python クライアント ライブラリ v0.25.1 の新しい型にアクセスするには、新しい google.cloud.vision.types モジュールをインクルードします。
types モジュールには、リクエストを作成するために必要な新しいクラス(types.Image など)が含まれています。
加えて、新しい google.cloud.vision.enums モジュールには、API レスポンスの解析と理解に役立つ列挙型(enums.Likelihood.UNLIKELY や enums.FaceAnnotation.Landmark.Type.LEFT_EYE など)が含まれています。
クライアントの作成
Client クラスは、ImageAnnotatorClient クラスに置き換えられました。Client クラスへの参照を ImageAnnotatorClient に置き換えてください。
旧バージョンのクライアント ライブラリ:
old_client = vision.Client()
Python クライアント ライブラリ v0.25.1:
画像コンテンツを表すオブジェクトの作成
ローカル ファイル、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:
URI からの画像コンテンツを表すオブジェクトの作成
次の例は、Google Cloud Storage URI やウェブ URI の画像コンテンツを表す新しい方法を示しています。uri は、Google Cloud Storage またはウェブ上にある画像ファイルの URI です。
旧バージョンのクライアント ライブラリ:
image = old_client.image(source_uri=uri)
Python クライアント ライブラリ v0.25.1:
リクエストの作成とレスポンスの処理
Python クライアント ライブラリ v.0.25.1 において、face_detection などの API メソッドは、Image オブジェクトではなく、ImageAnnotatorClient オブジェクトに属します。
以下に説明されているように、いくつかのメソッドでは戻り値が異なります。
特に、境界ボックスの頂点は、bounds.vertices ではなく bounding_poly.vertices に格納されるようになりました。各頂点の座標は vertex.x_coordinate と vertex.y_coordinate ではなく vertex.x と vertex.y に格納されます。
境界ボックスの変更は、face_detection、logo_detection、text_detection、document_text_detection、crop_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 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:
ランドマーク検出リクエストの作成とレスポンスの処理
クライアント ライブラリの旧バージョン:
ランドマークの場所の緯度と経度は、location.latitude と location.longitude ではなく、location.lat_lng.latitude と location.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 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: