[英]Speech_Recognition with python on computer speakers - OSError: [Errno -9998] Invalid number of channels
I'm trying to use the Speech_Recognition module in python (I am using python 3.7.0) to detect the speech coming out from my computer speakers (eg to detect what somebody is saying in a skype call)我正在尝试使用 python 中的Speech_Recognition模块(我正在使用 python 3.7.0)来检测从我的计算机扬声器发出的语音(例如检测某人在 Skype 通话中所说的话)
import speech_recognition as sr
def get_speakers_index(list_microphone_names):
list_index = []
for i in range(len(list_microphone_names)):
if "speakers" in list_microphone_names[i].lower():
list_index.append(i)
return list_index
def main():
r = sr.Recognizer()
list_speakers_index = get_speakers_index(sr.Microphone.list_microphone_names())
for speakers_index in list_speakers_index:
mic = sr.Microphone(device_index=speakers_index)
with mic as source:
print("listening")
audio = r.listen(source)
text = ""
try:
text = r.recognize_google(audio)
except Exception as e:
print("Exception " + str(e))
if __name__ == '__main__':
main()
But on every one of the speakers option I get this error:但是在每个扬声器选项上,我都会收到此错误:
Traceback (most recent call last):
File "C:/Users/User/Desktop/Project/mic.py", line 35, in <module>
main()
File "C:/Users/User/Desktop/Project/mic.py", line 21, in main
with mic as source:
File "C:\Python37\lib\site-packages\speech_recognition\__init__.py", line 141, in __enter__
input=True, # stream is an input stream
File "C:\Python37\lib\site-packages\pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "C:\Python37\lib\site-packages\pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels
I also tried to enter the speaker's indexes one by one to not create multiple Microphone()
instances, but it didn't help.我还尝试一一输入扬声器的索引以不创建多个
Microphone()
实例,但这没有帮助。
Thanks in advance提前致谢
Instead of this, try using just a microphone as the input that the mic takes from the speakers.取而代之的是,尝试仅使用麦克风作为麦克风从扬声器获取的输入。 It simplifies it and is straight forward.
它简化了它并且是直截了当的。 Sort of like this program right here...
有点像这里的这个程序......
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-us')
print("User said: {query}\n")
except Exception as e:
print(e)
print("I can't hear you sir.")
return "None"
return query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.