Save 37% off PRO during our Black Friday Sale! »

Google Assistant with Raspberry Pi

Google Assistant with Raspberry Pi

D2bcabeeb1ddff142fb8988b412cb4d3?s=128

Yuki Anzai

June 22, 2017
Tweet

Transcript

  1. Raspberry Pi Ͱ Google Assistant Λମݧ͠Α͏ ͋Μ͍͟Ώ͖ʢ@yanzmʣ GTUG Girls Meet

    Up #33
  2. ࢿྉ goo.gl/CDHpi4

  3. Google Assistant • https://assistant.google.com/ • SDK • https://developers.google.com/assistant/sdk/ prototype/getting-started-pi-python/

  4. ඞཁͳ΋ͷ • Raspberry Pi 3ʢRaspberry Pi 2 + WiFiυϯάϧͰ΋ Մʣ

    • Raspbian JessieͷΠϯετʔϧ͞ΕͨmicroSD • ిݯΞμϓλ ΋͘͠͸ microUSBέʔϒϧ • USB ϚΠΫ ΋͘͠͸ϚΠΫ಺ଂܕͷUSBΧϝϥ • εϐʔΧʔʢUSB or 3.5mmδϟοΫʣ
  5. ४උ • Raspberry Pi Λ WiFi ʹ઀ଓ͢Δ

  6. ४උ • Raspberry Pi ͰίϯιʔϧΛ։͖ɺifconfig ίϚϯ υΛ࣮ߦͯ͠IPΞυϨεΛϝϞ͢Δ

  7. ४උ • Raspberry Pi Ͱ SSH Λ༗ޮʹ͢Δ • Preferences -

    Raspberry Pi Configuration - Interfaces • SSH Λ Enabled ʹͯ͠ OK ΛΫϦοΫ
  8. ४උ2 • ࣌ࠁઃఆΛ֬ೝ͢Δʢ͕࣌ؒਖ਼͚͠Ε͹OKʣ • JST ʹ͍ͨ͠ͳΒ • Preferences - Raspberry

    Pi Configuration - Localisation • Set Timezone ΛΫϦοΫ • Area: Asia, Location: Tokyo • ࠶ىಈ
  9. ४උ3 • Raspberry Pi ʹ PC ͔Β SSH ͰϩάΠϯ͢Δ $

    ssh pi@[ϝϞͨ͠IPΞυϨε] password: [σϑΥϧτ͸raspberry] • ࣌ࠁઃఆΛ֬ೝ͢Δʢ͕࣌ؒਖ਼͚͠Ε͹OKʣ $ date
  10. ։ൃϓϩδΣΫτͱ ΞΧ΢ϯτͷઃఆ • https://developers.google.com/assistant/sdk/ prototype/getting-started-pi-python/config-dev- project-and-account Λ։͘ • goo.gl/GxLUSW •

    Ҏ߱ɺ͜ͷϖʔδΛʮઃఆखॱυΩϡϝϯτʯͱݺ ͼ·͢ɻ
  11. ৽͍͠ϓϩδΣΫτΛ࡞Δ - 1 • ʮઃఆखॱυΩϡϝϯτʯʹ͋Δ GO TO THE PROJECT PAGE

    ϘλϯΛΫϦοΫ͢Δ
  12. ৽͍͠ϓϩδΣΫτΛ࡞Δ - 2 • ্ͷํʹ͋ΔϓϩδΣΫτΛ࡞੒ΛΫϦοΫ • ϓϩδΣΫτ໊Λೖྗͯ͠࡞੒ΛΫϦοΫ • ͳΜͰ΋Α͍Ͱ͢ɻAssistant with

    Raspberry Pi ͱ͔
  13. Google Assistant API ༗ޮԽ • ʮઃఆखॱυΩϡϝϯτʯʹ͋Δ ENABLE THE API ϘλϯΛΫϦοΫ͢Δ

  14. OAuth Client ID ͷઃఆ - 1 • OAuth ಉҙը໘Λઃఆ͢Δ

  15. OAuth Client ID ͷઃఆ - 2 • OAuth ΫϥΠΞϯτ ID

    Λ࡞੒͢Δ
  16. OAuth Client ID ͷઃఆ - 3 • OAuth ΫϥΠΞϯτ ID

    Λ࡞੒͢Δ • μΠΞϩά͕දࣔ͞ΕͨΒ OK ΛΫϦοΫ
  17. OAuth Client ID ͷઃఆ - 4 • ΫϥΠΞϯτ ID secret

    JSON ϑΝΠϧΛμ΢ϯ ϩʔυ
  18. OAuth Client ID ͷઃఆ - 5 • μ΢ϯϩʔυͨ͠ΫϥΠΞϯτ ID secret

    JSON ϑΝΠϧΛ Raspberry Pi ʹసૹ $ scp [μ΢ϯϩʔυͨ͠JSONϑΝΠϧ] pi@[ϝϞ ͨ͠IPΞυϨε]:/home/pi/ password: [σϑΥϧτ͸raspberry]
  19. ΞΫςΟϏςΟίϯτϩʔϧͷ ઃఆ - 1 • ʮઃఆखॱυΩϡϝϯτʯͷԼͷํʹ͋Δ Activity Controls page ͷϦϯΫΛ։͘

  20. ΞΫςΟϏςΟίϯτϩʔϧͷ ઃఆ - 2 • ҎԼͷ߲໨Λ༗ޮʹ͢Δ • ΢ΣϒͱΞϓϦͷΞΫςΟϏςΟ • ϩέʔγϣϯཤྺ

    • ୺຤৘ใ • Ի੠ΞΫςΟϏςΟ
  21. Ի੠ͷઃఆͱ֬ೝ - 0 • RaspberryPi ʹ SSH ͰϩάΠϯ͢Δ

  22. Ի੠ͷઃఆͱ֬ೝ - 1 • εϐʔΧʔͷςετ • Front, Left ͕܁Γฦ͠ฉ͑͜Ε͹OK •

    Ի͕໐Βͳ͍ਓ͸࣍ͷεϥΠυ΁ $ speaker-test -t wav
  23. εϐʔΧʔ͔ΒԻ͕໐Βͳ͍ ਓ͸͜ΕΛ΍Δ $ sudo raspi-config • ʲAdvanced OptionsʳΛબ୒ • ʲAudioʳΛબ୒

    • ʲForce 3.5mmʢ'headphone'ʣjackʳΛબ୒
  24. Ի੠ͷઃఆͱ֬ೝ - 2 • ϚΠΫͷςετʢ5ඵؒ࿥Իʣ • Τϥʔ͕ग़ͳ͔ͬͨΒద౰ʹ͠Ό΂ͬͯ 7 ΁ •

    Τϥʔ͕ग़ͨΒ 3ʢ࣍ʣ΁ $ arecord --format=S16_LE --duration=5 -- rate=16k --file-type=raw out.raw
  25. Ի੠ͷઃఆͱ֬ೝ - 3 • ϚΠΫͷΧʔυ൪߸ͱσόΠε൪߸Λௐ΂Δ • ͜ΕͳΒΧʔυ൪߸͸ 1, σόΠε൪߸͸ 0

    $ arecord -l **** List of CAPTURE Hardware Devices **** card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  26. Ի੠ͷઃఆͱ֬ೝ - 4 • εϐʔΧʔͷΧʔυ൪߸ͱσόΠε൪߸Λௐ΂Δ • ͜ΕͳΒΧʔυ൪߸͸ 0, σόΠε൪߸͸ 0

    $ aplay -l **** List of PLAYBACK Hardware Devices **** card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  27. Ի੠ͷઃఆͱ֬ೝ - 5 • SSH ͷ઀ଓΛ੾Δ • ઃఆϑΝΠϧΛ࡞Δ • ϑΝΠϧ໊

    : .asoundrc • ಺༰: ࣍ͷεϥΠυ
  28. pcm.!default { type asym capture.pcm "mic" playback.pcm "speaker" } pcm.mic

    { type plug slave { pcm "hw:[ϚΠΫͷΧʔυ൪߸],[ϚΠΫͷσόΠε൪߸]" } } pcm.speaker { type plug slave { pcm "hw:[εϐʔΧʔͷΧʔυ൪߸],[εϐʔΧʔͷσόΠε൪߸]" } }
  29. pcm.!default { type asym capture.pcm "mic" playback.pcm "speaker" } pcm.mic

    { type plug slave { pcm "hw:1,0" } } pcm.speaker { type plug slave { pcm "hw:1,1" } } ྫʣ
  30. Ի੠ͷઃఆͱ֬ೝ - 6 • Raspberry Pi ͷ /home/pi/ ʹஔ͘ •

    2 ʹ໭Δ $ scp .asoundrc pi@[ϝϞͨ͠IPΞυϨε]:/ home/pi/ password: [σϑΥϧτ͸raspberry]
  31. Ի੠ͷઃఆͱ֬ೝ - 7 • 2Ͱ࿥Իͨ͠Ի੠Λ࠶ੜ • ໰୊ͳ͘࿥Ի͞Ε͍ͯΕ͹ OK • Իྔͷௐઅ͸

    $ aplay --format=S16_LE --rate=16k out.raw $ alsamixer
  32. Python ͷઃఆ • Python virtual environment Λ࢖ͬͯγεςϜͷ Python ύοέʔδ͔Β SDK

    ͱґଘ͢Δ΋Ζ΋ΖΛ ִ཭ • ݱঢ় Google Assistant ͷϥΠϒϥϦ͸ Python 3.x ͰͷΈಈ͘ • Πϯετʔϧͷ్தͰฉ͔ΕͨΒ y ͰਐΜͰͩ͘͞ ͍
  33. Python ͷઃఆ $ sudo apt-get update $ sudo apt-get install

    python3-dev python3-venv $ python3 -m venv env $ env/bin/python -m pip install --upgrade pip setuptools $ source env/bin/activate
  34. ϥΠϒϥϦͱαϯϓϧίʔυ Λऔͬͯ͘Δ (env) $ python -m pip install --upgrade google-assistant-library

  35. αϯϓϧίʔυͷೝূ - 1 • ϒϥ΢βΛ։͍ͯ Google ΞΧ΢ϯτʹϩάΠϯ͢ Δ • ΞΫςΟϏςΟίϯτϩʔϧͷઃఆΛͨ͠΍ͭ

    • ೝূπʔϧΛΠϯετʔϧ͢Δ (env) $ python -m pip install --upgrade google-auth-oauthlib[tool]
  36. αϯϓϧίʔυͷೝূ - 2 • ೝূπʔϧΛ࣮ߦ͢Δ • จࣈίʔυͷΤϥʔ͕ग़ͨΒ (env) $ google-oauthlib-tool

    --client- secrets /home/pi/[લʹసૹ͓͍ͯͨ͠JSON] -- scope https://www.googleapis.com/auth/ assistant-sdk-prototype --save --headless (env) $ export LC_ALL=C.UTF-8 (env) $ export LANG=C.UTF-8
  37. • ͱදࣔ͞ΕΔͷͰ … ෦෼ͷ URL Λίϐʔͯ͠ϒϥ ΢βʹషΓ෇͚ͯදࣔ • ΞΧ΢ϯτΛબ୒ʢඞཁͳ৔߹ʣ͠ɺALLOW ʢڐ

    ՄʣΛΫϦοΫ • දࣔ͞ΕΔίʔυΛίϐʔ͠ Please visit this URL to authorize this application: … Enter the authorization code: • ʹೖྗ
  38. αϯϓϧίʔυͷ࣮ߦ • OK, google. What’s the weather in Tokyo •

    ͱݴͬͯΈΑ͏ (env) $ google-assistant-demo