为什么在这种情况下使用OOP使用语音识别?
我正在尝试使用OOP使用语音识别。我尝试从此github 转换过程代码。
import speech_recognition as sr #recognise speech
import time
class SpeechRecognition():
def __init__(self):
self.r = sr.Recognizer() #initialize recognizer that is responsible for recognizing the speech
# listen for audio and convert it to text:
def record_audio(self, ask=False):
with sr.Microphone() as source: # microphone as source
if ask:
print(ask)
audio = self.r.listen(source) # listen for the audio via source
voice_data = ''
try:
voice_data = self.r.recognize_google(audio, language='sv-SE') # convert audio to text
except sr.UnknownValueError: # error: recognizer does not understand
print('Hörde inte det')
except sr.RequestError: # error: recognizer is not connected
print('Ledsen, tjänsten är nere')
print(f">> {voice_data.lower()}") # print what user said
return voice_data.lower()
def init_speech_recognizer(self):
time.sleep(1)
print('Hur kan jag hjälpa dig?')
while 1: #this will let it keep listen
voice_data = self.record_audio("Recording") # get the voice input
print("Done")
speechObj = SpeechRecognition()
speechObj.init_speech_recognizer()
输出是:
Hur kan jag hjälpa dig?
Recording
但是我说什么都没打印。为什么这是?
I am trying to use speech recognition with OOP. I try to convert the procedural code from this github.
import speech_recognition as sr #recognise speech
import time
class SpeechRecognition():
def __init__(self):
self.r = sr.Recognizer() #initialize recognizer that is responsible for recognizing the speech
# listen for audio and convert it to text:
def record_audio(self, ask=False):
with sr.Microphone() as source: # microphone as source
if ask:
print(ask)
audio = self.r.listen(source) # listen for the audio via source
voice_data = ''
try:
voice_data = self.r.recognize_google(audio, language='sv-SE') # convert audio to text
except sr.UnknownValueError: # error: recognizer does not understand
print('Hörde inte det')
except sr.RequestError: # error: recognizer is not connected
print('Ledsen, tjänsten är nere')
print(f">> {voice_data.lower()}") # print what user said
return voice_data.lower()
def init_speech_recognizer(self):
time.sleep(1)
print('Hur kan jag hjälpa dig?')
while 1: #this will let it keep listen
voice_data = self.record_audio("Recording") # get the voice input
print("Done")
speechObj = SpeechRecognition()
speechObj.init_speech_recognizer()
The output is:
Hur kan jag hjälpa dig?
Recording
But whatever I say nothing gets printed. Why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论