본문 바로가기

플랫폼/Google Cloud

[ Google Cloud ] 텍스트를 읽어주겠니?

Google Cloud

Text to Speach API

#Python

#command #사용자변수



0. Before

Text to Speach는 2018-06월 현재 Protocol, Java, Node.JS, Python 으로 사용가능합니다.

이 포스트에서는 Python으로 사용하는 방법을 포스팅 하였습니다.

사용 순서는 Python 외의 다른 언어도 동일하기에 순서만 보시고, Java, Node.JS 같은 경우 아래 #참조내용을 확인하세요.


Google Cloud의 기능을 사용하기 위해서는 기본적으로 다음과 같은 절차가 먼저 이루어져야 합니다.

>> Before Start Google Cloud Library


사용된 Package의 variable, method 정보를 알고 싶으면 #참조내용에 3번을 보세요. 


1. Install the Client library

$ pip install --upgrade google-cloud-texttospeech


- - - - 확인하기

혹시 오타있을까봐 명령어를 한 번 더 적어놓았습니다.

제대로 잘 설치가 되면 위와 같이 제일 마지막 줄에 Successfully installed google-cloud-texttospeech-0.2.0 이라고 뜹니다.

- - - -


2. Code

def synthesize_text(text):
   
"""Synthesizes speech from the input string of text."""
   
from google.cloud import texttospeech
    client
= texttospeech.TextToSpeechClient()

    input_text
= texttospeech.types.SynthesisInput(text=text)

   
# Note: the voice can also be specified by name.
   
# Names of voices can be retrieved with client.list_voices().
    voice
= texttospeech.types.VoiceSelectionParams(
        language_code
='en-US',
        ssml_gender
=texttospeech.enums.SsmlVoiceGender.FEMALE)

    audio_config
= texttospeech.types.AudioConfig(
        audio_encoding
=texttospeech.enums.AudioEncoding.MP3)

    response
= client.synthesize_speech(input_text, voice, audio_config)

   
# The response's audio_content is binary.
   
with open('output.mp3', 'wb') as out:
        out
.write(response.audio_content)
       
print('Audio content written to file "output.mp3"')

전체 예제코드는 #참조내용 4번 GitHub 페이지를 확인하세요.

저는 필요한 함수 코드 부분만 적어놓았습니다.


올려놓은 코드는 원본 코드를 그대로 가져온 것입니다. 필요에 따라 수정하면 되겠죠?


저의 경우에는

languge_code가 영어로 되어있으니 한국어 사용을 위해 en-US 를 ko-KR 로 수정하였습니다.

파일명 부분도 매번 바꾸어 주기 위하여 변수 형태로 지정하여 사용했습니다.


3. Environment Setting

$ export GOOGLE_APPLICATION_CREDENTIALS=json_PATH

앞서 만든 JOSN KEY를 환경변수로 설정해줍니다.


4. Program Start

$ python 실행파일명

프로그램을 실행시켜주세요.


동작이 끝나면 지정한 파일명으로 voice audio file이 만들어집니다.


5. End..

위의 과정을 따라한 후 다시 실행을 할 때는 환경변수 셋팅(3번)부터 다시 입력해주시면 됩니다.

환경변수 셋팅(3번) 이전의 과정은 1번만 해주시면 되요 :)


위의 과정을 따라했는데도 잘 안되거나 error가 나는 부분이 있다,

혹은 Google Cloud Text To Speech를 Python으로 구현한 간단한 예제가 보고싶다면

 #참조내용 5번 GitHub 페이지를 확인하세요.





#2018년06월26일