1 基础知识
1.1 介绍
本章以部署语音识别API为例实践如何部署Google的云SDK。
1.2 语音识别API的简介
Google云客户端允许你以喜欢的编程语言将语音识别请求发送到云语音结构然后转换成文本。
1.3 语音识别的方法
– Synchronous Recognition(同步识别),将音频数据发送到语音转文本API,识别后将返回文本结果(限1分钟内的音频数据)
– Asynchronous Recognition(即异步识别),将音频数据发送到语音转文本API并启用长时间运行操作(支持长达480分钟的连续音频数据异步请求)
– Streaming Recognition(即流识别),对双向的音频数据流执行识别,例如识别麦克风的实时音频
1.4 部署方法
– 登录Google账户,如果没有请先注册
– 配置GCP控制台项目并启用Google Speech-to-Text接口(注意需要下载JSON私钥)
– 配置客户端系统的环境变量“GOOGLE_APPLICATION_CREDENTIALS”指向JSON私钥的路径
– 安装并初始化Cloud SDK
2 最佳实践
2.1 系统环境配置
IP Address = 10.168.0.90
Host Name = googleAPI.cmdschool.org
OS = CentOS 7.x x86_64
2.2 部署SDK软件包
2.2.1 下载Google Cloud SDK
cd ~ wget --no-check-certificate https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-245.0.0-linux-x86_64.tar.gz
2.2.3 解压Google Cloud SDK
cd ~ tar -xf google-cloud-sdk-245.0.0-linux-x86_64.tar.gz
2.2.4 解压Google Cloud SDK
cd ~ mv google-cloud-sdk /usr/
2.3 配置SDK证书
2.3.1 部署证书目录
vim /usr/google-cloud-sdk/data/credentials.json
将申请到的证书JSON文件内容写入,
{ "type": "service_account", "project_id": "xxxx", "private_key_id": "xxxx", "private_key": "xxxx", "client_email": "xxx@appspot.gserviceaccount.com", "client_id": "", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxx.gserviceaccount.com" }
2.3.2 配置证书环境变量
echo 'export GOOGLE_CLOUD_SDK_HOME=/usr/google-cloud-sdk' > /etc/profile.d/google-cloud-sdk_credentials.sh echo 'export GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_CLOUD_SDK_HOME}/data/credentials.json' >> /etc/profile.d/google-cloud-sdk_credentials.sh echo 'export PATH=${GOOGLE_APPLICATION_CREDENTIALS}:$PATH' >> /etc/profile.d/google-cloud-sdk_credentials.sh source /etc/profile.d/google-cloud-sdk_credentials.sh
2.4 配置SDK软件包
2.4.1 确认Python的版本
python2 --version
注:我们需要Python 2以上的版本
2.4.2 执行安装
cd ${GOOGLE_CLOUD_SDK_HOME} ./install.sh
向导如下,
Welcome to the Google Cloud SDK! WARNING: You appear to be running this script as root. This may cause the installation to be inaccessible to users other than the root user. To help improve the quality of this product, we collect anonymized usage data and anonymized stacktraces when crashes are encountered; additional information is available at . You may choose to opt out of this collection now (by choosing 'N' at the below prompt), or at any time in the future by running the following command: gcloud config set disable_usage_reporting true Do you want to help improve the Google Cloud SDK (Y/n)? y Your current Cloud SDK version is: 245.0.0 The latest available version is: 245.0.0 ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ Components │ ├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤ │ Status │ Name │ ID │ Size │ ├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤ │ Not Installed │ App Engine Go Extensions │ app-engine-go │ 56.6 MiB │ │ Not Installed │ Cloud Bigtable Command Line Tool │ cbt │ 6.4 MiB │ │ Not Installed │ Cloud Bigtable Emulator │ bigtable │ 5.6 MiB │ │ Not Installed │ Cloud Datalab Command Line Tool │ datalab │ < 1 MiB │ │ Not Installed │ Cloud Datastore Emulator │ cloud-datastore-emulator │ 18.4 MiB │ │ Not Installed │ Cloud Datastore Emulator (Legacy) │ gcd-emulator │ 38.1 MiB │ │ Not Installed │ Cloud Firestore Emulator │ cloud-firestore-emulator │ 40.5 MiB │ │ Not Installed │ Cloud Pub/Sub Emulator │ pubsub-emulator │ 34.8 MiB │ │ Not Installed │ Cloud SQL Proxy │ cloud_sql_proxy │ 3.8 MiB │ │ Not Installed │ Emulator Reverse Proxy │ emulator-reverse-proxy │ 14.5 MiB │ │ Not Installed │ Google Cloud Build Local Builder │ cloud-build-local │ 6.0 MiB │ │ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr │ 1.8 MiB │ │ Not Installed │ gcloud Alpha Commands │ alpha │ < 1 MiB │ │ Not Installed │ gcloud Beta Commands │ beta │ < 1 MiB │ │ Not Installed │ gcloud app Java Extensions │ app-engine-java │ 105.6 MiB │ │ Not Installed │ gcloud app PHP Extensions │ app-engine-php │ │ │ Not Installed │ gcloud app Python Extensions │ app-engine-python │ 6.0 MiB │ │ Not Installed │ gcloud app Python Extensions (Extra Libraries) │ app-engine-python-extras │ 28.5 MiB │ │ Not Installed │ kubectl │ kubectl │ < 1 MiB │ │ Installed │ BigQuery Command Line Tool │ bq │ Start a new shell for the changes to take effect. For more information on how to get started, please visit: https://cloud.google.com/sdk/docs/quickstarts
2.4.3 导入环境变量
source /etc/profile.d/google-cloud-sdk.sh
2.4.4 安装缺失的组件
cd ~ gcloud components install app-engine-go cbt bigtable datalab gcd-emulator cloud-firestore-emulator pubsub-emulator cloud_sql_proxy emulator-reverse-proxy cloud-build-local docker-credential-gcr alpha beta app-engine-java app-engine-php app-engine-python-extras kubectl
2.4.5 确认组件安装
gcloud components list
可见如下显示,
Your current Cloud SDK version is: 245.0.0 The latest available version is: 245.0.0 ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ Components │ ├───────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤ │ Status │ Name │ ID │ Size │ ├───────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤ │ Installed │ App Engine Go Extensions │ app-engine-go │ 56.6 MiB │ │ Installed │ BigQuery Command Line Tool │ bq │ < 1 MiB │ │ Installed │ Cloud Bigtable Command Line Tool │ cbt │ 6.4 MiB │ │ Installed │ Cloud Bigtable Emulator │ bigtable │ 5.6 MiB │ │ Installed │ Cloud Datalab Command Line Tool │ datalab │ < 1 MiB │ │ Installed │ Cloud Datastore Emulator │ cloud-datastore-emulator │ 18.4 MiB │ │ Installed │ Cloud Datastore Emulator (Legacy) │ gcd-emulator │ 38.1 MiB │ │ Installed │ Cloud Firestore Emulator │ cloud-firestore-emulator │ 40.5 MiB │ │ Installed │ Cloud Pub/Sub Emulator │ pubsub-emulator │ 34.8 MiB │ │ Installed │ Cloud SDK Core Libraries │ core │ 10.3 MiB │ │ Installed │ Cloud SQL Proxy │ cloud_sql_proxy │ 3.8 MiB │ │ Installed │ Cloud Storage Command Line Tool │ gsutil │ 3.8 MiB │ │ Installed │ Emulator Reverse Proxy │ emulator-reverse-proxy │ 14.5 MiB │ │ Installed │ Google Cloud Build Local Builder │ cloud-build-local │ 6.0 MiB │ │ Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr │ 1.8 MiB │ │ Installed │ gcloud Alpha Commands │ alpha │ < 1 MiB │ │ Installed │ gcloud Beta Commands │ beta │ < 1 MiB │ │ Installed │ gcloud app Java Extensions │ app-engine-java │ 105.6 MiB │ │ Installed │ gcloud app PHP Extensions │ app-engine-php │ │ │ Installed │ gcloud app Python Extensions │ app-engine-python │ 6.0 MiB │ │ Installed │ gcloud app Python Extensions (Extra Libraries) │ app-engine-python-extras │ 28.5 MiB │ │ Installed │ kubectl │ kubectl │ < 1 MiB │ └───────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘ To install or remove components at your current SDK version [245.0.0], run: $ gcloud components install COMPONENT_ID $ gcloud components remove COMPONENT_ID To update your SDK installation to the latest version [245.0.0], run: $ gcloud components update
2.5 初始化SDK软件包
2.5.1 运行初始化命令
gcloud init
向导提示如下,
Welcome! This command will take you through the configuration of gcloud. Your current configuration has been set to: [default] You can skip diagnostics next time by using the following flag: gcloud init --skip-diagnostics Network diagnostic detects and fixes local network connection issues. Checking network connection...done. Reachability Check passed. Network diagnostic passed (1/1 checks passed). You must log in to continue. Would you like to log in (Y/n)? y Go to the following link in your browser: https://accounts.google.com/o/oauth2/auth?redirect_uri=xxxx Enter verification code: xxxxxxxxxxxxxxxxxxx
根据上面的提示,我们将连接贴到浏览器中运行并根据页面的提示操作后取得验证码并输入,得到如下提示,
You are logged in as: [xxx@cmdschool.org]. Pick cloud project to use: [1] xxx-c03d0 Please enter numeric choice or text value (must exactly match list item): 1 Your current project has been set to: [xxx-c03d0]. Not setting default zone/region (this feature makes it easier to use [gcloud compute] by setting an appropriate default value for the --zone and --region flag). See https://cloud.google.com/compute/docs/gcloud-compute section on how to set default compute region and zone manually. If you would like [gcloud init] to be able to do this for you the next time you run it, make sure the Compute Engine API is enabled for your project on the https://console.developers.google.com/apis page. Created a default .boto configuration file at [/root/.boto]. See this file and [https://cloud.google.com/storage/docs/gsutil/commands/config] for more information about configuring Google Cloud Storage. Your Google Cloud SDK is configured and ready to use! * Commands that require authentication will use xxx@cmdschool.org by default * Commands will reference project `xxx-c03d0` by default Run `gcloud help config` to learn how to change individual settings This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects. Run `gcloud topic configurations` to learn more. Some things to try next: * Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command. * Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting
另外,以上配置完成会在用户家目录创建以下配置文件,
ls ~/.boto
2.5.2 查看认证的账号
gcloud auth list
可见如下提示,
Credentialed Accounts ACTIVE ACCOUNT * xxx@cmdschool.org To set the active account, run: $ gcloud config set account `ACCOUNT`
2.5.3 查看配置
gcloud config list
可见如下提示,
[core] account = xxx@cmdschool.org disable_usage_reporting = False project = xxx-c03d0 Your active configuration is: [default]
2.5.4 撤销登录的账号
gcloud auth revoke xxx@cmdschool.org
可见如下提示,
Revoked credentials: - xxx@cmdschool.org
2.5.5 查看Gloud SDK的信息
gcloud auth revoke xxx@cmdschool.org
可见如下提示,
Google Cloud SDK [245.0.0] Platform: [Linux, x86_64] ('Linux', 'googleAPI.cmdschool.org', '3.10.0-514.el7.x86_64', '#1 SMP Tue Nov 22 16:42:41 UTC 2016', 'x86_64', 'x86_64') Python Version: [2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]] Python Location: [/bin/python2] Site Packages: [Disabled] Installation Root: [/usr/google-cloud-sdk] Installed Components: kubectl: [2019.05.03] app-engine-python: [1.9.85] pubsub-emulator: [2019.04.26] app-engine-python-extras: [1.9.74] gsutil: [4.38] cloud-datastore-emulator: [2.1.0] cbt: [] emulator-reverse-proxy: [] app-engine-java: [1.9.74] core: [2019.05.03] datalab: [20190116] cloud-firestore-emulator: [1.4.5] cloud-build-local: [] docker-credential-gcr: [] beta: [2019.02.22] bq: [2.0.43] alpha: [2019.02.22] bigtable: [] cloud_sql_proxy: [] app-engine-go: [] app-engine-php: [ ] gcd-emulator: [v1beta3-1.0.0] System PATH: [/usr/redis/src:/usr/java/jdk1.8.0_65/bin:/usr/google-cloud-sdk/data/credentials.json:/usr/google-cloud-sdk/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/var/lib/imapi/.local/bin:/var/lib/imapi/bin] Python PATH: [/usr/google-cloud-sdk/lib/third_party:/usr/google-cloud-sdk/lib:/usr/lib64/python27.zip:/usr/lib64/python2.7/:/usr/lib64/python2.7/plat-linux2:/usr/lib64/python2.7/lib-tk:/usr/lib64/python2.7/lib-old:/usr/lib64/python2.7/lib-dynload] Cloud SDK on PATH: [True] Kubectl on PATH: [/usr/google-cloud-sdk/bin/kubectl] Installation Properties: [/usr/google-cloud-sdk/properties] User Config Directory: [/var/lib/imapi/.config/gcloud] Active Configuration Name: [default] Active Configuration Path: [/var/lib/imapi/.config/gcloud/configurations/config_default] Account: [xxx@cmdschool.org] Project: [xxx-c03d0] Current Properties: [core] project: [xxx-c03d0] account: [xxx@cmdschool.org] disable_usage_reporting: [False] Logs Directory: [/var/lib/imapi/.config/gcloud/logs] Last Log File: [/var/lib/imapi/.config/gcloud/logs/2019.05.17/10.46.22.012245.log] git: [NOT AVAILABLE] ssh: [OpenSSH_6.6.1p1, OpenSSL 1.0.1e-fips 11 Feb 2013]
参阅文档
======================
快熟入门,
—————-
https://cloud.google.com/sdk/docs/quickstarts?refresh=1
安装方法,
—————–
https://cloud.google.com/sdk/docs/?refresh=1#linux
语音转换成文本的基础知识,
————————-
https://cloud.google.com/speech-to-text/docs/basics
没有评论