更改语言在语音转文本中

huangapple go评论72阅读模式
英文:

Change language in speech-to-text

问题

我想要将我的语音转文本引擎中的书面文本语言更改为另一种,但我无法做到
我正在使用speechrecognition库

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


def recsec():
    global seconds
    seconds = int(input('你想录制多少秒?'))
    if seconds >= 20:
        print('无效数字,禁止大于20秒')
        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 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

答案1

得分: 1

以下是已翻译的内容:

有一个名为 language 的参数,它存在于以下所有方法中:

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

以下代码将语言更改为法语。

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')

默认语言是 en-US。BCP-47 语言标签被使用,但每个方法都有特定的可用标签。有关更多信息,请查看此链接的回答。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月24日 16:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定