Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

Microphone detection problems with speech_recognition Centos/python

I'm using python with speech_recognition to listen from microphone.

Here is the python code corresponding to the listening :

#!/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))

The listen function is part of voiceThread class. The indentation problem comes from the copy/past, but it's correct on the code.

When I start the code, the listen stays "stuck" on "Dites quelque chose" without executing the loop (there is a delay to avoid staying stuck here if no one is speaking but it looks like it's ignored. I had the exact same behavior when I started this project before I added the delay because of the ambiant noise).

While the code is "stuck" here, if I go in Centos parameters and disable, then enable the microphone, everything works perfectly.

This problem started 2 or 3 days ago, before that, the speech_recognition was working fine without having to disable/enable the microphone. I didn't change any configuration on my computer, webcam (I'm using a webcam for the microphone), or the code. To be sure it's not due to the code, I checked out an older commit (the first commit I have with speech_recognition working) and have the same problem.

It started on a Virtual box VM. I installed a dual boot to be sure it wasn't just a virtual box problem, but I'm having the same problem.

Has anyone seen the behavior already?

question from:https://stackoverflow.com/questions/65906050/microphone-detection-problems-with-speech-recognition-centos-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...