Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

shutdown_demo.py code from AIY Projects Essential Guide #137

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 0 additions & 83 deletions HACKING.md

This file was deleted.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ check:
PYTHONPATH=$$PWD/src python3 -m unittest discover tests

deploy_scripts:
git ls-files | rsync -avz --exclude=".*" --exclude="*.desktop" --files-from - . pi@$(PI):~/voice-recognizer-raspi
git ls-files | rsync -avz --exclude=".*" --exclude="*.desktop" --files-from - . pi@$(PI):~/AIY-voice-kit-python

deploy_shortcuts:
scp $(SHORTCUTS) pi@$(PI):~/Desktop
Expand Down
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
This repository contains the source code for the AIYProjects "Voice Kit". See
https://aiyprojects.withgoogle.com/voice/.

If you're using Raspbian instead of Google's provided image, read
[HACKING.md](HACKING.md) for information on getting started.

[![Build Status](https://travis-ci.org/google/aiyprojects-raspbian.svg?branch=master)](https://travis-ci.org/google/aiyprojects-raspbian/builds)
[![Test Coverage](https://codecov.io/gh/google/aiyprojects-raspbian/branch/master/graph/badge.svg)](https://codecov.io/gh/google/aiyprojects-raspbian)

## Troubleshooting

The scripts in the `checkpoints` directory verify the Raspberry Pi's setup.
They can be run from the desktop shortcuts or from the terminal.
This repository contains the APIs and demo apps for the AIYProjects. See
https://aiyprojects.withgoogle.com.
50 changes: 0 additions & 50 deletions checkpoints/check_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
"""Check that the voiceHAT audio input and output are both working."""

import os
import subprocess
import sys
import tempfile
import textwrap
import time
import traceback

sys.path.append(os.path.realpath(os.path.join(__file__, '..', '..')) + '/src/')
Expand All @@ -30,10 +28,6 @@
CARDS_PATH = '/proc/asound/cards'
VOICEHAT_ID = 'googlevoicehat'

SERVICE_NAME = 'voice-recognizer'
ACTIVE_STR = 'ActiveState=active'
INACTIVE_STR = 'ActiveState=inactive'

STOP_DELAY = 1.0

TEST_SOUND_PATH = '/usr/share/sounds/alsa/Front_Center.wav'
Expand All @@ -57,20 +51,6 @@ def get_sound_cards():
return cards


def is_service_active():
"""Return True if the voice-recognizer service is active."""
output = subprocess.check_output(['systemctl', 'show', SERVICE_NAME]).decode('utf-8')

if ACTIVE_STR in output:
return True
elif INACTIVE_STR in output:
return False

print('WARNING: failed to parse output:')
print(output)
return False


def ask(prompt):
"""Get a yes or no answer from the user."""
ans = input(prompt + ' (y/n) ')
Expand All @@ -81,29 +61,6 @@ def ask(prompt):
return ans[0].lower() == 'y'


def stop_service():
"""Stop the voice-recognizer so we can use the mic.

Returns:
True if the service has been stopped.
"""
if not is_service_active():
return False

subprocess.check_call(['sudo', 'systemctl', 'stop', SERVICE_NAME], stdout=subprocess.PIPE)
time.sleep(STOP_DELAY)
if is_service_active():
print('WARNING: failed to stop service, mic may not work.')
return False

return True


def start_service():
"""Start the voice-recognizer again."""
subprocess.check_call(['sudo', 'systemctl', 'start', SERVICE_NAME], stdout=subprocess.PIPE)


def check_voicehat_present():
"""Check that the voiceHAT is present."""
return any(VOICEHAT_ID in card for card in get_sound_cards().values())
Expand All @@ -112,7 +69,6 @@ def check_voicehat_present():
def check_voicehat_is_first_card():
"""Check that the voiceHAT is the first card on the system."""
cards = get_sound_cards()

return 0 in cards and VOICEHAT_ID in cards[0]


Expand Down Expand Up @@ -174,14 +130,8 @@ def do_checks():


def main():
"""Run all checks, stopping the voice-recognizer if necessary."""
should_restart = stop_service()

do_checks()

if should_restart:
start_service()


if __name__ == '__main__':
try:
Expand Down
52 changes: 29 additions & 23 deletions checkpoints/check_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,30 @@

import json
import os
import subprocess
import os.path
import sys
import traceback

if os.path.exists('/home/pi/credentials.json'):
sys.path.append(os.path.realpath(os.path.join(__file__, '..', '..')) + '/src/')

import aiy._apis._speech # noqa

OLD_CREDENTIALS_FILE = os.path.expanduser('~/credentials.json')
NEW_CREDENTIALS_FILE = os.path.expanduser('~/cloud_speech.json')
if os.path.exists(OLD_CREDENTIALS_FILE):
# Legacy fallback: old location of credentials.
CREDENTIALS_PATH = '/home/pi/credentials.json'
CREDENTIALS_PATH = OLD_CREDENTIALS_FILE
else:
CREDENTIALS_PATH = '/home/pi/cloud_speech.json'
CREDENTIALS_PATH = NEW_CREDENTIALS_FILE

VOICE_RECOGNIZER_PATH = os.path.realpath(os.path.join(__file__, '..', '..'))
PYTHON3 = VOICE_RECOGNIZER_PATH + '/env/bin/python3'
SPEECH_PY = VOICE_RECOGNIZER_PATH + '/src/speech.py'
ROOT_PATH = os.path.realpath(os.path.join(__file__, '..', '..'))
PYTHON3 = ROOT_PATH + '/env/bin/python3'
SPEECH_PY = ROOT_PATH + '/src/aiy/_apis/_speech.py'
SPEECH_PY_ENV = {
'VIRTUAL_ENV': VOICE_RECOGNIZER_PATH + '/env',
'PATH': VOICE_RECOGNIZER_PATH + '/env/bin:' + os.getenv('PATH'),
'VIRTUAL_ENV': ROOT_PATH + '/env',
'PATH': ROOT_PATH + '/env/bin:' + os.getenv('PATH'),
}
TEST_AUDIO = VOICE_RECOGNIZER_PATH + '/checkpoints/test_hello.raw'
TEST_AUDIO = ROOT_PATH + '/checkpoints/test_hello.raw'
RECOGNIZED_TEXT = 'hello'


Expand All @@ -48,23 +55,22 @@ def check_credentials_valid():


def check_speech_reco():
"""Try to test the speech reco code from voice-recognizer-raspi."""
"""Try to test the speech recognition code from AIY APIs."""
print('Testing the Google Cloud Speech API...')
p = subprocess.Popen( # pylint: disable=invalid-name
[PYTHON3, SPEECH_PY, TEST_AUDIO], env=SPEECH_PY_ENV,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = p.communicate()[0].decode('utf-8')

if p.returncode:
print('Speech recognition failed with', p.returncode)
print(output)
return False
req = aiy._apis._speech.CloudSpeechRequest(CREDENTIALS_PATH)
with open(TEST_AUDIO, 'rb') as f:
while True:
chunk = f.read(64000)
if not chunk:
break
req.add_data(chunk)
req.end_audio()
output = req.do_request()

# speech.py succeeded, check the text was recognized
if RECOGNIZED_TEXT in output:
return True

print('Speech recognition output not as expected:')
print('Speech recognition failed or output not as expected:')
print(output)
print('Expected:', RECOGNIZED_TEXT)
return False
Expand Down Expand Up @@ -96,6 +102,6 @@ def main():
try:
main()
input('Press Enter to close...')
except Exception: # pylint: disable=W0703
except: # pylint: disable=bare-except
traceback.print_exc()
input('Press Enter to close...')
3 changes: 2 additions & 1 deletion checkpoints/check_wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
def check_wifi_is_configured():
"""Check wpa_supplicant.conf has at least one network configured."""
output = subprocess.check_output(['sudo', 'cat', WPA_CONF_PATH]).decode('utf-8')

return 'network=' in output


Expand Down Expand Up @@ -74,6 +75,6 @@ def main():
try:
main()
input('Press Enter to close...')
except Exception: # pylint: disable=W0703
except: # pylint: disable=bare-except
traceback.print_exc()
input('Press Enter to close...')
Loading