运行语音识别模块时出错

时间:2021-01-20 05:37:09

标签: python-3.x speech-recognition pyaudio pyttsx3

我正在开发一个使用语音识别模块的项目。

    # Importing all the installed packages
    import speech_recognition as sr
    audio = sr.Recognizer
    try:
     
     with sr.Microphone() as source:
    voice = audio.listen(source)
    command = audio.recognize_google(voice)
    print(command)
  finally:
pass

我收到此错误

我尝试了所有可能的解决方案,但我一次又一次地遇到相同的错误 我正在使用 PyCharm 编辑器。

ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWitePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
 Traceback (most recent call last):
 File "/home/rohan/Rocland_Python/Rocland/Rocland2.py", line 9, in <module>
command = audio.recognize_google(sound)
File "/home/rohan/Rocland_Python/Rocland/venv/lib/python3.9/site- 
 packages/speech_recognition/__init__.py", line 858, in recognize_google
 if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: 
raise UnknownValueError()
speech_recognition.UnknownValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/rohan/Rocland_Python/Rocland/Rocland2.py", line 10, in <module>
except:   command = command.lower()
NameError: name 'command' is not defined

请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:-1)

这里有我在其中一个代码中使用的语音识别代码片段,我希望它也适用于您:

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 !") 

尝试并结帐!

有socket还有command = command.lower()