Google Speech-to-Text API ์ฌ์ฉ ๋ฐฉ๋ฒ
Google Speech-to-Text API๋ ์์ฑ์ ํ ์คํธ๋ก ๋ณํํ๋ ๊ฐ๋ ฅํ ๋๊ตฌ๋ค.
์ด๋ฅผ ํ์ฉํ๋ ค๋ฉด Google Cloud Platform(GCP) ๊ณ์ ์ด ํ์ํ๋ฉฐ, ์๋ ๋จ๊ณ๋ฅผ ํตํด API ์ค์ ๊ณผ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ฐฐ์ธ ์ ์๋ค.
1. Google Cloud ํ๋ก์ ํธ ์ค์
- Google Cloud Console์ ๋ก๊ทธ์ธ
- ์ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๊ฑฐ๋ ๊ธฐ์กด ํ๋ก์ ํธ๋ฅผ ์ ํ
- API ๋ฐ ์๋น์ค โ API ๋ฐ ์๋น์ค ์ฌ์ฉ ์ค์ ์ผ๋ก ์ด๋
- Speech-to-Text API๋ฅผ ๊ฒ์ํ ํ ํ์ฑํ
2. ์๋น์ค ๊ณ์ ํค ์์ฑ ๋ฐ ๋ค์ด๋ก๋
์๋น์ค ๊ณ์ ํค ์์ฑ
- Google Cloud Console์์ ํ๋ก์ ํธ๋ฅผ ์ ํ
- IAM ๋ฐ ๊ด๋ฆฌ์ โ ์๋น์ค ๊ณ์ ์ผ๋ก ์ด๋
- ์๋น์ค ๊ณ์ ์์ฑ์ ํด๋ฆญํ๊ณ ๊ถํ์ ์ถ๊ฐ
- ํค ์ถ๊ฐ ๋ฒํผ์ ํด๋ฆญํ์ฌ JSON ํ์์ ํค ํ์ผ์ ๋ค์ด๋ก๋
API ํค ์์ฑ (๋์)
์๋น์ค ๊ณ์ ํค ๋์ API ํค๋ฅผ ์ฌ์ฉํ์ฌ ์ธ์ฆํ ์๋ ์๋ค:
- API ๋ฐ ์๋น์ค โ ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด๋ก ์ด๋
- ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด ๋ง๋ค๊ธฐ๋ฅผ ํด๋ฆญํ๊ณ API ํค๋ฅผ ์ ํ
- ์์ฑ๋ API ํค๋ฅผ ์ ์ฅ(API ํธ์ถ ์ ์ด ํค๋ฅผ ์ฌ์ฉ)
Info
API ํค๋ ๊ฐํธํ ์ธ์ฆ ๋ฐฉ์์ด์ง๋ง, ์๋น์ค ๊ณ์ ํค๋ ๋ ๊ฐ๋ ฅํ ๊ถํ ์ ์ด์ ๋ณด์์ ์ ๊ณตํ๋ค. ํ๋ก์ ํธ ๊ท๋ชจ์ ๋ฐ๋ผ ์ ํฉํ ๋ฐฉ๋ฒ์ ์ ํํ์.
3. ํ๊ฒฝ ๋ณ์ ์ค์
๋ค์ด๋ก๋ํ ํค ํ์ผ์ ํ๊ฒฝ ๋ณ์๋ก ์ค์ ํด์ผ ํ๋ค.
Python ์ฝ๋์์ ์ค์
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/your-service-account-key.json"ํฐ๋ฏธ๋์์ ์ค์
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-service-account-key.json"4. Google Speech-to-Text API ์ฌ์ฉ ์์
์ ์ฒด ์ฝ๋
์๋๋ Google Speech-to-Text API๋ฅผ ์ฌ์ฉํ๋ Python ์์ ์ด๋ค:
import os
from google.cloud import speech
# ํ๊ฒฝ ๋ณ์ ์ค์
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/your-service-account-key.json"
# ํด๋ผ์ด์ธํธ ์์ฑ
client = speech.SpeechClient()
# MP3 ํ์ผ ์ฝ๊ธฐ
with open('/path/to/audio.mp3', 'rb') as audio_file:
ย ย content = audio_file.read()
# RecognitionAudio ๊ฐ์ฒด ์์ฑ
audio = speech.RecognitionAudio(content=content)
# RecognitionConfig ์ค์
config = speech.RecognitionConfig(
ย ย encoding=speech.RecognitionConfig.AudioEncoding.MP3,
ย ย sample_rate_hertz=16000,
ย ย language_code='ko-KR'
)
# API ์์ฒญ
try:
ย ย response = client.recognize(config=config, audio=audio)
ย ย for result in response.results:
ย ย ย ย print("Transcript: {}".format(result.alternatives[0].transcript))
except Exception as e:
ย ย print(f"Error during API call: {e}")์ฝ๋ ์ฃผ์ ๋จ๊ณ
- ํด๋ผ์ด์ธํธ ์์ฑ
- speech.SpeechClient()๋ฅผ ์ฌ์ฉํ์ฌ API์์ ์ฐ๊ฒฐ์ ์ค์
- ์ค๋์ค ํ์ผ ์ฝ๊ธฐ
- ํ์ผ์ ๋ฐ์ด๋๋ฆฌ ํ์์ผ๋ก ์ฝ์ด RecognitionAudio ๊ฐ์ฒด๋ฅผ ์์ฑ
- RecognitionConfig ์ค์
- ์ค๋์ค ํ์ผ์ ์ธ์ฝ๋ฉ ๋ฐฉ์, ์ํ๋ง ์๋, ์ธ์ด ์ฝ๋๋ฅผ ์ง์
- ์ ์์ ์์๋ MP3 ํ์๊ณผ **ํ๊ตญ์ด(ko-KR)**๋ฅผ ์ฌ์ฉ
- API ์์ฒญ ๋ฐ ์ฒ๋ฆฌ
client.recognize()๋ก ์์ฒญ์ ๋ณด๋ด๊ณ , ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅ
5. ์คํ ๊ฒฐ๊ณผ
API๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ํธ์ถํ๋ฉด, ์ค๋์ค ํ์ผ์ ํ ์คํธ ๋ณํ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๋ค.
์๋ฅผ ๋ค์ด:
Transcript: ์๋
ํ์ธ์, ์ฌ๊ธฐ๋ Google Cloud Speech-to-Text API์
๋๋ค.์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์์ธ(Exception) ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค:
Error during API call: 403 PERMISSION_DENIED6. API ์ฌ์ฉ ์ ์ฃผ์์ฌํญ
- ์ฌ์ฉ๋ ์ ํ
- Speech-to-Text API๋ ์ฌ์ฉ๋์ ๋ฐ๋ผ ๊ณผ๊ธ๋๋ค. Google Cloud Console์์ ๋ฌด๋ฃ ํ ๋น๋๊ณผ ์ฒญ๊ตฌ ์ค์ ์ ํ์ธํ์.
- ์ค๋์ค ํ์ผ ํ์
- ์ง์๋๋ ์ค๋์ค ํ์์ ํ์ธํ๊ณ , ์ํ๋ง ์๋(sample_rate_hertz)์ ์ธ์ฝ๋ฉ(encoding)์ ์ ํํ ์ค์ ํ์.
- ํ๊ฒฝ ๋ณ์ ๋ณด์
- ์๋น์ค ๊ณ์ ํค๋ ํ๋ก์ ํธ์ ๋ฏผ๊ฐํ ์ ๋ณด๋ฅผ ํฌํจํ๋ฏ๋ก, ์์ ํ ๊ฒฝ๋ก์์ ๊ด๋ฆฌํด์ผ ํ๋ค.