简体   繁体   English

使用 Python 和 PyAudio 的语音到文本无法在操作系统上运行

[英]Speech to Text using Python & PyAudio not working on OS

I am trying to use the Speech Recognition library on Python 3.7 (OS X/internal microphone)我正在尝试在 Python 3.7(OS X/内置麦克风)上使用语音识别库

Here's the code I am using so far:这是我到目前为止使用的代码:

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

print("Google Speech Recognition thinks you said " + r.recognize_google(audio))

However, there is no output and no error message when I run the program.但是,运行程序时没有 output 并且没有错误消息。 But when I hit the "stop" button, this error message turns up但是当我点击“停止”按钮时,会出现此错误消息

Traceback (most recent call last): File "/Users/diandraelmira/PycharmProjects/untitled/venv/APP.py", line 6, in audio = r.listen(source) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/speech_recognition/ init .py", line 620, in listen buffer = source.stream.read(source.CHUNK) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/speech_recognition/ init .py", line 161, in read return self.pyaudio_stream.read(size, exception_on_overflow=False) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/pyaudio.py", line 608, in read return pa.read_stream(self._stream, num_frames, exception_on_overflow) KeyboardInterrupt回溯(最近一次通话最后):文件“/Users/diandraelmira/PycharmProjects/untitled/venv/APP.py”,第 6 行,音频 = r.listen(源)文件“/Users/diandraelmira/PycharmProjects/untitled/venv /lib/python3.7/site-packages/speech_recognition/init .py”,第 620 行,在监听缓冲区 = source.stream.read(source.CHUNK) 文件“/Users/diandraelmira/PycharmProjects/untitled/venv/lib/ python3.7/site-packages/speech_recognition/ init .py”,第 161 行,在读取返回 self.pyaudio_stream.read(size, exception_on_overflow=False) 文件“/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3. 7/site-packages/pyaudio.py”,第 608 行,在读取中返回 pa.read_stream(self._stream, num_frames, exception_on_overflow) KeyboardInterrupt

How can I fix this?我怎样才能解决这个问题?

Hi could you please try this and will look for the errors嗨,你可以试试这个,并会寻找错误

try:
    print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
    print("Sphinx could not understand audio")
except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))

i think there is need to little bit changes in your code and it is:我认为您的代码需要稍作改动,它是:

import speech_recognition as sr

rObject = sr.Recognizer() 
audio = '' 
with sr.Microphone() as source: 
    print("Speak...")   
    audio = rObject.listen(source, phrase_time_limit = 0) 
    print("Stop.")
    try: 
        text = rObject.recognize_google(audio, language ='en-US') 
        print("You : "+ text)  
    except: 
        speak("Could not understand your audio...PLease try again !") 

Try this.尝试这个。 Hope error will be solved .希望错误会得到解决。

Use the following code:使用以下代码:

#import library

import speech_recognition as sr

# Initialize recognizer class (for recognizing the speech)

r = sr.Recognizer()

# Reading Microphone as source
# listening the speech and store in audio_text variable

with sr.Microphone() as source:
    print("Talk")
    audio_text = r.listen(source)
    print("Time over, thanks")
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
    
    try:
        # using google speech recognition
        print("Text: "+r.recognize_google(audio_text))
    except:
         print("Sorry, I did not get that")

Hopefully it works.希望它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM