The challenge we face as we move forward in this class is that certain important functions which we need are ‘Blocking’ in nature. That is, they block the remainder of the program as they wait for input. For example, imagine blinking an LED and having the user input the delay time. When the program is waiting for user input, it can not continue to perform the blinking operation. This is also true for speech input. While the program waits for you to say something, execution of the remainder of the program stalls. To overcome this, we use threads. Threads are functions, or small snippets of code, which we can have execute in the background. In today’s lesson, I will show you how to incorporate threading into your AI projects.
In today’s lesson, this is the code we developed.
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
fromfusion_hat.pin importPin,Mode
importthreading
fromqueueimportQueue
fromtimeimportsleep
redPin=17
redLED=Pin(redPin,Mode.OUT)
redLED.low()
blinkDelay=1.0
running=True
blinkQ=Queue()
defblinkTime():
print("Input Thread is Running")
globalrunning
whilerunning:
cmd=input("Input Blink Time in Seconds, or Q for Quit: ")
In this video lesson I show you my solution to the homework assignment I gave in LESSON #10. The assignment was to control an LED using voice commands on the Raspberry Pi. This uses the Speech To Text expertise we developed in the last few lessons, but incorporates them into a real world project. With this basic framework, you are now equipped to make speech part of your future Raspberry Pi projects.
This is the schematic of the circuit we are using for our AI class. We go into great detail on this schematic in LESSON #5 if you want to learn more about it.
This is the circuit we will use moving forward in the class
Now this is the code we developed in this lesson:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fromfusion_hat.stt importSTT
fromfusion_hat.pin importPin,Mode
fromtimeimportsleep
redPin=17
redLED=Pin(redPin,mode=Mode.OUT)
stt=STT(language="en-us")
whileTrue:
print("Voice Assistant At the READY!, Speak at Any Time: ")
In this video lesson you will learn how to train the Raspberry Pi to take voice commands from you. We do this through the Fusion AI+ hat’s microphone, and a Speech to Text (STT) model. Our goal is to develop the ability to interact with our projects through spoken words. We give commands to the project, and then it responds intelligently with words.
Remember these lessons depend on you using the AI Educational OS, a special version of bookworm that has all the libraries, modules, and models already installed for you. See LESSON #2 in this class for instructions on flashing that OS.
Below is the simple demonstration code we developed to give simple voice commands:
Python
1
2
3
4
5
6
7
8
fromfusion_hat.stt importSTT
stt=STT(language="en-us")
whileTrue:
print("Say Something: ")
command=stt.listen(stream=False)
print(command)
Similar to our Speech to Text example, the first time you run this program you will get a permissions error. You need to open a terminal, and put these commands in one at a time to enable permissions. This only has to be done once, and after that this and all STT programs should run properly.
In this video lesson I will show you how to get the Raspberry Pi to speak to you in plain English. This is our first dabbling with AI. In earlier lessons we have discussed that one of our first objectives will be to begin to audibly interact with our project through speech. The first step will be to get the Pi to talk to us. Then in future lessons we will show how to get the Pi to listen to us.
In this lesson we demonstrated simple Text to Speech (TTS) with this code.
Remember this program requires use of the AI Educational OS we flashed in LESSON #2.
1
2
3
4
5
from fusion_hat.tts import Piper
tts=Piper()
tts.set_model('en_US-amy-low')
msg="Hi, I'm piper TTS. Are you Ready to Rumble? I am here to pump you up!"
tts.say(msg,stream=False)
As we say in the video, the first time you run the program you will get a permission error. This is because the model folders are inside a system folder and must be created as a ‘superuser’ using ‘sudo’. As shown in the video, you need to open a terminal window, and type in these commands at the command prompt (Put them in one at a time):
Shell
1
2
3
sudo mkdir-p/opt/piper_models
sudo chown-Rpi:pi/opt/piper_models
sudo chmod-R755/opt/piper_models
You only need to do that one time. Next time you run the program, all will work properly.
Then, in order to hear all the different voice models Piper offers, you can run this program, and each voice will introduce itself to you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from fusion_hat.tts import Piper
from time import sleep# Optional: for pauses between voices
device.enable_speaker()
tts=Piper()
# Get the dictionary of available en_US models
voiceDict=tts.available_models('en_US')
print(voiceDict)
# Flatten into a single list of full model names
allVoices=[]
forvoice invoiceDict.values():
forvarinvoice:
allVoices.append(var)
print(allVoices)
msg="Hello World. This is a test message."
forvoice inallVoices:
tts.set_model(voice)# Auto-downloads on first use
# Create a readable voice name (remove 'en_US-' and replace '_' with space)
In this video lesson we present the solution to the homework assignment given in LESSON #6. Your assignment was to create a dimmable LED where the brightness of the RGB LED is controlled by the potentiometer. We are still using the schematic from our earlier project.
This is the circuit we will use moving forward in the class
In this lesson, this is the code which we came up with:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python3
fromfusion_hat.adc importADC
fromfusion_hat.pwm importPWM
fromtimeimportsleep
importmath
potPin=0
myPot=ADC(0)
redPin=5
redLED=PWM(redPin)
whileTrue:
potVal=myPot.read()# 0 to 4095
writeVal=100**(potVal/4095)-.005
redLED.pulse_width_percent(int(writeVal))
print(potVal,writeVal)
sleep(0.1)
Making The World a Better Place One High Tech Project at a Time. Enjoy!
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok