Search code examples
pythonspeech-to-text

Change language in speech-to-text


I want to change the lagnuage of the written text in my speech to text engine to another one and I can't do it I am using the speechrecognition library

import speech_recognition as sr
import os
import winsound
import pyaudio
import wave
seconds = 11
start = True


def recsec():
    global seconds
    seconds = int(input('How many seconds do you want to record?'))
    if seconds >= 20:
        print('Invalid number, Higher than 20 seconds is forbidden')
        recsec()


recsec()
p = pyaudio.PyAudio()
winsound.Beep(2000, 175)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
frames = []
try:
    while True:
        for i in range(seconds * 45):
            data = stream.read(1024)
            frames.append(data)
        break
except KeyboardInterrupt:
    pass
stream.stop_stream()
stream.close()
p.terminate()
sound_file = wave.open("output.wav", "wb")
sound_file.setnchannels(1)
sound_file.setsampwidth(p.get_sample_size(pyaudio.paInt16))
sound_file.setframerate(44100)
sound_file.writeframes(b''.join(frames))
sound_file.close()
filename = "output.wav"
r = sr.Recognizer()
with sr.AudioFile(filename) as source:
    # listen for the data (load audio to memory)
    audio_data = r.record(source)
    # recognize (convert from speech to text)
    text = r.recognize_google(audio_data)
    print(text)
os.remove("output.wav")

I tried to look for a parameter that indicates the language but didn't find


Solution

  • There's a parameter named language which is present in all of the following methods;

    • recognize_bing()
    • recognize_google()
    • recognize_google_cloud()
    • recognize_ibm()
    • recognize_sphinx()

    Below code changes language to French.

    import speech_recognition as sr
    
    r = sr.Recognizer()
    with sr.AudioFile('"output.wav"') as source:
        audio = r.record(source)
    
    r.recognize_google(audio, language='fr-FR')
    

    The default is en-US. BCP-47 language tags are used but avaliable tags are specific per method. For more info check this answer.