NOOB Python Script ...
 
Notifications
Clear all

NOOB Python Script Problem

4 Posts
3 Users
0 Likes
957 Views
DavidTN
(@davidtn)
Member
Joined: 4 years ago
Posts: 3
Topic starter  

Hello,

I'm just a few days into trying to get the Google Voice Hat working with the scripts that Bill wrote a few years back.  I had to dig around to find updates since the image has changed so much since the original post by Bill.   At this point my Python skills are non existent.

I updated the auth_helpers.py script and changed the last argument in webbrowser.register() (line 75) from "-1" to "preferred=True".

I changed references to a few libraries to what I think are their new names. 

import aiy.voice to import aiy.voice.audio
import aiy.voicehat to import aiy.board

I'm down to what I hope is the last error.  I'm using the November 2019 AIY build since this is the only one that I can get to boot on my Pi 4, 4GB, 32GB SD.  My dumb guess is that the code is looking for the model of my Pi but that it's not defined somewhere because the Pi 4 didn't exist when the code was written.  Any insight would help.  As I'm a noob, please be as detailed as possible. It's a learning experience for me.

Python 3.7.3 (/usr/bin/python3)

>>> %cd /home/pi/AIY-voice-kit-python/src/examples/voice
>>> %Run dbws_motorcontrol.py
Traceback (most recent call last):
File "/home/pi/AIY-projects-python/src/examples/voice/dbws_motorcontrol.py", line 147, in <module>
main()
File "/home/pi/AIY-projects-python/src/examples/voice/dbws_motorcontrol.py", line 141, in main
with Assistant(credentials) as assistant:
TypeError: __init__() missing 1 required positional argument: 'device_model_id'

Here is Bill's script with my changes:

#!/usr/bin/env python3
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# dbws_motorcontrol.py
# Controls DCMotor Speed
# Modified by DroneBot Workshop
# 2018-02-20
#

"""Run a recognizer using the Google Assistant Library.

The Google Assistant Library has direct access to the audio API, so this Python
code doesn't need to record audio. Hot word detection "OK, Google" is supported.

The Google Assistant Library can be installed with:
env/bin/pip install google-assistant-library==0.0.2

It is available for Raspberry Pi 2/3 only; Pi Zero is not supported.
"""

import logging
import subprocess
import sys

import aiy.assistant.auth_helpers
import aiy.voice.audio
import aiy.board
from google.assistant.library import Assistant
from google.assistant.library.event import EventType

import RPi.GPIO as GPIO

from gpiozero import PWMOutputDevice

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(26,GPIO.OUT)
GPIO.setup(6,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)

motor1 = PWMOutputDevice(4)

logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
)

def motor_on_full():
aiy.audio.say('Motor On. Full Speed')
motor1.value = 1
GPIO.output(26,False)
GPIO.output(6,False)
GPIO.output(13,True)

def motor_off():
aiy.audio.say('Motor Off')
motor1.value = 0
GPIO.output(26,True)
GPIO.output(6,False)
GPIO.output(13,False)

def motor_on_half():
aiy.audio.say('Motor On. Half speed')
motor1.value = 0.5
GPIO.output(26,False)
GPIO.output(6,True)
GPIO.output(13,False)

def power_off_pi():
aiy.audio.say('Good bye!')
subprocess.call('sudo shutdown now', shell=True)

def reboot_pi():
aiy.audio.say('See you in a bit!')
subprocess.call('sudo reboot', shell=True)

def say_ip():
ip_address = subprocess.check_output("hostname -I | cut -d' ' -f1", shell=True)
aiy.audio.say('My IP address is %s' % ip_address.decode('utf-8'))

def process_event(assistant, event):
status_ui = aiy.board.get_status_ui()
if event.type == EventType.ON_START_FINISHED:
status_ui.status('ready')
if sys.stdout.isatty():
print('Say "OK, Google" then speak, or press Ctrl+C to quit...')

elif event.type == EventType.ON_CONVERSATION_TURN_STARTED:
status_ui.status('listening')

elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args:
print('You said:', event.args['text'])
text = event.args['text'].lower()
if text == 'power off':
assistant.stop_conversation()
power_off_pi()
elif text == 'reboot':
assistant.stop_conversation()
reboot_pi()
elif text == 'ip address':
assistant.stop_conversation()
say_ip()
elif text == 'motor on':
assistant.stop_conversation()
motor_on_full()
elif text == 'motor off':
assistant.stop_conversation()
motor_off()
elif text == 'motor half':
assistant.stop_conversation()
motor_on_half()

elif event.type == EventType.ON_END_OF_UTTERANCE:
status_ui.status('thinking')

elif event.type == EventType.ON_CONVERSATION_TURN_FINISHED:
status_ui.status('ready')

elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args['is_fatal']:
sys.exit(1)

def main():
credentials = aiy.assistant.auth_helpers.get_assistant_credentials()
with Assistant(credentials) as assistant:
for event in assistant.start():
process_event(assistant, event)

if __name__ == '__main__':
main()

 


   
Quote
Ddaved
(@ddaved)
Member
Joined: 3 years ago
Posts: 13
 

 

IMG 2408

Trouble here also. Following your post, my errors are as in attached picture.

I don't know where auth_helpers.py is to edit it.

don't know where libraries are to rename them.

I appreciate any help you might provide.

 

This post was modified 3 years ago by Ddaved

   
ReplyQuote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1051
 

@ddaved You DO realize that you posted this in the Test Forum and that no one will see your question, right?

Just checking, as I'll be deleting the posts here very soon and I didn't want you to be surprised when it disappeared!

😎

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
Ddaved
(@ddaved)
Member
Joined: 3 years ago
Posts: 13
 

Thank you, Bill. I'll look for the correct place to post. The status of this posting has changed significantly since I made this request. Good health to you.


   
ReplyQuote