LoginSignup
0
0

More than 1 year has passed since last update.

GCP API v2.0.0の注意点:メソッド名からenumが削除されている

Last updated at Posted at 2021-07-25

( 利用環境 )

Google Cloud Platform (GCP)のアカウントを取得後、ローカルのMacbookから、GCPにssh接続して、Google Video Intelligence APIを叩いています。

GCP API v2.0.0のハマりポイント

次のポイントでハマりましたので、解決策を掲載します。

  • GCP API v2.0.0から、メソッド名中のenumは削除されている。

GCP VideoIntelligenceのインスタンス作成まで

>>> import json
>>> from google.cloud import videointelligence
>>> from google.oauth2 import service_account
>>>
>>> service_account_key_name = "electroncgpvision-8bf54f743c93.json"
>>> info = json.load(open(service_account_key_name))
>>> creds = service_account.Credentials.from_service_account_info(info)
>>>
>>> video_client = videointelligence.VideoIntelligenceServiceClient(credentials=creds)

動画ファイル読み込み

>>> path = './olympic.mp4'
>>>
>>> import io
>>>
>>> path = './olympic.mp4'
>>>
>>> with io.open(path, 'rb') as file:
...     input_content = file.read()
... 
>>>

メソッド名の未定義エラー

>>> features = [videointelligence.enums.Feature.OBJECT_TRACKING]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'google.cloud.videointelligence' has no attribute 'enums'
>>>

以下のウェブサイトなどでは、enumの文字列を含むAPIを叩いている。

次にAPIを実行して結果を取得する

features = [videointelligence.enums.Feature.OBJECT_TRACKING]

しかし、次のエラーが発生しました。

エラー
AttributeError: module 'google.cloud.videointelligence' has no attribute 'enums'

GCP API v2.0.0では、enumが削除されていた

enums と types サブモジュールが削除されています。
例えば、v1.0.0では、vision.types.Image を v2.0.0では、vision.Image として利用します。同様に、vision.enums.Feature.Typeはvision.Feature.Typeとします。

以下で成功した。

>>> features = [videointelligence.Feature.OBJECT_TRACKING]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0