<font size = 6>**Google Speech-to-Text API**</font> ## <font size = 5>VoiceTalk運用</font> 安裝套件 ``` pip install google-cloud-speech ``` ## <font size = 5>API使用</font> ```python= import io from google.oauth2 import service_account from google.cloud import speech client_file = "金鑰.json" credentials = service_account.Credentials.from_service_account_file(client_file) client = speech.SpeechClient(credentials = credentials) #audio_file : 音檔路徑 def SpeechToText(audio_file): with io.open(audio_file, 'rb') as f: content=f.read() audio = speech.RecognitionAudio(content = content) config = speech.RecognitionConfig( encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz = 48000, language_code='en-US', model="telephony_short", audio_channel_count=1, enable_word_confidence=True, enable_word_time_offsets=True ) operation = client.long_running_recognize(config=config, audio=audio) response = operation.result(timeout=90) r = response.results[0] return r.alternatives[0].transcript, r.alternatives[0].confidence, r.alternatives[0].words, r.result_end_time, r.language_code # 辨識結果、信心度、每個單字的開始和結束時間戳、音檔時長、語言 ``` ## <font size = 5> Speech-to-Text設定與獲得API金鑰 </font> > 側邊欄:「API和服務」-> 「程式庫」 選擇並啟用"Cloud Speect-to-Text API" ![image](https://hackmd.io/_uploads/HJlwDv_-xg.png) ![image](https://hackmd.io/_uploads/BJZODDdbgl.png) **建立「服務帳戶」** 側邊欄:「IAM 與管理」-> 「服務帳戶」 ![](https://hackmd.io/_uploads/ryE839_ya.png) 設定「服務帳戶名稱」和「服務帳戶 ID」:隨意取 ![](https://hackmd.io/_uploads/ryLX6qOy6.png) 「基本版」->「瀏覽者」 ![image](https://hackmd.io/_uploads/rJoYDvOWle.png) 直接按「完成」即可 --- **產生金鑰** 「管理金鑰」,進入金鑰管理頁面 ![](https://hackmd.io/_uploads/BkqtAq_ya.png) 「新增金鑰」,選擇 JSON,並按下建立。 ![](https://hackmd.io/_uploads/HJy5A9dya.png) ![](https://hackmd.io/_uploads/rJyqAqu1p.png) ![](https://hackmd.io/_uploads/SkJ9A5dkT.png)