pythonpyttsx3

Run loop already started (pyttsx3)


from RealtimeSTT import AudioToTextRecorder

from colorama import Fore, Back, Style
import colorama
import os
from googletrans import Translator
import re
import pyttsx3




if __name__ == '__main__':

    print("Initializing RealtimeSTT test...")

    colorama.init()

    full_sentences = []
    displayed_text = ""

    def clear_console():
        os.system('clear' if os.name == 'posix' else 'cls')

    def text_detected(text):
        global displayed_text
        sentences_with_style = [
            f"{Fore.YELLOW + sentence + Style.RESET_ALL if i % 2 == 0 else Fore.CYAN + sentence + Style.RESET_ALL} "
            for i, sentence in enumerate(full_sentences)
        ]
        new_text = "".join(sentences_with_style).strip() + " " + text if len(sentences_with_style) > 0 else text

        if new_text != displayed_text:
            displayed_text = new_text
            clear_console()
            print(displayed_text, end="", flush=True)
            
            # Vertaal de tekst naar het Engels zonder opmaak
            translator = Translator()
            plain_text = strip_ansi(displayed_text)  # Verwijder opmaakcodes
            result = translator.translate(plain_text, dest='en')
            clear_console()
            print(result.text)
            speak(result.text)

    def strip_ansi(text):
        # Verwijder ANSI-kleurencodes uit de tekst
        ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
        return ansi_escape.sub('', text)

    def process_text(text):
        text = full_sentences.append(text)
        text_detected("")  # Update de getoonde tekst

    def speak(text):
        engine = pyttsx3.init()
        engine.say(text)
        engine.runAndWait()
        text = ""
        engine.endLoop
        engine.stop


        
    recorder_config = {
        'spinner': False,
        'model': 'large-v2',
        'language': 'en',
        'silero_sensitivity': 0.4,
        'webrtc_sensitivity': 2,
        'post_speech_silence_duration': 0.4,
        'min_length_of_recording': 0,
        'min_gap_between_recordings': 0,
        'enable_realtime_transcription': True,
        'realtime_processing_pause': 0.2,
        'realtime_model_type': 'large-v3',
        'on_realtime_transcription_update': text_detected, 
    }

    recorder = AudioToTextRecorder(**recorder_config)

    clear_console()
    print("Say something...", end="", flush=True)

    while True:
        recorder.text(process_text)

When I first speak , it translates and speaks fine the translation but when I speak again without restarting it says the error.

I tried a lot but I think I'm just missing something easy.


Solution

  • You are doing a lot of stuff in text_detected. Like initializing translator = Translator() and calling speak. text_detected method will gets called very often because you also referenced it at 'on_realtime_transcription_update': text_detected. You want to speak the detected sentence only on process_text method, when the final transcription is delivered. Also Tim is right, engine.endLoop and engine.stop are wrong here.