Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Speech recognition 如何在python中使用google云语音api_Speech Recognition_Speech To Text_Google Cloud Speech_Google Python Api - Fatal编程技术网

Speech recognition 如何在python中使用google云语音api

Speech recognition 如何在python中使用google云语音api,speech-recognition,speech-to-text,google-cloud-speech,google-python-api,Speech Recognition,Speech To Text,Google Cloud Speech,Google Python Api,我正在用python探索google云语音api。我正在关注这一点。 我也提到了这一点,但我对环境变量的设置感到惊讶 我所做的事情: 1.已安装gcloud python模块 2.安装google api python客户端模块 3.已建立服务账号(获取JSON文件) 4.获得API密钥 我在导出GOOGLE应用程序凭据和GCLOUD项目环境变量时被打动了 我的疑问是: 1.是否应该使用google cloud sdk导出?如果是,google cloud sdk在这里扮演什么角色?我们应该何时

我正在用python探索google云语音api。我正在关注这一点。 我也提到了这一点,但我对环境变量的设置感到惊讶

我所做的事情:

1.已安装gcloud python模块

2.安装google api python客户端模块

3.已建立服务账号(获取JSON文件)

4.获得API密钥

我在导出GOOGLE应用程序凭据和GCLOUD项目环境变量时被打动了

我的疑问是:

1.是否应该使用google cloud sdk导出?如果是,google cloud sdk在这里扮演什么角色?我们应该何时使用此sdk

2.由于我没有在代码中显式地封装API密钥, 这是否意味着我的身份验证是在线自动验证的?在这种情况下,下面代码中的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凭据路径(您已经在这样做了)

从快速示例脚本开始:

python quickstart.py
您应该得到以下输出:

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

python quickstart.py