0

我正在用 python 3 制作一个聊天机器人。我使用麦克风功能来录制我的声音。如果我插入耳机,那么它可以完美运行。但是如果我说什么,如果没有耳机,它会接受输入(听我说话)但不会停止听,直到我插入耳机并说些什么。为什么它不停止听我没有耳机?这是我的代码片段-

r = sr.Recognizer()
with sr.Microphone() as source:
    print("Listening....")
    r.pause_threshold = 1
    audio = r.listen(source)

我希望聊天机器人在我没有连接耳机时停止收听并在 1 秒的间隙内开始执行。

4

2 回答 2

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
于 2019-05-30T10:43:12.397 回答
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)

我研究了几个小时,找到了我的问题的解决方案。

于 2019-05-30T10:49:10.050 回答