CS/졸업 프로젝트(Duk to Me)
Google Cloud Text-to-Speech(TTS) API, Python으로 사용하기
letthegamesbegin7
2023. 6. 20. 02:23
반응형
이미 Speech-to-Text API사용을 위해 만들어 둔 Project를 그대로 사용할 거다.
① Google Cloud Console창(https://console.cloud.google.com/welcome/new?project=thorn-technologies-public)에서 지난 번에 생성한 Project를 Select한다.
② Library에 들어가 검색창에서 'text to speech'를 검색하여 Enable한다.
나머지 Service Account, API Key 받아오기 등과 같은 설정들은 Speech-to-Text API 설정 때 했던 것 그대로 쓰면 된다.
③ 터미널 창에서 Python 가상환경 폴더 경로로 들어가서 가상환경을 활성화 시킨다.
cd Scripts
activate.bat
④ Text-to-Speech API 사용을 위해 필요한 Package를 설치한다.
pip install --upgrade google-cloud-texttospeech
⑤ 'demo2.py' Python Script 파일을 작업폴더에 만들어 아래와 같이 작성한다.
from google.cloud import texttospeech
from google.oauth2 import service_account
# Instantiates a client
client_file = 'speech_demo.json' # JSON 파일명 넣기
credentials = service_account.Credentials.from_service_account_file(client_file)
client = texttospeech.TextToSpeechClient(credentials=credentials)
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="You're welcome. Have a safe and pleasant flight!")
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# The response's audio_content is binary.
with open("output.mp3", "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
문제가 없다면 Python파일을 Run했을 때 아래와 같이 성공 Console Message 를 보여주며 폴더 밑에는 음성파일이 성공적으로 만들어져 있을 것이다.
변환될 음성은 언어, 방언, 성별, 말하는 속도 등 공식문서를 참조하여 원하는 대로 세세하게 설정할 수 있다.
참조링크(https://cloud.google.com/text-to-speech/docs/reference/rest/v1/SsmlVoiceGender)
Reference
반응형