speech-recognition - 如何在python中使用google cloud speech api

标签 speech-recognition speech-to-text google-cloud-speech google-python-api

我正在探索 python 中的谷歌云语音 api。我正在关注这个 link . 我也提到了这个 stackoverflow link .但是我对设置环境变量感到震惊。

我做过的事情:

1.安装gcloud python模块

2.安装google-api-python-client模块

3.已经设置了服务账号(获取JSON文件)

4.获得API KEY

我对导出 GOOGLE_APPLICATION_CREDENTIALS 和 GCLOUD_PROJECT 环境变量感到震惊。

我的疑问是:

1.他们应该使用google cloud sdk导出吗?如果是这样,google cloud sdk在这里扮演什么角色,我们应该什么时候使用这个sdk?

2.由于我没有在代码中明确包含 API key , 这是否意味着我的身份验证是自动在线验证的?在这种情况下,下面代码中的 get_speech_service() 函数有什么作用?

下面是代码

import argparse
import base64
import json


import httplib2
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials


if __name__ == '__main__':
   parser = argparse.ArgumentParser()
   parser.add_argument('speech_file',help='This is the path of the audio')
   args = parser.parse_args()
   print args.speech_file
   main(args.speech_file)

def main(speech_file):
    with open(speech_file,'rb') as speech:
         speech_content = base64.b64encode(speech.read())

    service = get_speech_service()
    service_request = service.speech().syncrecognize(
    body={
        'config':{
            'encoding':'LINEAR16',
            'sampleRate':16000,
            'languageCode':'en-US',
            },
        'audio':{
            'content':speech_content.decode('UTF-8')
            }
        })
    response = service_request.execute()
    print(json.dumps(response))
DISCOVERY_URL = ('https://speech.googleapis.com/$discovery/rest?/version=v1beta1')

def get_speech_service():
    credentials = GoogleCredentials.get_application_default().create_scoped(
    ['https://www.googleapis.com/auth/cloud-platform'])
    http = httplib2.Http()
    credentials.authorize(http)
    return    discovery.build('speech','v1beta1',http=http,discoveryServiceUrl=DISCOVERY_URL)

我谷歌了很多次,我得到了提到的stackoverflow链接,这澄清了一些事情。由于我对上述疑虑不清楚,所以我在这里发布。

最佳答案

以下步骤对我有用。希望对你有用。

从 github 克隆以下 repo:

git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

导航到文件夹:

cd python-docs-samples/speech/cloud-client/

安装 pip(我确定你已经有了)和 virtualenv 。 执行以下命令:

$ virtualenv env
$ source env/bin/activate

然后从requirements.txt安装

 pip install -r requirements.txt

定义并导出 google 凭据路径(您已经这样做了)。

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_file.json

从快速示例脚本开始:

python quickstart.py

您应该得到以下输出: enter image description here

在此之后,您可以探索同一文件夹中的其他脚本,还可以尝试使用 URI 样本进行长时间识别。

关于speech-recognition - 如何在python中使用google cloud speech api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41422512/

相关文章:

c++ - 训练 sapi : Creating transcripted wav files and adding file paths to registry

javascript - 如何突出显示 Amazon Polly 中的音频播放词

speech-recognition - 获取 WAV 文件转录以与 Sphinx4 一起使用

c# - 使用 google-cloud-speech API 在 iOS 应用程序中转录长音频文件

nlp - ARPA语言模型文档

javascript - x-webkit-speech,如何自动点击麦克风图标自动录制很多单词?

python - 具有精确时间戳的语音识别?

java - 免费(最好是开源)语音到文本库的选项

google-cloud-speech - 有没有办法使用 Google Cloud Speech 生成 SRT 文件(或类似文件)?

python - Google Speech-to-text API,InvalidArgument : 400 Must use single channel (mono)