快速入門:使用適用於 Python 的語音 SDK 來辨識語音Quickstart: Recognize speech with the Speech SDK for Python

本文說明如何透過適用於 Python 的語音 SDK 來使用語音服務。This article shows how to use the Speech Services through the Speech SDK for Python. 它會說明如何從麥克風輸入辨識語音。It illustrates how to recognize speech from microphone input.

必要條件Prerequisites

  • 適用於語音服務的 Azure 訂用帳戶金鑰。An Azure subscription key for the Speech Services. 免費取得一個金鑰Get one for free.

  • Python 3.5 或更新版本Python 3.5 or later.

  • Python 語音 SDK 套件適用於下列作業系統:The Python Speech SDK package is available for these operating systems:

    • Windows:x64 和 x86。Windows: x64 and x86.
    • Mac:macOS X 10.12 版或更新版本。Mac: macOS X version 10.12 or later.
    • Linux:x64 上的 Ubuntu 16.04、Ubuntu 18.04、Debian 9。Linux: Ubuntu 16.04, Ubuntu 18.04, Debian 9 on x64.
  • 在 Linux 上,執行下列命令以安裝必要的套件:On Linux, run these commands to install the required packages:

    • 在 Ubuntu 上:On Ubuntu:

      sudo apt-get update
      sudo apt-get install build-essential libssl1.0.0 libasound2
      
    • 在 Debian 9 上:On Debian 9:

      sudo apt-get update
      sudo apt-get install build-essential libssl1.0.2 libasound2
      
  • 在 Windows 上,您需要適用於您平台的適用於 Visual Studio 2019 的 Microsoft Visual C++ 可轉散發套件On Windows, you need the Microsoft Visual C++ Redistributable for Visual Studio 2019 for your platform.

安裝語音 SDKInstall the Speech SDK

重要

下載此頁面上的任何「Azure 認知服務的語音 SDK」元件,即表示您知悉其授權。By downloading any of the Speech SDK for Azure Cognitive Services components on this page, you acknowledge its license. 請參閱語音 SDK 的 Microsoft 軟體授權條款See the Microsoft Software License Terms for the Speech SDK.

此命令會從 PyPI 安裝語音 SDK 的 Python 套件:This command installs the Python package from PyPI for the Speech SDK:

pip install azure-cognitiveservices-speech

支援及更新Support and updates

語音 SDK Python 套件的更新會透過 PyPI 散發,並在版本資訊上宣佈。Updates to the Speech SDK Python package are distributed via PyPI and announced in the Release notes. 如果有新版本可用,您可以使用 pip install --upgrade azure-cognitiveservices-speech 命令來更新至該版本。If a new version is available, you can update to it with the command pip install --upgrade azure-cognitiveservices-speech. 請檢查 azure.cognitiveservices.speech.__version__ 變數來確認目前所安裝的版本。Check which version is currently installed by inspecting the azure.cognitiveservices.speech.__version__ variable.

如果您有問題或缺少功能,請參閱支援和協助選項If you have a problem, or you're missing a feature, see Support and help options.

建立使用語音 SDK 的 Python 應用程式Create a Python application that uses the Speech SDK

執行範例Run the sample

您可以將本快速入門中的程式碼範例複製到原始程式檔 quickstart.py,並在您的 IDE 或主控台中執行它:You can copy the sample code from this quickstart to a source file quickstart.py and run it in your IDE or in the console:

python quickstart.py

或者,也可以從語音 SDK 範例存放庫 (英文) 將本快速入門教學課程下載為 Jupyter (英文) 筆記本,並以筆記本的形式執行它。Or you can download this quickstart tutorial as a Jupyter notebook from the Speech SDK sample repository and run it as a notebook.

範例程式碼Sample code

import azure.cognitiveservices.speech as speechsdk

# Creates an instance of a speech config with specified subscription key and service region.
# Replace with your own subscription key and service region (e.g., "westus").
speech_key, service_region = "YourSubscriptionKey", "YourServiceRegion"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

# Creates a recognizer with the given settings
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

print("Say something...")


# Starts speech recognition, and returns after a single utterance is recognized. The end of a
# single utterance is determined by listening for silence at the end or until a maximum of 15
# seconds of audio is processed.  The task returns the recognition text as result. 
# Note: Since recognize_once() returns only a single utterance, it is suitable only for single
# shot recognition like command or query. 
# For long-running multi-utterance recognition, use start_continuous_recognition() instead.
result = speech_recognizer.recognize_once()

# Checks result.
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
    print("Recognized: {}".format(result.text))
elif result.reason == speechsdk.ResultReason.NoMatch:
    print("No speech could be recognized: {}".format(result.no_match_details))
elif result.reason == speechsdk.ResultReason.Canceled:
    cancellation_details = result.cancellation_details
    print("Speech Recognition canceled: {}".format(cancellation_details.reason))
    if cancellation_details.reason == speechsdk.CancellationReason.Error:
        print("Error details: {}".format(cancellation_details.error_details))

透過 Visual Studio Code 安裝及使用語音 SDKInstall and use the Speech SDK with Visual Studio Code

  1. 在您的電腦上下載並安裝 64 位元版本的 Python (英文) (3.5 或更新版本)。Download and install a 64-bit version of Python, 3.5 or later, on your computer.

  2. 下載並安裝 Visual Studio CodeDownload and install Visual Studio Code.

  3. 開啟 Visual Studio Code,然後安裝 Python 擴充功能。Open Visual Studio Code and install the Python extension. 從功能表選取 [檔案] > [喜好設定] > [擴充功能] 。Select File > Preferences > Extensions from the menu. 搜尋 PythonSearch for Python.

    安裝 Python 擴充功能

  4. 建立資料夾來儲存專案。Create a folder to store the project in. 例如,使用 Windows 檔案總管。An example is by using Windows Explorer.

  5. 在 Visual Studio Code 中選取 [檔案] 圖示。In Visual Studio Code, select the File icon. 然後開啟您所建立的資料夾。Then open the folder you created.

    開啟資料夾

  6. 選取 [新增檔案] 圖示,以建立新的 Python 來源檔案 speechsdk.pyCreate a new Python source file, speechsdk.py, by selecting the new file icon.

    建立檔案

  7. Python 程式碼複製並貼上到新建立的檔案中,然後儲存它。Copy, paste, and save the Python code to the newly created file.

  8. 插入您的語音服務訂用帳戶資訊。Insert your Speech Services subscription information.

  9. 如果已經選取,Python 解譯器會顯示在視窗底部狀態列的左側。If selected, a Python interpreter displays on the left side of the status bar at the bottom of the window. 否則,會顯示可用 Python 解譯器的清單。Otherwise, bring up a list of available Python interpreters. 開啟命令選擇區 (Ctrl+Shift+P),然後輸入 Python:選取解譯器Open the command palette (Ctrl+Shift+P) and enter Python: Select Interpreter. 選擇適當的解譯器。Choose an appropriate one.

  10. 您可以從 Visual Studio Code 內安裝語音 SDK Python 套件。You can install the Speech SDK Python package from within Visual Studio Code. 如果您選取的 Python 解譯器尚未安裝該套件,請予以安裝。Do that if it's not installed yet for the Python interpreter you selected. 若要安裝語音 SDK 套件,請開啟終端機。To install the Speech SDK package, open a terminal. 再次開啟命令選擇區 (Ctrl+Shift+P),然後輸入 Terminal:Create New Integrated Terminal 來開啟終端機。Bring up the command palette again (Ctrl+Shift+P) and enter Terminal: Create New Integrated Terminal. 在開啟的終端機中,輸入命令 python -m pip install azure-cognitiveservices-speech 或系統所適用的命令。In the terminal that opens, enter the command python -m pip install azure-cognitiveservices-speech or the appropriate command for your system.

  11. 若要執行範例程式碼,請以滑鼠右鍵按一下編輯器內的任何一個地方。To run the sample code, right-click somewhere inside the editor. 選取 [在終端機中執行 Python 檔案] 。Select Run Python File in Terminal. 出現提示時,請說出幾個字。Speak a few words when you're prompted. 不久之後便會顯示轉譯的文字。The transcribed text displays shortly afterward.

    執行範例

如果您在遵循這些指示時遇到問題,請參閱更加詳盡的 Visual Studio Code Python 教學課程 (英文)。If you have issues following these instructions, refer to the more extensive Visual Studio Code Python tutorial.

後續步驟Next steps