コードはあらかじめ作成されていますが、pythonで実行する方法がわかりません。
https://www.digikey.com/en/maker/projects/77508022b7d64df0955fb793f04af5be
上記のリンクから実際のプロジェクトのコードを参照いただけます。
RobertCNelson Applications Engineer
arsenio_4112さん、完全なファイルを取得して、anything.py…というファイル名でPiにコピーしてください。
そして、以下の依存ファイルをインストールしてください(OSはRaspbian を想定しています) 。
sudo apt update sudo apt upgrade sudo apt install python3-rpi.gpio python3-setuptools python3-pip python3-pyaudio
sudo pip3 install SpeechRecognition
そして、デモの例を実行してみてください…
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")
よろしくお願いします。
rgenck
音声ソフトをアップデートしてインストールするためのターミナルコードのヒントをありがとうございます。電源を入れて動作させるためには、プログラムコードはどこに入れればいいのでしょうか?
また、"voiceOK = False "とはどういう意味ですか?これらの用語を学ぶにはどこを探せばいいのでしょうか?それはPythonの何かなのでしょうか?あるいは音声認識ソフトに特有のものなのでしょうか?この用語を検索すると、この記事と、このコードが出てきた元の記事が出てきます。
RobertCNelson Applications Engineer
こんにちはrgenckさん、システム上に新しいファイルを作成してコードをコピーしてください。それはpythonなので、インデントの問題を覚えておいてください…
https://www.digikey.com/en/maker/projects/77508022b7d64df0955fb793f04af5be
いいえ、このデモアプリケーションのオリジナルの作者が、トリガーワードを検出するまでループし続けることにしたように見えます。'while true'として書かれていて、検出されたときにブレークするようになっていたかもしれません。ただ、個人的には「voiceOK = false」を使ってブーイクさせるという選択をしただけなのですが...。
よろしくお願いします。
ヒントをありがとうございます、Raspberry Piを使用するにあたり、このことがどんなに重要かが分かったので、オンラインでPythonのコースを取ることにしました。
実はこのコードを改善する方法を考えたのですが、パスワードと音声入力の大文字と小文字の設定が異なる場合、コードが動作しないことに気づきました。この更新されたコードでは、パスワードと音声入力の両方を常に小文字にします。
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")