実装
私たちのアシスタントは、次の原則に従って動作します。
- 常にマイクを「聞く」
- グーグルで単語を認識する
- コマンドを実行するか、応答します
1)音声の合成
まず、Windowsシステムにロシア語の音声をインストールします。これを行うには、リンクをたどり、SAPI5->ロシア語セクションの音声をダウンロードします。そこには4つの声があり、好きなものを選ぶことができます。インストールして次に進みます。
音声合成用のpyttsx3ライブラリを提供する必要があります。
pip install pyttsx3
次に、テストプログラムを実行して、正しく実行されているかどうかを確認できます。
import pyttsx3
text = '- '
tts = pyttsx3.init()
rate = tts.getProperty('rate') #
tts.setProperty('rate', rate-40)
volume = tts.getProperty('volume') #
tts.setProperty('volume', volume+0.9)
voices = tts.getProperty('voices')
#
tts.setProperty('voice', 'ru')
#
for voice in voices:
if voice.name == 'Anna':
tts.setProperty('voice', voice.id)
tts.say(text)
tts.runAndWait()
2)音声認識
音声認識ツールはたくさんありますが、すべて有料です。だから私は自分のプロジェクトのための無料の解決策を見つけようとしました、そしてそれを見つけました!これはspeech_recognitionライブラリです。
pip install SpeechRecognition
また、マイクを操作するには、PyAudioライブラリが必要です。
pip install PyAudio
PyAudioのインストールに問題がある人もいるので、このリンクをたどって、必要なバージョンのPyAudioをダウンロードする必要があります。次に、コンソールに入ります。
pip instal
次に、テストプログラムを実行します。ただし、その前に、その中のdevice_index = 1をマイクインデックス値に修正する必要があります。このプログラムを使用して、マイクのインデックスを確認できます。
import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
音声認識テスト:
import speech_recognition as sr
def record_volume():
r = sr.Recognizer()
with sr.Microphone(device_index = 1) as source:
print('.')
r.adjust_for_ambient_noise(source, duration=0.5) #
print('...')
audio = r.listen(source)
print('.')
try:
query = r.recognize_google(audio, language = 'ru-RU')
text = query.lower()
print(f' : {query.lower()}')
except:
print('Error')
while True:
record_volume()
すべて問題がなければ、次に進みます。
アシスタントに話しかけるだけ(AIなし)にする場合は、Googleの無料のDialogFlowツールを使用してこれを行うことができます。ログインすると、最初のボットを作成できる画面が表示されます。 [エージェントの作成]をクリックします。ボットの名前(エージェント名)を考え出し、言語(デフォルト言語)を選択して、[作成]をクリックします。ボットが作成されました!
さまざまな質問に新しい回答を追加するには、新しいインテントを作成する必要があります。これを行うには、[インテント]セクションで[インテントの作成]をクリックします。 「タイトル」と「トレーニングフレーズ」のフィールドに入力してから、回答を入力します。 [保存]をクリックします。それで全部です。
pythonでボットを制御するには、次のコードを記述する必要があります。私のプログラムでは、ボットがすべての答えを表明します。
import apiai, json, re
import pyttsx3
import speech_recognition as sr
tts = pyttsx3.init()
rate = tts.getProperty('rate')
tts.setProperty('rate', rate-40)
volume = tts.getProperty('volume')
tts.setProperty('volume', volume+0.9)
voices = tts.getProperty('voices')
tts.setProperty('voice', 'ru')
for voice in voices:
if voice.name == 'Anna':
tts.setProperty('voice', voice.id)
def record_volume():
r = sr.Recognizer()
with sr.Microphone(device_index = 1) as source:
print('.')
r.adjust_for_ambient_noise(source, duration=1)
print('...')
audio = r.listen(source)
print('.')
try:
query = r.recognize_google(audio, language = 'ru-RU')
text = query.lower()
print(f' : {query.lower()}')
textMessage( text )
except:
print(' .')
def talk( text ):
tts.say( text )
tts.runAndWait()
def textMessage( text ):
request = apiai.ApiAI(' ').text_request() # API Dialogflow
request.lang = 'ru' #
request.session_id = ' id' # ID (, )
request.query = text #
responseJson = json.loads(request.getresponse().read().decode('utf-8'))
response = responseJson['result']['fulfillment']['speech'] # JSON
# - , -
if response:
request.audio_output = response
talk(response)
else:
talk('. .')
while True:
record_volume()
それが今日のすべてです。次のパートでは、スマートボットの作成方法を説明します。彼が答えるだけでなく何かをすることができるように。