조현우
조현우

Reputation: 11

How to set language in Google Speech API Python client

I want to know about Google speech to text API. I tried sample code that is provided by Google cloud API.

This is my code:

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

Then I want to know about how I can use other languages instead of English. One thing that I want to use Korean recognition, not English. So please, what should I put into this code for Korean recognition? ko-kr is the language code that is provided by Google.

Upvotes: 1

Views: 2346

Answers (1)

Thomas Schultz
Thomas Schultz

Reputation: 2467

You can pass language='ko-kr' to sync_recognize() and if the service supports it should work.

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

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

Upvotes: 3

Related Questions