gpt4 book ai didi

python - 转到另一个功能后,我的麦克风会恢复收听

转载 作者:太空宇宙 更新时间:2023-11-04 00:17:20 25 4
gpt4 key购买 nike

每当我运行此代码并告诉启动函数“google”时,它都会返回到另一个函数。我已经尝试这样做几天了,但仍然没有运气。任何帮助将不胜感激:)

import webbrowser
import string
import time
import pyttsx3
import speech_recognition as sr
engine = pyttsx3.init()
r = sr.Recognizer()

def Listen():
with sr.Microphone() as sourceL:
print("Listening...")
Open = r.listen(sourceL, phrase_time_limit=2)
try:
if "Nova" in r.recognize_google(Open):
print("Nova Recieved...")
Command()
else:
Listen()
except:
Listen()

def Google():
print("what would you like me to search for you? ")
engine.say("what would you like me to search for you? ")
engine.runAndWait()
with sr.Microphone as source:
Search = r.listen(source)
Search = r.recognize(Search)

代码将返回到 Listen() 处的,以 sr.Mirophone 作为源这就是我调用 google() 的方式...

def Command():
print("You called me?")
engine.say("you called me? ")
engine.runAndWait()
Cr = sr.Recognizer()
with sr.Microphone() as source:
print("Listening For Command...")
CommandToDo = Cr.listen(source, phrase_time_limit=2)
print("...")
if "YouTube" in Cr.recognize_google(CommandToDo):
YouTube()

elif "Google" in Cr.recognize_google(CommandToDo):
Google()

else:
print("Command not recognized>> " + r.recognize_google(CommandToDo))

最佳答案

Google 函数中调用 listen 方法时,您需要指定一个函数参数 phrase_time_limit
phrase_time_limit 表示程序的等待时间,等待用户输入的秒数。在这里它将等待 2 秒。如果您不给出任何时间限制,它将无限期地等待。

来自源代码文档:

The phrase_time_limit parameter is the maximum number of seconds that this will allow a phrase to continue before stopping and returning the part of the phrase processed before the time limit was reached. The resulting audio will be the phrase cut off at the time limit. If phrase_timeout is None, there will be no phrase time limit.

澄清timeout参数

The timeout parameter is the maximum number of seconds that this will wait for a phrase to start before giving up and throwing an speech_recognition.WaitTimeoutError exception. If timeout is None, there will be no wait timeout.

更多详情请查看the source code.

def Google():
print("what would you like me to search for you? ")
engine.say("what would you like me to search for you? ")
engine.runAndWait()
with sr.Microphone() as source:
Search = r.listen(source, phrase_time_limit=2) # <-- Here
Search = r.recognize_google(Search)
print(Search)

此更改后,它对我有用。
检查它是
以 sr.Microphone() 作为来源: 不是
以 sr.Microphone 作为来源:。你没戴牙套。

关于python - 转到另一个功能后,我的麦克风会恢复收听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50428100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com