我在面对语音识别中的错误

2024-09-30 00:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

import speech_recognition as sr
import pyaudio
r=sr.Recognizer()

with sr.Microphone()as source:
    print(" speak :")
    audio=r.listen(source)
    try:
        output=r.recognize_google(audio)
        print(" you said :".format(output))
    except:
       print(" i cont recognize what u said place spaek clear")

错误:

Traceback (most recent call last):
  File "d:/codes/speechreg.py", line 5, in <module>
    with sr.Microphone()as source:
  File "C:\Users\bjman\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 141, in __enter__
    input=True,  # stream is an input stream
  File "C:\Users\bjman\AppData\Local\Programs\Python\Python37\lib\site-packages\pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "C:\Users\bjman\AppData\Local\Programs\Python\Python37\lib\site-packages\pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9999] Unanticipated host error

Tags: inpysourcestreamlocalaslineusers
1条回答
网友
1楼 · 发布于 2024-09-30 00:33:41

您是否尝试启用对麦克风的访问?尝试使用此guid启用麦克风。 我想你的代码没有打印输出。尝试更改以下行

print(" you said :{}".format(output))

这段代码将按照您的意愿执行。您也可以尝试

import speech_recognition as sr
import pyaudio
r=sr.Recognizer()

with sr.Microphone()as source:
    print(" speak :")
    audio=r.listen(source)
    try:
        value = r.recognize_google(audio)

        if str is bytes: 
            result = u"{}".format(value).encode("utf-8")

        else: 
            result = "{}".format(value)

        with open("outputs.txt","a") as f:
            f.write(result)
        print(result)
    except:
       print(" i cont recognize what u said place spaek clear")

相关问题 更多 >

    热门问题