gcloudコマンドでApplication Default Credentials (ADC)の作成

gcloudコマンドでApplication Default Credentials (ADC)の作成

https://cloud.google.com/docs/authentication/provide-credentials-adc?hl=ja#local-dev
にリファレンスがある

Application Default Credentials (ADC) は、アプリケーション環境に基づいて認証情報を自動的に検索するために、認証ライブラリが使用する手法です。認証ライブラリは、これらの認証情報を Cloud クライアント ライブラリと Google API クライアント ライブラリで使用可能にします。ADC を使用すると、Google Cloud サービスと API に対するアプリケーションの認証方法を変更せずに、開発環境または本番環境でコードを実行できます

とのこと

参考サイトにしている
https://nikkie-ftnext.hatenablog.com/entry/ocr-with-google-vision-api-python-first-step
では
ユーザー認証情報で進めました
とあるので

gcloud auth application-default login

を実行する

すると
Google Auth Library が Google アカウントへのアクセスをリクエストが要求されるので
アカウントを選択し
許可する

これで
ライブラリで参照するJSONファイルが作られる

ls ~/.config/gcloud/application_default_credentials.json

でファイルがあるか確認できる

ただし
https://nikkie-ftnext.hatenablog.com/entry/ocr-with-google-vision-api-python-first-step
によれば
Vision APIを最初に叩いたとき「The vision.googleapis.com API requires a quota project, which is not set by default.」が返りました
* 案内に沿ってgcloud auth application-default set-quota-project <選んだプロジェクトID>して解決しています
とあるので

gcloud auth application-default set-quota-project 選んだプロジェクトID

とした

Credentials saved to file: [/Users/snowpool/.config/gcloud/application_default_credentials.json]

These credentials will be used by any library that requests Application Default Credentials (ADC).

Quota project "選んだプロジェクトID" was added to ADC which can be used by Google client libraries for billing and quota. Note that some services may still bill the project owning the resource.

というログが出る

ls ~/.config/gcloud/    

で調べたら

access_tokens.db			default_configs.db
active_config				gce
application_default_credentials.json	legacy_credentials
config_sentinel				logs
configurations				virtenv
credentials.db

となっていた

次に依存ライブラリのインストール

pip install google-cloud-vision

しかし

Collecting google-cloud-vision
  Downloading google_cloud_vision-3.7.3-py2.py3-none-any.whl.metadata (5.2 kB)
Requirement already satisfied: google-api-core!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-cloud-vision) (2.11.0)
Requirement already satisfied: google-auth!=2.24.0,!=2.25.0,<3.0.0dev,>=2.14.1 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-cloud-vision) (2.18.0)
Collecting proto-plus<2.0.0dev,>=1.22.3 (from google-cloud-vision)
  Downloading proto_plus-1.24.0-py3-none-any.whl.metadata (2.2 kB)
Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0dev,>=3.20.2 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-cloud-vision) (3.20.3)
Requirement already satisfied: googleapis-common-protos<2.0dev,>=1.56.2 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-cloud-vision) (1.59.0)
Requirement already satisfied: requests<3.0.0dev,>=2.18.0 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-cloud-vision) (2.28.2)
Collecting grpcio<2.0dev,>=1.33.2 (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-cloud-vision)
  Downloading grpcio-1.65.1-cp310-cp310-macosx_12_0_universal2.whl.metadata (3.3 kB)
Collecting grpcio-status<2.0dev,>=1.33.2 (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-cloud-vision)
  Downloading grpcio_status-1.65.1-py3-none-any.whl.metadata (1.1 kB)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0dev,>=2.14.1->google-cloud-vision) (5.3.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0dev,>=2.14.1->google-cloud-vision) (0.3.0)
Requirement already satisfied: six>=1.9.0 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0dev,>=2.14.1->google-cloud-vision) (1.16.0)
Requirement already satisfied: urllib3<2.0 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0dev,>=2.14.1->google-cloud-vision) (1.26.14)
Requirement already satisfied: rsa<5,>=3.1.4 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0dev,>=2.14.1->google-cloud-vision) (4.9)
INFO: pip is looking at multiple versions of grpcio-status to determine which version is compatible with other requirements. This could take a while.
  Downloading grpcio_status-1.64.1-py3-none-any.whl.metadata (1.1 kB)
  Downloading grpcio_status-1.64.0-py3-none-any.whl.metadata (1.1 kB)
  Downloading grpcio_status-1.63.0-py3-none-any.whl.metadata (1.1 kB)
  Downloading grpcio_status-1.62.2-py3-none-any.whl.metadata (1.3 kB)
Collecting protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0dev,>=3.20.2 (from google-cloud-vision)
  Downloading protobuf-4.25.3-cp37-abi3-macosx_10_9_universal2.whl.metadata (541 bytes)
Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from pyasn1-modules>=0.2.1->google-auth!=2.24.0,!=2.25.0,<3.0.0dev,>=2.14.1->google-cloud-vision) (0.5.0)
Requirement already satisfied: charset-normalizer<4,>=2 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-cloud-vision) (3.0.1)
Requirement already satisfied: idna<4,>=2.5 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-cloud-vision) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in ./.pyenv/versions/3.10.6/lib/python3.10/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-cloud-vision) (2022.12.7)
Downloading google_cloud_vision-3.7.3-py2.py3-none-any.whl (466 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 466.4/466.4 kB 4.3 MB/s eta 0:00:00
Downloading proto_plus-1.24.0-py3-none-any.whl (50 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 50.1/50.1 kB 2.5 MB/s eta 0:00:00
Downloading grpcio-1.65.1-cp310-cp310-macosx_12_0_universal2.whl (10.4 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.4/10.4 MB 8.5 MB/s eta 0:00:00
Downloading grpcio_status-1.62.2-py3-none-any.whl (14 kB)
Downloading protobuf-4.25.3-cp37-abi3-macosx_10_9_universal2.whl (394 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 394.2/394.2 kB 8.9 MB/s eta 0:00:00
Installing collected packages: protobuf, grpcio, proto-plus, grpcio-status, google-cloud-vision
  Attempting uninstall: protobuf
    Found existing installation: protobuf 3.20.3
    Uninstalling protobuf-3.20.3:
      Successfully uninstalled protobuf-3.20.3
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
mediapipe 0.10.9 requires protobuf<4,>=3.11, but you have protobuf 4.25.3 which is incompatible.
Successfully installed google-cloud-vision-3.7.3 grpcio-1.65.1 grpcio-status-1.62.2 proto-plus-1.24.0 protobuf-4.25.3

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: pip install --upgrade pip

となる

chatgptによれば
インストールは成功したようですが、mediapipeパッケージとprotobufのバージョンに互換性の問題があります。mediapipeはprotobufの3.11から4未満のバージョンを必要としていますが、現在インストールされているprotobufのバージョンは4.25.3です。
とのこと

提示された解決策は
1. protobufのバージョンを互換性のあるバージョンにダウングレードする:
bashコードをコピーする

pip install protobuf==3.20.3
これにより、protobufをmediapipeと互換性のあるバージョンに戻すことができますが、google-cloud-visionと競合する可能性があります。
2. mediapipeの新しいバージョンを探す: mediapipeのドキュメントや問題追跡システムをチェックして、新しいprotobufバージョンと互換性のあるmediapipeのバージョンやワークアラウンドを探します。
3. 仮想環境を使用する: プロジェクトごとに異なるバージョンのパッケージを必要とする場合は、仮想環境を作成することをお勧めします。これにより、パッケージの競合を避けることができます。

とのこと

mediapipeの最新バージョンは?
最新のmediapipeのバージョンは0.10.14です。このバージョンは2024年5月8日にリリースされました。インストールするには、以下のコマンドを使用してください:

pip install mediapipe==0.10.14

とのことなので
これを実行

再度

pip install google-cloud-vision

を実行するとエラーは消えた

次はサンプルコードを動かす

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です