Logo
一発話認識

Python SDK

音声対話サービスのためのPython SDKです。 対応しているサービス 一発話認識、リアルタイム音声認識。

1.1 SDKファイルの説明

ファイル/ディレクトリ説明
speech_recSDK関連ファイル
demoコード例
 ├─ recognizer_demo.py一発話認識例コード
 ├─ demo.wav中国語(北京語)サンプル音声(WAV形式)
 ├─ demo.mp3中国語(北京語)サンプル音声(MP3形式)
setup.pyインストールファイル
README-JA.md日本語説明文書
README-EN.md英語説明文書

注意:SDKで提供されるテスト音声の認識結果は一定です。デフォルトで使用される音声はMP3形式です。入力された音声がWAVやその他の形式の場合、MP3形式に変換されます。

2 動作環境

Python3.4以降、ffmpeg。

Pythonの実行環境を別に作成することをお勧めします。そうしないとバージョンの衝突が発生する可能性があります。

3 設置方法

1.Pythonパッケージ管理ツールsetuptoolsがインストールされていることを確認する。インストールされていない場合は、インストールしてください:

$ pip install setuptools

2.SDKを解凍し、フォルダ(setup.pyファイルがあるフォルダ)に移動し、SDKディレクトリで以下のコマンドを実行します:

$ python setup.py install
  • The above pip and python commands correspond to Python3.
  • If the following information is displayed, the installation is successful: Finished processing dependencies for speech-python-rec-sdk==1.0.0.8
  • After installation, the build, dist, and speech_python_rec_sdk.egg-info files are generated.

3.demoのファイルの具体的なパラメーターを修正する:

recognizer_demo.py and transcriber_demo.py are execution files for one-sentence recognition and real-time speech recognition, respectively.

//Enter the appID that you get when you purchase a service in the platform
app_id = '#####'
  
//Enter appSecret, which you get when you purchase a service in the platform
app_secret = '#####'
  
//Enter the path of the voice file to be identified. Change it to the path of the customized audio file
audio_path = '####'
  
//Input language, format see platform documentation center-Speech recognition-Development Guide
lang_type = 'ja-JP'

4.recognizer_demo.pyまたはtranscriber_demo.pyを実行して音声を認識します。トークンの認識に失敗したり、有効期限が切れたりした場合は、ローカルのSpeechRecognizer_token.txtファイルまたはSpeechTranscriber_token.txtファイルを削除して再度お試しください。それでも期限切れの場合は、技術スタッフまでご連絡ください。

To run the command in the demo directory, set parameters such as app_id corresponding to python files in the demo.

$ python recognizer_demo.py 
$ python transcriber_demo.py 

After the successful run, the SpeechRecognizer_token.txt or SpeechTranscriber_token.txt files are generated in the path where the demo is running.

注意: 「timestamp timeout 」または 「timestamp is greater than current time 」と表示された場合、ローカル時間とサーバー時間が一致していません。エラーメッセージの時差に応じて、

speech-python-recファイルの_token.pyファイルを修正し、timestamp = int(t)の行コードを適切に修正します。

timestamp = int(t)+1または2,3,4など、またはtimestamp = int(t)-1または2,3,4など。

  • After _token.py is modified, the modification takes effect only after it is created again. The specific steps are as follows:

Delete build, dist, and speech_python_rec_sdk.egg-info files created and generated in the SDK directory.

To uninstall and reinstall the SDK, run $pip uninstall speech-python-rec-sdk and repeat steps 2,3,4.

4 パラメータの説明

4.1 一発話認識デモ使用

speech_rec/demo/recognizer_demo.py デモを識別するための文章は、直接実行してください。

4.1.1 キー・インターフェースの説明

一発話認識SDKは主に Recognizer クラスを使うことで完結し、認可SDKは Token クラスを使うことで完結する。

  • SpeechClientクラスの get_token() メソッドを呼び出してトークンを取得する。
  • SpeechRecognizer.のインスタンスを作成する。
  • Callbackインスタンスを作成する。
  • パラメータを設定するには、SpeechRecognizer インスタンスの set_token() メソッドを呼び出します。
  • SpeechRecognizerインスタンスの start() メソッドを呼び出して、サーバーに接続する。
  • 音声を送信するために SpeechRecognizer インスタンスの send() メソッドを呼び出します。
  • SpeechRecognizerインスタンスの stop() メソッドを呼び出して、送信を停止する。
  • SpeechRecognizerインスタンスの close() メソッドを呼び出して、サーバーとの接続を切断する。

4.1.2 パラメータの説明

パラメータタイプ必須説明デフォルト値
lang_typeStringはい言語オプション必須
formatStringいいえ音声エンコード形式pcm
sample_rateIntegerいいえ音声サンプルレート
sample_rate=「8000」の場合
fieldパラメータ・フィールドが必須で、かつfield=「call-center」
16000
enable_punctuation_predictionBooleanいいえ後処理で句読点を追加するかどうかtrue
enable_inverse_text_normalizationBooleanいいえ後処理でITNを実行するかどうかtrue
enable_modal_particle_filterBooleanいいえ語気詞フィルタを有効にするかどうかtrue
max_sentence_silenceIntegerいいえ音声センテンスのサイレンス検出しきい値、サイレンス時間がこのしきい値を超えるとセンテンスが切り替えられます。有効なパラメータ範囲は200~1200で、単位はミリ秒ですsample_rate=16000:800
sample_rate=8000:250
enable_wordsBooleanいいえ単語情報を返すかどうかfalse
hotwords_listStringいいえ一度限り有効な単語リスト、本接続中のみ有効です
hotwords_id と同時に存在する場合は、hotwords_list が優先されます。一度に最大 100 個まで提供可能です
無し
hotwords_idStringいいえホットワードID無し
hotwords_weightFloatいいえホットワードの重み、値の範囲[0.1, 1.0]0.4
correction_words_idStringいいえ強制置換単語庫ID
複数の強制置換単語庫IDを使用することができ、各IDは縦線|で区切られます;all は全ての強制置換単語庫IDを使用することを意味します。
無し
forbidden_words_idStringいいえNG単語ID
複数のNG単語IDを使用することができ、各IDは縦線|で区切られます;all は全てのNG単語IDを使用することを意味します。
無し
fieldStringいいえ分野
一般:general (サポートサンプルレート16000Hz)
コールセンター:call-center (サポートサンプルレート8000Hz)
無し
audio_urlStringいいえ戻り音声のフォーマット(プラットフォームでは30日間のみ保存)
mp3:mp3形式の音声リンクを返す
pcm:pcm形式の音声リンクを返す
wav:wav形式の音声リンクを返す
無し
gainIntegerいいえ振幅増幅係数を表し
1は拡大しないことを示し、2は元の振幅の2倍(拡大1倍)を示し、以下同様
sample_rate=16000:1
sample_rate=8000:2
enable_save_logBooleanいいえ音声データと認識結果のログを提供して、弊社が製品とサービスの質を向上させるために使用することは可能ですかtrue

4.1.3 一発話認識サンプルコード

完全なコードはSDKのspeech_python_rec/demo/recognizer_demo.pyファイルを参照してください。

# -*- coding: utf-8 -*-
import os
import time
import threading
import speech_rec
from speech_rec.callbacks import SpeechRecognizerCallback
from speech_rec.parameters import DefaultParameters

token = None
expire_time = 7  # Expiration time


class Callback(SpeechRecognizerCallback):
    """
    The parameters of the constructor are not required. You can add them as needed
    The name parameter in the example can be used as the audio file name to be recognized for distinguishing in multithreading
    """

    def __init__(self, name='SpeechRecognizer'):
        self._name = name

    def started(self, message):
        print('MyCallback.OnRecognitionStarted: %s' % message)

    def result_changed(self, message):
        print('MyCallback.OnRecognitionResultChanged: file: %s, task_id: %s, payload: %s' % (
            self._name, message['header']['task_id'], message['payload']))

    def completed(self, message):
        print('MyCallback.OnRecognitionCompleted: file: %s, task_id:%s, payload:%s' % (
            self._name, message['header']['task_id'], message['payload']))

    def task_failed(self, message):
        print(message)

    def warning_info(self, message):
        print(message)

    def channel_closed(self):
        print('MyCallback.OnRecognitionChannelClosed')

def solution(client, app_id, app_secret, audio_path, lang_type, kwargs):
    """
    Recognize speech,single thread
    :param client: SpeechClient
    :param app_id: Your app_id
    :param app_secret: Your app_secret
    :param audio_path: Audio path
    :param lang_type: Language type
    """
    assert os.path.exists(audio_path), "Audio file path error, please check your audio path."
    sample_rate = kwargs.get("sample_rate", DefaultParameters.SAMPLE_RATE_16K)
    each_audio_format = kwargs.get("audio_format", DefaultParameters.MP3)
    field_ = kwargs.get("field", DefaultParameters.FIELD)

    if judging_expire_time(app_id, app_secret, expire_time):
        callback = Callback(audio_path)
        recognizer = client.create_recognizer(callback)
        recognizer.set_app_id(app_id)
        recognizer.set_token(token)
        # fixme You can customize the configuration according to the official website documentation
        payload = {
            "lang_type": lang_type,
            "format": each_audio_format,
            "field": field_,
            "sample_rate": sample_rate,
        }
        recognizer._payload.update(**payload)
        try:
            ret = recognizer.start()
            if ret < 0:
                return ret
            print('sending audio...')
            cnt = 0
            with open(audio_path, 'rb') as f:
                audio = f.read(7680)
                while audio:
                    cnt += 0.24
                    ret = recognizer.send(audio)
                    if ret < 0:
                        break
                    time.sleep(0.24)
                    audio = f.read(7680)
            recognizer.stop()
        except Exception as ee:
            print(f"send ee:{ee}")
        finally:
            recognizer.close()
    else:
        print("token expired")


def judging_expire_time(app_id, app_secret, extime):
    global token
    new_time = time.time()
    token_file = "SpeechRecognizer_token.txt"
    if not os.path.exists(token_file):
        client.get_token(app_id, app_secret, token_file)
    with open(token_file, "r", encoding="utf-8") as fr:
        token_info = eval(fr.read())
    old_time = token_info['time']
    token = token_info['token']
    flag = True
    if new_time - old_time > 60 * 60 * 24 * (extime - 1):
        flag, _ = client.get_token(app_id, app_secret, token_file)
        if flag:
            flag = True
            pass
        else:
            for i in range(7):
                flag, _ = client.get_token(app_id, app_secret, token_file)
                if flag is not None:
                    flag = True
                    break
    return flag


def channels_split_solution(audio_path, right_path, left_path, **kwargs):
    client = kwargs.get('client')
    appid = kwargs.get('app_id')
    appsecret = kwargs.get('app_secret')
    langtype = kwargs.get('lang_type')
    remove_audio = kwargs.get('rm_audio', True)
    client.auto_split_audio(audio_path, right_path, left_path)
    thread_list = []
    thread_r = threading.Thread(target=solution, args=(client, appid, appsecret, right_path, langtype, kwargs))
    thread_list.append(thread_r)
    thread_l = threading.Thread(target=solution, args=(client, appid, appsecret, left_path, langtype, kwargs))
    thread_list.append(thread_l)
    for thread in thread_list:
        thread.start()
    for thread in thread_list:
        thread.join()
    if remove_audio:
        os.remove(right_path)
        os.remove(left_path)
    pass


if __name__ == "__main__":
    client = speech_rec.SpeechClient()
    # Set the level of output log information:DEBUG、INFO、WARNING、ERROR
    client.set_log_level('INFO')
    # Type your app_id and app_secret
    app_id = ""  # your app id
    app_secret = ""  # your app secret
    audio_path = ""  # audio path
    lang_type = ""  # lang type
    field = ""  # field
    sample_rate = 16000  # sample rate [int] 16000 or 8000
    audio_format = ""  # audio format
    assert app_id and app_secret and audio_path and lang_type and field and sample_rate and audio_format, "Please check args"
    channel = client.get_audio_info(audio_path)['channel']
    # fixme This is just a simple example, please modify it according to your needs.
    multi = False
    process_num = 4
    if channel == 1:
        kwargs = {
            "field": field,
            "sample_rate": sample_rate,
            "audio_format":audio_format
        }
        solution(client, app_id, app_secret, audio_path, lang_type, kwargs)

    elif channel == 2:
        # Dual channel 8K audio solution
        channels_split_solution(audio_path=audio_path,
                                left_path=f"left.{audio_format}",
                                right_path=f"right.{audio_format}",
                                client=client,
                                app_id=app_id,
                                app_secret=app_secret,
                                lang_type=lang_type,
                                field=field,
                                sample_rate=sample_rate,
                                audio_format=audio_format)

5 SDKダウンロード

Python SDK