LoginSignup
4
6

More than 5 years have passed since last update.

Pythonで音声認識するまで

Posted at

環境

音声認識でテキスト化したものをmecabで形態素解析するものを作ろうと思って、音声認識で詰まったところを備忘録として残しておきます。

環境

MacOSX mojave
Python 3.7
Python 2.7

Speech APIのインストール

Google Cloud PlatformでSpeech APIを使えるようにします。

にアクセスし、ログインを行います。
コンソールの検索部分にSpeechAPIと入力し検索を行います。
APIを有効にし、プロジェクトを作成しておきます

スクリーンショット 2019-02-22 8.41.29.png

なぜか僕が検索した時にはSpeechAPIはなく(2019/2/21)、Speech-to-text APIを使うことにしました。

ここまできたらインストールをします。
https://cloud.google.com/sdk/
僕はmacosxをインストールしました。

ここから難しかった

インストールをする直前に、python2の仮想環境(gcpと適当な名前)を用意しておきました。
準備ができたらインストール

(gcp) hiroyukimrp $  ./google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!

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 <https://cloud.google.com/sdk/usage-statistics>. 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

Traceback (most recent call last):
  File "/Users/hiroyukimrp/./google-cloud-sdk/bin/bootstrapping/install.py", line 214, in <module>
    main()
  File "/Users/hiroyukimrp/./google-cloud-sdk/bin/bootstrapping/install.py", line 192, in main
    Install(pargs.override_components, pargs.additional_components)
  File "/Users/hiroyukimrp/./google-cloud-sdk/bin/bootstrapping/install.py", line 138, in Install
    _CLI.Execute(['--quiet', 'components', 'list'])
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 815, in Execute
    self._HandleAllErrors(exc, command_path_string, specified_arg_names)
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 853, in _HandleAllErrors
    exceptions.HandleError(exc, command_path_string, self.__known_error_handler)
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 789, in Execute
    resources = calliope_command.Run(cli=self, args=args)
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 756, in Run
    resources = command_instance.Run(args)
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/surface/components/list.py", line 95, in Run
    only_local_state=args.only_local_state)
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/core/updater/update_manager.py", line 669, in List
    to_print, current_version, latest_version = self._GetPrintListWithDiff()
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/core/updater/update_manager.py", line 699, in _GetPrintListWithDiff
    _, diff = self._GetStateAndDiff(command_path='components.list')
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/core/updater/update_manager.py", line 595, in _GetStateAndDiff
    command_path=command_path)
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/core/updater/update_manager.py", line 578, in _GetLatestSnapshot
    *effective_url.split(','), command_path=command_path)
  File "/Users/hiroyukimrp/google-cloud-sdk/lib/googlecloudsdk/core/updater/snapshots.py", line 171, in FromURLs
    for url in urls]
 ・
 ・
 ・
 ・
  File "/Users/hiropy/google-cloud-sdk/lib/third_party/httplib2/__init__.py", line 1081, in connect
    raise SSLHandshakeError(e)
httplib2.SSLHandshakeError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)

エラー出ました。こちらをみてましたが、改善せず。

小一時間検索しまくった挙句、間違ってpython2の仮想環境じゃないところ(python3)で同様のインストールした結果エラーがなくなりました。

hiroyukimrp$  ./google-cloud-sdk/install.sh

Welcome to the Google Cloud SDK!

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 <https://cloud.google.com/sdk/usage-statistics>. 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 #yes押しました。


Your current Cloud SDK version is: 180.0.0
The latest available version is: 235.0.0

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                   Components                                                   │
├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤
│      Status      │                         Name                         │            ID            │    Size   │
├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤
│ Update Available │ BigQuery Command Line Tool                           │ bq                       │   < 1 MiB │
│ Update Available │ Cloud SDK Core Libraries                             │ core                     │   9.5 MiB │
│ Update Available │ Cloud Storage Command Line Tool                      │ gsutil                   │   3.6 MiB │
│ Not Installed    │ App Engine Go Extensions                             │ app-engine-go            │  56.4 MiB │
│ Not Installed    │ Cloud Bigtable Command Line Tool                     │ cbt                      │   6.3 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 │  32.1 MiB │
│ Not Installed    │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │  33.4 MiB │
│ Not Installed    │ Cloud SQL Proxy                                      │ cloud_sql_proxy          │   3.7 MiB │
│ Not Installed    │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │  14.5 MiB │
│ Not Installed    │ Google Cloud Build Local Builder                     │ cloud-build-local        │   5.9 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          │ 108.8 MiB │
│ Not Installed    │ gcloud app PHP Extensions                            │ app-engine-php           │  21.9 MiB │
│ Not Installed    │ gcloud app Python Extensions                         │ app-engine-python        │   6.2 MiB │
│ Not Installed    │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras │  28.5 MiB │
│ Not Installed    │ kubectl                                              │ kubectl                  │   < 1 MiB │
└──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘
To install or remove components at your current SDK version [180.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [235.0.0], run:
  $ gcloud components update

==> Source [/Users/hiropy/google-cloud-sdk/completion.bash.inc] in your profile to enable shell command completion for gcloud.
==> Source [/Users/hiropy/google-cloud-sdk/path.bash.inc] in your profile to add the Google Cloud SDK command line tools to your $PATH.

For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts

たくさん出てきた。これうまくいっているのか?
よくわらかず、curlコマンドやりなおしてみました。
https://cloud.google.com/sdk/downloads

$ curl https://sdk.cloud.google.com | bash
・
・
・
Remove it before installing? (y/N): y
rm -rf /Users/hiroyukimrp/google-cloud-sdk
tar -C /Users/hiroyukimrp -zxvf /var/folders/wc/lxgylkbj3vbb469ckbq83mr40000gq/T/tmp.XXXXXXXXXX.FQQP34Bc/google-cloud-sdk.tar.gz
・
・
・
/Users/hiroyukimrp/google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!

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 <https://cloud.google.com/sdk/usage-statistics>. 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


This will install all the core command line tools necessary for working with
the Google Cloud Platform.



Your current Cloud SDK version is: 235.0.0
Installing components from version: 235.0.0

┌────────────────────────────────────────────────────────────────────────────┐
│                    These components will be installed.                     │
├─────────────────────────────────────────────────────┬────────────┬─────────┤
│                         Name                        │  Version   │   Size  │
├─────────────────────────────────────────────────────┼────────────┼─────────┤
│ BigQuery Command Line Tool                          │     2.0.41 │ < 1 MiB │
│ BigQuery Command Line Tool (Platform Specific)      │     2.0.34 │ < 1 MiB │
│ Cloud SDK Core Libraries (Platform Specific)        │ 2018.09.24 │ < 1 MiB │
│ Cloud Storage Command Line Tool                     │       4.36 │ 3.6 MiB │
│ Cloud Storage Command Line Tool (Platform Specific) │       4.34 │ < 1 MiB │
│ Default set of gcloud commands                      │            │         │
│ gcloud cli dependencies                             │ 2018.08.03 │ 1.5 MiB │
└─────────────────────────────────────────────────────┴────────────┴─────────┘

For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area                             ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool                   ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool (Platform Spec... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud SDK Core Libraries (Platform Specific) ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool              ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Default set of gcloud commands               ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud cli dependencies                      ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.                                       

Update done!


Modify profile to update your $PATH and enable shell command 
completion?

Do you want to continue (Y/n)?  Y

なんかできた気がする。とりあえずcontinue。

シェルを再起動して、gcloud 環境を初期化します。

$ exec -l $SHELL
$ gcloud init

適当に情報を入力。https://qiita.com/k_shiota/items/d511382d8406a323e960

認証設定

$ python -m pip install --upgrade pip
$ pip install google gcloud google-auth google-cloud-speech grpc-google-cloud-speech-v1beta1

多分準備できました。
クイックスタートはこちら→https://cloud.google.com/sdk/docs/quickstart-mac-os-x

pyaudioの準備

多分これで大丈夫

$ brew update
$ brew install portaudio
$ brew link --overwrite portaudio
$ pip install pyaudio

ファイル作成

Desktopにmic.pyを作成しました。コードはこちら使わせていただきました。

$ python mic.py
0 Built-in Microphone
1 Built-in Output

こう表示されたら、あとは喋るだけ。完成です。

GCPの準備が大変でした。いや無知な自分にびっくりした。。

参照

https://qiita.com/delete/items/395776c6843d67fd65fd
https://qiita.com/hamham/items/9b553d0759a2319ea211

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6