我对Python比较陌生。我正在尝试创建一个程序来打开YouTube。在这里,用户应该说“ open youtube”,而python应该在浏览器中将其打开。但是,我在以下代码中不断收到操作系统错误。当我在朋友的PC上尝试相同的程序时,错误出乎意料地消失了!但是由于某种原因它仍然存在于我的系统中。经过深入研究,我发现问题出在“ audio = r.listen(source)”行,其中代码返回错误。请帮助。 (我已经检查了麦克风,似乎没有问题。)
import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install speechRecognition
import webbrowser
import os
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)
def takeCommand():
#It takes microphone input from the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 0.5
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except Exception as e:
# print(e)
print("Say that again please...")
return "None"
return query
while True:
query = takeCommand().lower()
if 'open youtube' in query:
webbrowser.open("youtube.com") #end of code
I am getting the following error message:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-15-a53e26f9d26c> in <module>
32
33 while True:
---> 34 query = takeCommand().lower()
35
36 if 'open youtube' in query:
<ipython-input-15-a53e26f9d26c> in takeCommand()
14
15 r = sr.Recognizer()
---> 16 with sr.Microphone() as source:
17 print("Listening...")
18 r.pause_threshold = 0.5
c:\users\it\appdata\local\programs\python\python37\lib\site-packages\speech_recognition\__init__.py in __enter__(self)
139 input_device_index=self.device_index, channels=1,
140 format=self.format, rate=self.SAMPLE_RATE, frames_per_buffer=self.CHUNK,
--> 141 input=True, # stream is an input stream
142 )
143 )
c:\users\it\appdata\local\programs\python\python37\lib\site-packages\pyaudio.py in open(self, *args, **kwargs)
748 """
749
--> 750 stream = Stream(self, *args, **kwargs)
751 self._streams.add(stream)
752 return stream
c:\users\it\appdata\local\programs\python\python37\lib\site-packages\pyaudio.py in __init__(self, PA_manager, rate, channels, format, input, output, input_device_index, output_device_index, frames_per_buffer, start, input_host_api_specific_stream_info, output_host_api_specific_stream_info, stream_callback)
439
440 # calling pa.open returns a stream object
--> 441 self._stream = pa.open(**arguments)
442
443 self._input_latency = self._stream.inputLatency
OSError: [Errno -9999] Unanticipated host error