我正在用python 3制作一个聊天机器人。我使用了麦克风功能来记录我的声音。如果我插入耳机,那么它可以完美工作。但是如果没有耳机,我要说些什么,它就需要输入(听我说),但是直到我插入耳机并说些什么才停止听。为什么它不停止不带耳机就听我说话? 这是我的代码段-
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening....")
r.pause_threshold = 1
audio = r.listen(source)
我希望聊天机器人在没有连接耳机的情况下停止监听并在1秒间隔内开始执行。
答案 0 :(得分:0)
import sys
!pip install -c conda-forge google-api-python-client
import speech_recognition as sr
r = sr.Recognizer()
r.energy_threshold = 2500
#Input your speech by listen(_)
with sr.Microphone() as source:
print('Say Something:!')
audio = r.listen(source)
print('Done!')
try:
print("You said: \n" + r.recognize_google(audio, language = 'hi-IN'))
except Exception as e:
print(e)
以上我曾在本地工作和尝试过。 请进一步说明您的查询。我了解的是,使用麦克风工作正常,当您不使用麦克风讲话时效果不佳。可能是由于sr.Microphone()作为来源。
您可以尝试使用音频文件作为音频源-
from os import path
AUDIO_FILE = "/Path of audio file.....wav"
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file
答案 1 :(得分:0)
如果提示从不消失,则很可能是您的麦克风拾取了太多的环境噪声。要处理环境噪声,您需要使用Recognizer类的Adjust_for_ambient_noise()方法。
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening....")
r.adjust_for_ambient_noise(source) # This filters noise
r.pause_threshold = 1
audio = r.listen(source)
我研究了几个小时,为我的问题找到了解决方案。