私的AI研究会 > AIカメラ作成

AIカメラを作る

画像認識 Google Vision の設定

カメラの設定

$ raspistill -o image.jpg

Google Vision クイックスタート

Vision AI オフィシャルページ から
「クイックスタート」(「Raspberry Pi + AI 電子工作入門」275ページ)を呼び出す。

 

1.「使ってみる」を押す。

2. 左メニューから「Vision API」を選択。
3. 左メニューから「クライアントライブラリの使用」を選択。

 

Google Vision の設定

※Microsoft Edge or IE11 では不具合があるようなので。Google Chromeを使用する。

1. Google Cloud Console を開く。
検索窓に「cloud vision」と入力して「Cloud Vision API」を選択」する。

2. 「有効にする」を選択。

3. 左メニューより「認証情報」を選択。

4. メニュー中央の「+認証情報の作成」から「サービスアカウント」を選ぶ。

5. サービスアカウント名を入力
  cloudvision
6. 「作成」を押す。

7. ロールの右▼を押し「オーナー」を選ぶ。
8. 「続行」を押す。

9. 「+キーを作成」を押す。

10. 「作成」を押す。

11. キーが作成されて、自動的に秘密鍵がダウンロードされる。
※Microsoft Edge or IE11 ではダウンロードできないので注意。

12. ダウンロードされた秘密鍵をラズパイのホームディレクトリ(/home/pi/)に転送する。

 

Raspberry Pi 上の設定

  1. 認証ファイルを環境変数として読み込ませる。
    $ export GOOGLE_APPLICATION_CREDENTIALS=/home/pi/raspberryai-mizutu-ec573de141fd.json
  2. Client Library インストール
    $ pip install --upgrade google-cloud-vision
    Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
    Collecting google-cloud-vision
      Downloading https://files.pythonhosted.org/packages/0d/7f/e10d602c2dc3f749f1b78377a3357790f1da71b28e7da9e5bc20b3a9bd40/google_cloud_vision-1.0.0-py2.py3-none-any.whl (435kB)
        100% |████████████████████████████████| 440kB 231kB/s 
    Requirement already satisfied, skipping upgrade: enum34; python_version < "3.4" in /usr/lib/python2.7/dist-packages (from google-cloud-vision) (1.1.6)
    Collecting google-api-core[grpc]<2.0.0dev,>=1.14.0 (from google-cloud-vision)
      Downloading https://files.pythonhosted.org/packages/4c/b9/c0dd70bcdf06a43d1e21f387448e7997e0ce91f10d0fbee359af4cde1571/google_api_core-1.17.0-py2.py3-none-any.whl (70kB)
        100% |████████████████████████████████| 71kB 353kB/s 
    Collecting futures>=3.2.0; python_version < "3.2" (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/d8/a6/f46ae3f1da0cd4361c344888f59ec2f5785e69c872e175a748ef6071cdb5/futures-3.3.0-py2-none-any.whl
    Requirement already satisfied, skipping upgrade: requests<3.0.0dev,>=2.18.0 in /usr/lib/python2.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision) (2.21.0)
    Collecting googleapis-common-protos<2.0dev,>=1.6.0 (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/05/46/168fd780f594a4d61122f7f3dc0561686084319ad73b4febbf02ae8b32cf/googleapis-common-protos-1.51.0.tar.gz
    Collecting google-auth<2.0dev,>=1.14.0 (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/d2/f8/1623d69e5de22e499b68a0cb5e5d02cd6a2843e55acc19f314f48fe04299/google_auth-1.14.1-py2.py3-none-any.whl (89kB)
        100% |████████████████████████████████| 92kB 328kB/s 
    Collecting protobuf>=3.4.0 (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/27/9c/ef816295b4b40298fd0a17bf8f0ba6cf3e0c44cb2ce72257168e09996b8b/protobuf-3.11.3-py2.py3-none-any.whl (434kB)
        100% |████████████████████████████████| 440kB 237kB/s 
    Requirement already satisfied, skipping upgrade: setuptools>=34.0.0 in /usr/lib/python2.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision) (40.8.0)
    Requirement already satisfied, skipping upgrade: six>=1.10.0 in /usr/lib/python2.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision) (1.12.0)
    Requirement already satisfied, skipping upgrade: pytz in /usr/lib/python2.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision) (2019.1)
    Collecting grpcio<2.0dev,>=1.8.2; extra == "grpc" (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/a4/16/3138c5bec5255a1b1bf9b1024ecc22b643c4207c7b328535c4303fdc0a37/grpcio-1.28.1-cp27-cp27mu-linux_armv7l.whl (19.8MB)
        100% |████████████████████████████████| 19.8MB 17kB/s 
    Collecting rsa<4.1,>=3.1.4 (from google-auth<2.0dev,>=1.14.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/02/e5/38518af393f7c214357079ce67a317307936896e961e35450b70fad2a9cf/rsa-4.0-py2.py3-none-any.whl
    Collecting pyasn1-modules>=0.2.1 (from google-auth<2.0dev,>=1.14.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/95/de/214830a981892a3e286c3794f41ae67a4495df1108c3da8a9f62159b9a9d/pyasn1_modules-0.2.8-py2.py3-none-any.whl (155kB)
        100% |████████████████████████████████| 163kB 152kB/s 
    Collecting cachetools<5.0,>=2.0.0 (from google-auth<2.0dev,>=1.14.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/2f/a6/30b0a0bef12283e83e58c1d6e7b5aabc7acfc4110df81a4471655d33e704/cachetools-3.1.1-py2.py3-none-any.whl
    Collecting pyasn1>=0.1.3 (from rsa<4.1,>=3.1.4->google-auth<2.0dev,>=1.14.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision)
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Cache entry deserialization failed, entry ignored
      Downloading https://files.pythonhosted.org/packages/62/1e/a94a8d635fa3ce4cfc7f506003548d0a2447ae76fd5ca53932970fe3053f/pyasn1-0.4.8-py2.py3-none-any.whl (77kB)
        100% |████████████████████████████████| 81kB 151kB/s 
    Building wheels for collected packages: googleapis-common-protos
      Running setup.py bdist_wheel for googleapis-common-protos ... done
      Stored in directory: /home/pi/.cache/pip/wheels/2c/f9/7f/6eb87e636072bf467e25348bbeb96849333e6a080dca78f706
    Successfully built googleapis-common-protos
    Installing collected packages: futures, protobuf, googleapis-common-protos, pyasn1, rsa, pyasn1-modules, cachetools, google-auth, grpcio, google-api-core, google-cloud-vision
      The scripts pyrsa-decrypt, pyrsa-encrypt, pyrsa-keygen, pyrsa-priv2pub, pyrsa-sign and pyrsa-verify are installed in '/home/pi/.local/bin' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Successfully installed cachetools-3.1.1 futures-3.3.0 google-api-core-1.17.0 google-auth-1.14.1 google-cloud-vision-1.0.0 googleapis-common-protos-1.51.0 grpcio-1.28.1 protobuf-3.11.3 pyasn1-0.4.8 pyasn1-modules-0.2.8 rsa-4.0
  3. '~/Programs/' ディレクトリにサンプルファイルをダウンロードする。
    $ cd ~/Programs
    $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    Cloning into 'python-docs-samples'...
    remote: Enumerating objects: 78, done.
    remote: Counting objects: 100% (78/78), done.
    remote: Compressing objects: 100% (67/67), done.
    remote: Total 29487 (delta 25), reused 25 (delta 7), pack-reused 29409
    Receiving objects: 100% (29487/29487), 39.48 MiB | 196.00 KiB/s, done.
    Resolving deltas: 100% (15567/15567), done.
    $ ls
    aibot     googlesamples  hello_nihongo.py  image.jpg  python-docs-samples  switch_led2.py     talk2text
    exercise  hello.py       hello_today.py    led.py     switch_led.py        switch_speaker.py
  4. ダウンロードしたサンプルファイルの確認
    $ ls ~/Programs/python-docs-samples
    AUTHORING_GUIDE.md  automl            compute             dns              iap                ml_engine            run            testing
    CONTRIBUTING.md     bigquery          container_registry  document         iot                monitoring           scheduler      texttospeech
    ISSUE_TEMPLATE.md   bigquery_storage  datacatalog         endpoints        jobs               notebooks            scripts        third_party
    LICENSE             bigtable          dataflow            error_reporting  kms                noxfile-template.py  secretmanager  trace
    MAC_SETUP.md        blog              datalabeling        favicon.ico      kubernetes_engine  opencensus           spanner        translate
    README.md           cdn               dataproc            firestore        language           profiler             speech         video
    appengine           cloud-sql         datastore           functions        logging            pubsub               storage        vision
    asset               codelabs          dialogflow          healthcare       media-translation  pytest.ini           tables
    auth                composer          dlp                 iam              memorystore        renovate.json        tasks
    
    $ ls ~/Programs/python-docs-samples/vision/cloud-client/quickstart
    README.rst  README.rst.in  quickstart.py  quickstart_test.py  requirements-test.txt  requirements.txt  resources
  5. 必要ライブラリのインストール
    $ cd ~/Programs/python-docs-samples/vision/cloud-client/quickstart
    $ pip install -r requirements.txt
    Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
    Requirement already satisfied: google-cloud-vision==1.0.0 in /home/pi/.local/lib/python2.7/site-packages (from -r requirements.txt (line 1)) (1.0.0)
    Requirement already satisfied: enum34; python_version < "3.4" in /usr/lib/python2.7/dist-packages (from google-cloud-vision==1.0.0->-r requirements.txt (line 1)) (1.1.6)
      :
      :
    Requirement already satisfied: pyasn1>=0.1.3 in /home/pi/.local/lib/python2.7/site-packages (from rsa<4.1,>=3.1.4->google-auth<2.0dev,>=1.14.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-vision==1.0.0->-r requirements.txt (line 1)) (0.4.8)
    すでに 2. でインストール済み。

Python サンプルプログラムの実行

クイックスタート: クライアント ライブラリの使用 → 「ラベル検出」→ 「Python」


Last-modified: 2020-05-03 (日) 09:10:53