如何在Google Speech API Python客户端中设置语言

时间:2017-04-04 02:42:04

标签: python speech-recognition

我想了解Google演讲文字 API 。我尝试了由Google云提供的示例代码 API

这是我的代码:

import io
import os

# Imports the Google Cloud client library
from google.cloud import speech

# Instantiates a client
speech_client = speech.Client()

# The name of the audio file to transcribe
file_name = os.path.join(
    os.path.dirname(__file__),
    'resources',
    'audio.raw')

# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
    content = audio_file.read()
    audio_sample = speech_client.sample(
        content,
        source_uri=None,
        encoding='LINEAR16',
        sample_rate=16000)

# Detects speech in the audio file
alternatives = speech_client.speech_api.sync_recognize(audio_sample)

for alternative in alternatives:
    print('Transcript: {}'.format(alternative.transcript))

然后我想知道如何使用其他语言而不是英语。 有一点我想用韩语识别,而不是英语。 那么请问,我应该把这个代码用于韩国认可? ko-kr 是Google提供的语言代码。

1 个答案:

答案 0 :(得分:3)

您可以将language='ko-kr'传递给sync_recognize(),如果服务支持则应该有效。

results = sample.sync_recognize(language_code='ko-kr')

请参阅:https://googlecloudplatform.github.io/google-cloud-python/stable/speech-usage.html#synchronous-recognition