4

My code:

with sr.Microphone() as source:
    audio = r.listen(source)
    try:
        print("You said: " + r.recognize_google(audio) + "in french")
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service")

I would like to change the listening language to french. How should i do that?

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Sudar
  • 59
  • 1
  • 2
  • 7

3 Answers3

13

here's the relevant line of documentataion.

try r.recognize_google(audio, language="fr-FR")

e.s.
  • 1,351
  • 8
  • 12
0
try:
    voise = r.recognize_google(audio, language="fr-FR")
except sr.UnknownValueError:
    speak('understand')
return voise
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Maksat
  • 1
0

I was able to change the language of google speech recognition with this code:

import speech_recognition as sr
from googletrans import Translator

def Listen():
    r = sr.Recognizer()

    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        # listening duration 8 seconds
        audio = r.listen(source,0,8)

    try:
        print("Recognizing...")
        # hindi language
        query = r.recognize_google(audio,language="hi")
        print(query)
    
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print(f"Could not request results from Google Speech Recognition service: {e}")

    query = str(query).lower()
    return query

print(Listen())

Hope this helps!