我试图在脚本运行时在后台收听音频我有这部分代码用于收听
import cv2
import mediapipe as mp
import pyautogui
import speech_recognition as sr
import time
r = sr.Recognizer()
m = sr.Microphone()
def callback(recognizer, audio):
print('go')
# received audio data, now we'll recognize it using Google Speech Recognition
try:
print("fun trig")
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print("Google Speech Recognition thinks you said " + recognizer.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
r = sr.Recognizer()
m = sr.Microphone()
# print(sr.Microphone.list_microphone_names())
with m as source:
r.adjust_for_ambient_noise(source) # we only need to calibrate once, before we start listening
# start listening in the background (note that we don't have to do this inside a `with` statement)
stop_listening = r.listen_in_background(m, callback)
# `stop_listening` is now a function that, when called, stops background listening
# do some unrelated computations for 5 seconds
for _ in range(50): time.sleep(0.1) # we're still listening even though the main thread is doing other things
# calling this function requests that the background listener stop listening
stop_listening(wait_for_stop=False)
# do some more unrelated things
while True:
print('gone')
time.sleep(0.1)
过了一会儿,我得到“gone”打印,既没有打印 go,也没有打印“fun trig”,这意味着回调函数没有被调用
我运行了脚本并返回 python runonce.py 不相关的输入...Google语音识别认为您打招呼 Google 语音识别无法理解音频 Google 语音识别无法理解音频 Google 语音识别认为您打过招呼 Google 语音识别无法理解音频 谷歌语音识别认为你说你好你好你好你好你好
似乎没有正确调用回调 为什么?请帮忙
试试这个。告诉我是否有效?
import speech_recognition as sr
def callback(recognizer, audio):
try:
print("Google Speech Recognition thinks you said " + recognizer.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source)
stop_listening = r.listen_in_background(m, callback)
while True:
input('unrelated input ...')