SpeechRecognition 不工作(未检测到麦克风)
SpeechRecognition not working (microphone not detected)
我想用语音识别做一个小程序。
这是我的代码(经典代码):
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("SAY SOMETHING")
audio = r.listen(source,timeout=3, phrase_time_limit=3)
print("TIME OVER")
try:
print("TEXTE : "+r.recognize_google(audio, language="fr-FR"))
except Exception:
print("ERROR")
但是当我尝试启动程序时出现此错误:
ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channeljack 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 "record.py", line 6, in <module>
with sr.Microphone() as source:
File "/usr/lib/python2.7/site-
packages/speech_recognition/__init__.py", line 86, in __init__
device_info = audio.get_device_info_by_index(device_index) if
device_index is not None else audio.get_default_input_device_info()
File "/usr/lib64/python2.7/site-packages/pyaudio.py", line 949, in
get_default_input_device_info
device_index = pa.get_default_input_device()
IOError: No Default Input Device Available
当我这样做时 arecord -l
我有这个 :
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC298 Analog [ALC298 Analog]
Subdevices: 0/1
Subdevice #0: subdevice #0
Ps : 麦克风适用于任何软件,如 Skype 或 Google
尝试:更改您的默认麦克风设备。
我的一个项目中有一个有效的代码片段试试这个它可能会起作用:
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 ='fr-FR')
print("You : "+ text)
except:
speak("Could not understand your audio...PLease try again !")
运行 这个片段,我认为有一些名为 PyAudio 的包的问题...没有正确安装,如果没有 pyaudio 它将无法工作(特别是在 python 2.7 中没有 pyaudio 将无法工作。
尝试将 PyAudio 安装到您的代码中以消除错误。甚至尝试连接到网络或让您的麦克风正常工作。
如何在异常块中添加一个Speech_regognition超时错误
with sr.Microphone() as source:
print("SAY SOMETHING")
audio = r.listen(source,timeout=3, phrase_time_limit=3)
print("TIME OVER")
try:
print("TEXTE : "+r.recognize_google(audio, language="fr-FR"))
except sr.WaitTimeoutError:
print("ERROR")
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, 1)
print("Listening....")
speak("listening")
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"You said: {query}\n ")
except sr.UnknownValueError:
speak("Could not hear that, Try saying again")
except sr.RequestError:
speak("Make Sure that you have a good Internet connection")
return query
您必须通过 pip install pyttsx3
import pyttsx3
并且通过 pip install SpeechRecognition
import speech_recognition as sr
然后您的代码才能工作。
我想用语音识别做一个小程序。
这是我的代码(经典代码):
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("SAY SOMETHING")
audio = r.listen(source,timeout=3, phrase_time_limit=3)
print("TIME OVER")
try:
print("TEXTE : "+r.recognize_google(audio, language="fr-FR"))
except Exception:
print("ERROR")
但是当我尝试启动程序时出现此错误:
ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channeljack 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 "record.py", line 6, in <module>
with sr.Microphone() as source:
File "/usr/lib/python2.7/site- packages/speech_recognition/__init__.py", line 86, in __init__
device_info = audio.get_device_info_by_index(device_index) if
device_index is not None else audio.get_default_input_device_info()
File "/usr/lib64/python2.7/site-packages/pyaudio.py", line 949, in
get_default_input_device_info
device_index = pa.get_default_input_device()
IOError: No Default Input Device Available
当我这样做时 arecord -l
我有这个 :
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC298 Analog [ALC298 Analog]
Subdevices: 0/1
Subdevice #0: subdevice #0
Ps : 麦克风适用于任何软件,如 Skype 或 Google
尝试:更改您的默认麦克风设备。
我的一个项目中有一个有效的代码片段试试这个它可能会起作用:
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 ='fr-FR')
print("You : "+ text)
except:
speak("Could not understand your audio...PLease try again !")
运行 这个片段,我认为有一些名为 PyAudio 的包的问题...没有正确安装,如果没有 pyaudio 它将无法工作(特别是在 python 2.7 中没有 pyaudio 将无法工作。
尝试将 PyAudio 安装到您的代码中以消除错误。甚至尝试连接到网络或让您的麦克风正常工作。
如何在异常块中添加一个Speech_regognition超时错误
with sr.Microphone() as source:
print("SAY SOMETHING")
audio = r.listen(source,timeout=3, phrase_time_limit=3)
print("TIME OVER")
try:
print("TEXTE : "+r.recognize_google(audio, language="fr-FR"))
except sr.WaitTimeoutError:
print("ERROR")
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, 1)
print("Listening....")
speak("listening")
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"You said: {query}\n ")
except sr.UnknownValueError:
speak("Could not hear that, Try saying again")
except sr.RequestError:
speak("Make Sure that you have a good Internet connection")
return query
您必须通过 pip install pyttsx3
import pyttsx3
并且通过 pip install SpeechRecognition
import speech_recognition as sr
然后您的代码才能工作。