The codes are already premade, and I’m clueless on how to execute it via python.
for code reference, here’s the link for the actual project.
The codes are already premade, and I’m clueless on how to execute it via python.
for code reference, here’s the link for the actual project.
@arsenio_4112, grab the full file and copy to the PI in a file name anything.py…
Then install these dependices (assuming Raspbian)
sudo apt update
sudo apt upgrade
sudo apt install python3-rpi.gpio python3-setuptools python3-pip python3-pyaudio
sudo pip3 install SpeechRecognition
Then just run the demo example…
python3 ./demo.py
import RPi.GPIO as GPIO
import time
import speech_recognition as sr
# Configure Varaibles
command = ""
password = "open this door please"
voiceOK = False
r = sr.Recognizer()
# Configure GPIO
solenoidPin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(solenoidPin, GPIO.OUT)
GPIO.output(solenoidPin, GPIO.LOW)
while(1):
with sr.Microphone() as source:
voiceOK = False
# Keep running until the voice is understood by Google
while(voiceOK == False):
print("Speak:")
audio = r.listen(source)
try:
speechString = r.recognize_google(audio)
print(speechString)
voiceOK = True
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
# Determine if the password is correct
if(speechString == password):
GPIO.output(solenoidPin, GPIO.HIGH)
print("ACCESS GRANTED")
time.sleep(2)
GPIO.output(solenoidPin, GPIO.LOW)
else:
print("ACCESS DENIED")
Regards,
Thanks for the tips on the terminal code to update and install the speech software. Where do I put the program code so I can turn it on and start using it?
Also, what does “voiceOK = False” mean? Where can I go to learn those terms, is that just something in Python, or specific to the speech recognition software? Whenever I search for this term it just brings up this article, and the original one that this code came from.
Hi @rgenck, Just create a new file on your system and copy the code, remember it’s python so indentation matters…
https://www.digikey.com/en/maker/projects/77508022b7d64df0955fb793f04af5be
No it just looks like the original Author of this demo application, just decided to keep looping till it detected a trigger word. It could have just been written as a ‘while true’ and break on detection. Just a personal choice to use “voiceOK = false” to break out…
Regards,
Thanks again for the tips, I started taking a Python course online cause I can see how important this will be to use Raspberry Pi.
I actually thought of a way to improve that code, I noticed that if the password and voice input had different case settings, the code wouldn’t work. This updated code will make the password and voice input both be lowercase all the time:
import RPi.GPIO as GPIO
import time
import speech_recognition as sr
# Configure Varaibles
command = ""
password = "open this lock"
password = password.lower()
voiceOK = False
r = sr.Recognizer()
# Configure GPIO
solenoidPin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(solenoidPin, GPIO.OUT)
GPIO.output(solenoidPin, GPIO.LOW)
while(1):
with sr.Microphone() as source:
voiceOK = False
# Keep running until the voice is understood by Google
while(voiceOK == False):
print("Speak:")
audio = r.listen(source)
try:
speechString = r.recognize_google(audio)
print(speechString.lower())
voiceOK = True
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
# Determine if the password is correct
if(speechString == password):
GPIO.output(solenoidPin, GPIO.HIGH)
print("ACCESS GRANTED")
time.sleep(1)
GPIO.output(solenoidPin, GPIO.LOW)
else:
print("ACCESS DENIED")