2

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
Nir Malka
  • 41
  • 4
  • what are you trying to avoid, the text "listening.." or the text "result2: { 'alternative': [{'confidence': 0.97219545, 'transcript': 'hello there'}], 'final': True} hello there " – Jacob Glik Jan 11 '23 at 19:18
  • 1
    just fixed the qustion, it is clearer to understand – Nir Malka Jan 11 '23 at 19:28

1 Answers1

2

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)
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48