pythonpython-3.xvoice-recognitionpyttsx3

Why the command listener.listen from lib pyttsx3 printing this extra text?


basicly trying to follow the steps of a vid, everything is working great exapt that those lines of code:

with sr.Microphone() as source:
print('listening..')
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)

it is giving me the output:

listening..
result2:
{   'alternative': [{'confidence': 0.97219545, 'transcript': 'hello there'}],
    'final': True}
hello there

Process finished with exit code 0

Im trying just to avoid this line:

  result2:
    {   'alternative': [{'confidence': 0.97219545, 'transcript': 'hello there'}],
        'final': True}

to have the result like this:

listening..
hello there

Process finished with exit code 0

Solution

  • For some reason (looks like a debugging leftover) speach_recognition has this print here. Here's the related issue.

    You can avoid it by passing show_all=True and then do what the library does manually:

    with sr.Microphone() as source:
        print('listening..')
        voice = listener.listen(source)
        actual_result = listener.recognize_google(voice, show_all=True)
    
        if "confidence" in actual_result["alternative"]:
            best_hypothesis = max(actual_result["alternative"], key=lambda alternative: alternative["confidence"])
        else:
            best_hypothesis = actual_result["alternative"][0]
        confidence = best_hypothesis.get("confidence", 0.5)
        
        command = best_hypothesis["transcript"], confidence
        print(command)
    

    without confidence:

    with sr.Microphone() as source:
        print('listening..')
        voice = listener.listen(source)
        actual_result = listener.recognize_google(voice, show_all=True)
    
        if "confidence" in actual_result["alternative"]:
            best_hypothesis = max(actual_result["alternative"], key=lambda alternative: alternative["confidence"])
        else:
            best_hypothesis = actual_result["alternative"][0]
        
        command = best_hypothesis["transcript"]
        print(command)