#speech-recognition #raspberry-pi4 #listen
Вопрос:
Я использую gtts==2.2.3 для говорения и распознавания речи==3.8.1 для прослушивания на Raspberry pi и Windows 10. Это разговорное приложение для ответов на вопросы. Причина, по которой я делаю и то, и другое, заключается в том, что качество речи при использовании gtts на Raspberry Pi было намного лучше.
Приложение предлагает пользователю, использующему его, задать вопрос. Пользователь отвечает, произнося вопрос. Приложение прослушивает вопрос и фиксирует его в виде текста для передачи в модель нейронной сети.
Именно здесь происходит сбой приложения. Он не прослушивает ответ пользователя. Это просто продолжается следующим утверждением. Ответ (вопрос) пользователя пуст. В приложении нет никаких явных ошибок. Этот процесс безупречно работает в Windows 10 — тот же код.
Вот код:
import os
import time
import speech_recognition as sr
import pyttsx3
import mute_alsa
from gtts import gTTS
question = ""
myText = ""
r = sr.Recognizer()
# calibrate the speech
def calibrate():
engine = pyttsx3.init()
""" RATE"""
rate = engine.getProperty('rate') # getting details of current speaking rate
# print(rate) # printing current voice rate
engine.setProperty('rate', 112) # setting up new voice rate
"""VOLUME"""
volume = engine.getProperty('volume') # getting to know current volume level (min=0 and max=1)
# print(volume) # printing current volume level
engine.setProperty('volume', 1.0) # setting up volume level between 0 and 1
"""VOICE"""
voices = engine.getProperty('voices') # getting details of current voice
# engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male
engine.setProperty('voice', voices[1].id)
return engine
def speak(speech):
# Language in which you want to convert
language = 'en'
# Passing the text and language to the engine,
# here we have marked slow=False. Which tells
# the module that the converted audio should
# have a high speed
myObj = gTTS(text=speech, lang=language, slow=False)
# Saving the converted audio in a mp3 file named
# welcome
myObj.save("speech.mp3")
# Playing the converted file
os.system("mpg321 speech.mp3")
# Give the Greeting
def askQuestion():
global myText, question
# Initialize recognizer class (for recognizing the speech)
# r = sr.Recognizer()
# The text that you want to convert to audio
myText = 'Ask me a question about New Mexico volcanoes'
speak(myText)
print("Ask A Question")
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening.....')
r.pause_threshold = 1
r.energy_threshold = 4000
audio = r.listen(source)
try:
print('Recognising...')
question = r.recognize_google(audio, language='en-us')
print("Question: " question)
myText = "You asked:" question
speak(myText)
time.sleep(1)
myText = "Give me a couple of minutes and I will have your answer.' question"
speak(myText)
except Exception as e:
print('exception : ', e)
myText = "Sorry, I didn't hear that, Say that again Please"
speak(myText)
return ""
return question
# Give the answer
def getAnswer(answer):
print("The answer is... ", answer)
"""SPEAK THE ANSWER"""
myText = "The answer is: " answer['answer']
speak(myText)