痞子衡嵌入式:語音處理工具Jays-PySPEECH誕生記(1)- 環境搭建(Python2.7.14 + PyAudio0.2.11 + Matplotlib2.2.3 + SpeechRecognition3.8.1 + pyttsx3 2.7)

痞子衡發表於2017-06-04

  大家好,我是痞子衡,是正經搞技術的痞子。今天痞子衡給大家介紹的是語音處理工具Jays-PySPEECH誕生之環境搭建

  在寫Jays-PySPEECH時需要先搭好開發環境,下表列出了開發過程中會用到的所有軟體/工具包:

一、涉及工具列表

工具 功能 下載地址
Python 2.7.14 Python官方包(直譯器) https://www.python.org/
PyAudio 0.2.11 跨平臺開源Audio I/O庫 PortAudio 的Python封裝 http://people.csail.mit.edu/hubert/pyaudio/
Matplotlib 2.2.3 一款非常強大的Python 2D繪相簿 https://matplotlib.org/
https://github.com/matplotlib/matplotlib
NumPy 1.15.0 基礎Python科學計算包 http://www.numpy.org/
https://www.scipy.org/
SpeechRecognition 3.8.1 一款支援多引擎的Python語音識別(ASR)庫 https://github.com/Uberi/speech_recognition
PocketSphinx 0.1.15 卡內基-梅隆大學開源語音識別引擎 CMU Sphinx 的Python封裝 https://github.com/bambocher/pocketsphinx-python
https://pypi.org/project/pocketsphinx/
pyttsx3 2.7 pyTTS, pyttsx專案的延續之作,一款輕量級的Python文語合成引擎 https://github.com/nateshmbhat/pyttsx3
https://pypi.org/project/pyttsx3/
eSpeak 1.48.04 一款開源的TTS,可將轉換結果儲存為wav http://espeak.sourceforge.net/
wxPython 4.0.3 跨平臺開源GUI庫 wxWidgets 的Python封裝庫 https://www.wxpython.org/
https://pypi.org/project/wxPython/
wxFormBuilder 3.8.0 wxPython GUI介面構建工具 https://github.com/wxFormBuilder/wxFormBuilder
PyCharm Community 2018.02 一款流行的Python整合開發環境 http://www.jetbrains.com/pycharm/

二、基礎環境搭建(Python + PyAudio + Matplotlib + NumPy)

  Jays-PySPEECH工具是一個完全基於Python語言開發的應用軟體,首先安裝好Python 2.7.14,痞子衡的安裝目錄為C:\tools_mcu\Python27,安裝完成後確保系統環境變數裡包括該路徑(C:\tools_mcu\Python27),因為該路徑下包含python.exe,後續python命令需呼叫這個python.exe完成的。此外pip是Python的包管理工具,我們可以藉助pip來安裝PyAudio和Matplotlib包(NumPy含在Matplotlib裡):

PS C:\tools_mcu\Python27\Scripts> .\pip.exe install pyaudio

Collecting pyaudio
 Downloading https://files.pythonhosted.org/packages/94/3e/430d4e4e24e89b19c1df052644f69e03d64c1ae2e83f5a14bd365e0236de/PyAudio-0.2.11-cp27-cp27m-win_amd64.whl (52kB)
Installing collected packages: pyaudio
Successfully installed pyaudio-0.2.11

PS C:\tools_mcu\Python27\Scripts> .\pip.exe install matplotlib

Collecting matplotlib
  Downloading https://files.pythonhosted.org/packages/f7/5b/4bc804df462961a3f0d138243611ce24b7899db04e6043e46df0ff1080e9/matplotlib-2.2.3-cp27-cp27m-win_amd64.whl (8.4MB)
Collecting backports.functools-lru-cache (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/03/8e/2424c0e65c4a066e28f539364deee49b6451f8fcd4f718fefa50cc3dcf48/backports.functools_lru_cache-1.5-py2.py3-none-any.whl
Requirement already satisfied, skipping upgrade: six>=1.10 in c:\tools_mcu\python27\lib\site-packages (from matplotlib) (1.11.0)
Collecting pytz (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
Collecting cycler>=0.10 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl
Collecting kiwisolver>=1.0.1 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/e0/3a/2fda27dacdfafcf8f40cce2be09890b1443af3e65c3ab8f7294216a2946b/kiwisolver-1.0.1-cp27-none-win_amd64.whl (64kB)
Collecting python-dateutil>=2.1 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/cf/f5/af2b09c957ace60dcfac112b669c45c8c97e32f94aa8b56da4c6d1682825/python_dateutil-2.7.3-py2.py3-none-any.whl (211kB)
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/6a/8a/718fd7d3458f9fab8e67186b00abdd345b639976bc7fb3ae722e1b026a50/pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
Collecting numpy>=1.7.1 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/3d/d6/f04730ad69240be04584b3979dcd2f0b25f9e58463547df6fcafa139c567/numpy-1.15.0-cp27-none-win_amd64.whl (13.5MB)
Requirement already satisfied, skipping upgrade: setuptools in c:\tools_mcu\python27\lib\site-packages (from kiwisolver>=1.0.1->matplotlib) (28.8.0)
Installing collected packages: backports.functools-lru-cache, pytz, cycler, kiwisolver, python-dateutil, pyparsing, numpy, matplotlib
Successfully installed backports.functools-lru-cache-1.5 cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.3 numpy-1.15.0 pyparsing-2.2.0 python-dateutil-2.7.3 pytz-2018.5

  有了PyAudio便可以讀寫Audio,有了Matplotlib便可以將Audio以波形方式圖形化顯示出來。這兩個工具安裝完成,JaysPySPEECH工具開發的Python基礎環境便搭好了。

Note: 關於GUI及除錯等相關工具(wxPython、wxFormBuilder、PyCharm)的安裝詳見痞子衡另一個作品 tinyPyCOM的環境搭建

二、高階環境搭建(SpeechRecognition + PocketSphinx + pyttsx3 + eSpeak)

  上一步主要安裝了Jays-PySPEECH的基礎開發環境,用於Audio的錄播與顯示,但是Jays-PySPEECH設計之初便考慮支援語音識別、文語轉換功能,因為我們還需要進一步安裝相關Python庫。
  首先安裝語音識別庫,SpeechRecognition是一款非常流行的支援多引擎的語音識別Python庫,痞子衡為Jays-PySPEECH選用的就是SpeechRecognition,其中語音識別引擎選用的是可以離線工作的PocketSphinx,具體安裝如下:

PS C:\tools_mcu\Python27\Scripts> .\pip.exe install SpeechRecognition

Collecting SpeechRecognition
  Downloading https://files.pythonhosted.org/packages/26/e1/7f5678cd94ec1234269d23756dbdaa4c8cfaed973412f88ae8adf7893a50/SpeechRecognition-3.8.1-py2.py3-none-any.whl (32.8MB)
Installing collected packages: SpeechRecognition
Successfully installed SpeechRecognition-3.8.1

PS C:\tools_mcu\Python27\Scripts> python -m pip install --upgrade pip setuptools wheel

Requirement already up-to-date: pip in c:\tools_mcu\python27\lib\site-packages (18.0)
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/66/e8/570bb5ca88a8bcd2a1db9c6246bb66615750663ffaaeada95b04ffe74e12/setuptools-40.2.0-py2.py3-none-any.whl (568kB)
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/81/30/e935244ca6165187ae8be876b6316ae201b71485538ffac1d718843025a9/wheel-0.31.1-py2.py3-none-any.whl (41kB)
Installing collected packages: setuptools, wheel
  Found existing installation: setuptools 28.8.0
    Uninstalling setuptools-28.8.0:
      Successfully uninstalled setuptools-28.8.0
Successfully installed setuptools-40.2.0 wheel-0.31.1

PS C:\tools_mcu\Python27\Scripts> .\pip.exe install --upgrade pocketsphinx

Collecting pocketsphinx
  Downloading https://files.pythonhosted.org/packages/38/d3/192476022e989377ab00cb84fb0b18790e400bbd58e464155c58cb4622f8/pocketsphinx-0.1.15-cp27-cp27m-win_amd64.whl (29.1MB)
Installing collected packages: pocketsphinx
Successfully installed pocketsphinx-0.1.15

  最後安裝文語合成庫,pyttsx3是一款超輕量級的文語合成Python庫,其是經典的pyTTS、pyttsx專案的延續,其核心為Microsoft Speech API (SAPI5),可離線工作,具體安裝如下:

PS C:\tools_mcu\Python27\Scripts> .\pip.exe install pyttsx3

Collecting pyttsx3
  Downloading https://files.pythonhosted.org/packages/24/4e/580726c73272344d3e74b7aaffae55ff6b6450061fbecb8cc6e112531c02/pyttsx3-2.7.tar.gz
Requirement already satisfied: pypiwin32 in c:\tools_mcu\python27\lib\site-packages (from pyttsx3) (223)
Requirement already satisfied: pywin32>=223 in c:\tools_mcu\python27\lib\site-packages (from pypiwin32->pyttsx3) (223)
Building wheels for collected packages: pyttsx3
  Running setup.py bdist_wheel for pyttsx3 ... done
  Stored in directory: C:\Users\nxa07314\AppData\Local\pip\Cache\wheels\a2\8a\fe\11112aca9c89142c3a404bc67ef3393a7ad530da26639a05d4
Successfully built pyttsx3
Installing collected packages: pyttsx3
Successfully installed pyttsx3-2.7

  pyttsx3僅能線上發聲,無法儲存到wav檔案,因此我們還需要一個可以儲存wav檔案的TTS,痞子衡選擇了eSpeak,其具體安裝詳見系列第六篇。到了這裡,Jays-PySPEECH工具開發的Python環境便全部搭好了。

  至此,語音處理工具Jays-PySPEECH誕生之環境搭建痞子衡便介紹完畢了,掌聲在哪裡~~~

相關文章