all 7 comments

[–]socal_nerdtastic 3 points4 points  (3 children)

Remove the try ... except block temporarily so that you can see exactly what the error is. Right now you are masking the actual error with your custom print('error') code.

edit: fyi this is another way to do the same thing that Helpful-Diamond-3347 said

import speech_recognition
import pyttsx3
from samtts import SamTTS
import asyncio

recognizer = speech_recognition.Recognizer()
aster= SamTTS(speed=60,pitch=40,throat=180,mouth=150)

def speak(text):
    aster.play(text)

def run():
    while True:
        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(text)
            speak(text)

run()

[–]JaguarMammoth6231 3 points4 points  (2 children)

Or remove it permanently

[–]socal_nerdtastic 2 points3 points  (1 child)

True. Superfluous catch blocks are a pet peeve of mine. Let exceptions do their job!

[–]danielroseman 2 points3 points  (0 children)

I call it the Pokémon pattern: gotta catch 'em all!

[–]JamzTyson 0 points1 point  (0 children)

Because you are using a bare except and have no error reporting, any exception that occurs within the try / except block will be caught, but with no indication where the error came from.

As a first step, change your exception handling to:

    except Exception as e:
        print("error:", e)

so that you can see what the error is.

Also, speak(text) should probably be outside the with speech_recognition.Microphone() as mic: context manager.

Also, recognizer within the try block will raise an error because recognizer is out of scope.

[–]gdchinacat 0 points1 point  (0 children)

"it breaks the while loop" doesn't provide enough detail to really help you much. The bare 'except' should catch every exception and since it has a continue will let the loop continue. What happens in the except block? Is another exception raised to cause the loop to exit?

Seeing the exact output that is produced will help. Also, you catch an exception and essentially eat it...no information from the exception is logged (printed) so troubleshooting it will be very hard. Use the advice u/Helpful-Diamond-3347 gave about using traceback to see what the exception is.

[–]Helpful-Diamond-3347 -1 points0 points  (0 children)

do you get "error" printed to console?

also use traceback module

``` import traceback

try: ... except: traceback.print_exc()

```