简体   繁体   中英

How to make voice detection in python faster?

I have some voice detection code and it works, but. it runs really slowly? Can I do anything to make it faster?

import speech_recognition
import pyttsx3

recognizer = speech_recognition.Recognizer()

while True:
    try:
        with speech_recognition.Microphone() as mic:
            recognizer.adjust_for_ambient_noise(mic, duration=0.2)
            audio = recognizer.listen(mic)

            text = recognizer.recognize_google(audio)
            text = text.lower()
            print(f" {text}")
    except speech_recognition.UnknownValueError():
    
        recognizer = speech_recognition.Recognizer()
        continue

Try creating mic once instead of each iteration:

import speech_recognition
import pyttsx3

recognizer = speech_recognition.Recognizer()


with speech_recognition.Microphone() as mic:
    while True:
        try:
            recognizer.adjust_for_ambient_noise(mic, duration=0.2)
            audio = recognizer.listen(mic)

            text = recognizer.recognize_google(audio)
            text = text.lower()
            print(f" {text}")
        except speech_recognition.UnknownValueError():
            pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM