我正在使用 python 和语音识别从麦克风收听。
这里是监听对应的python代码:
#!/usr/bin/python3
import speech_recognition as sr
import espeakng
import subprocess
import threading
from commands import Commands
speaker = espeakng.Speaker(voice='fr')
listener = sr.Recognizer()
class voiceThread(threading.Thread):
running = True
converseInProgress = False
def __init__(self, threadID, name, commands, managerThread):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.managerThread = managerThread
self.commands = commands
self.insertMode = False
def run(self):
print("Starting {}".format(self.name))
self.continuousListen(5)
def configureESpeakNG(self):
speaker.pitch = 52
speaker.speed = 150
def talk(self, sentence):
speaker.say(sentence,sync=True)
def continuousListen(self, delay):
self.configureESpeakNG()
converseStarted = False
with sr.Microphone() as source:
listener.adjust_for_ambient_noise(source, duration=10)
while not converseStarted :
text = self.listen(5, source)
if text is not None:
if "julie" in text.lower():
converseStarted = True
self.processConverse(text)
# This indent error is due to copy/past, it's correct on my computer
def listen(self, delay, source):
print("Dites quelque chose")
audio = listener.listen(source, phrase_time_limit=delay)
# audio = r.listen(source)
try:
text = listener.recognize_google(audio, language="fr-FR")
print("Vous avez dit : " + text)
return text
except sr.UnknownValueError:
print("L'audio n'as pas été compris")
except sr.RequestError as e:
print("Le service Google Speech API ne fonctionne plus" + format(e))
listen 函数是 voiceThread 类的一部分。缩进问题来自复制/过去,但在代码上是正确的。
当我启动代码时,侦听保持“卡住”在“Dites quelque selected”上而不执行循环(如果没有人说话,有一个延迟以避免卡在这里,但看起来它被忽略了。我有在我添加延迟之前开始这个项目时完全相同的行为,因为环境噪音)。
虽然代码“卡在这里”,但如果我进入 Centos 参数并禁用,然后启用麦克风,一切正常。
这个问题是在 2 或 3 天前开始的,在此之前,speech_recognition 工作正常,无需禁用/启用麦克风。 我没有更改计算机、网络摄像头(我使用网络摄像头作为麦克风)或代码上的任何配置。为确保这不是由代码引起的,我检查了一个较旧的提交(我在语音识别工作时的第一次提交)并且遇到了同样的问题。
它在一个 Virtual box VM 上启动。我安装了双启动以确保这不仅仅是虚拟机问题,但我遇到了同样的问题。
有人见过这种行为吗?