LoginSignup
2
1

More than 3 years have passed since last update.

Amazon Connectで保存された音声データをIBM SpeechToTextで文字起こしする

Last updated at Posted at 2019-05-10

やりたいこと

顧客とエージェントが通話した音声データを文字起こしする。

AmazonConnect → S3 → 文字起こし(Lambda) → S3

前準備

やり方

src/sample.ts
import {Handler} from "aws-lambda";
import {S3} from "aws-sdk"

export const handler: Handler = async (event: any) => {
    const s3 = new S3Service(new S3());

    // 一時作業ディレクトリを使用する
    let fs = require('fs');
    const wavPath = '/tmp/' + (new Date()).getTime() + '.wav';

    for (let record of event.Records) {
        // AmazonConnectのデフォルト設定だとURLエンコードされているため、URLデコードする
        let key = decodeURIComponent(record.s3.object.key);
        const bucketName = record.s3.bucket.name;

        const object = await s3.get(bucketName, key);
        // 一時作業ディレクトリに.WAVファイルを保存する
        fs.writeFile(wavPath, object.Body, 'binary', function (e) {
            if (e) {
                console.error(e);
                return;
            }
        });

        // SpeechToTextの設定
        const SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
        const speechToText = new SpeechToTextV1({
            iam_apikey: 'XXXXXXXXXXXXXXX', // TODO IBM_APIKEYを置き換える
            url: 'https://gateway-tok.watsonplatform.net/speech-to-text/api'
        });
        let recognizeParams = {
            audio: fs.createReadStream(wavPath),
            model: 'ja-JP_NarrowbandModel',
            content_type: 'audio/wav'
        };

        // SpeechToTextを呼び出して文字起こししたデータをS3に保存する
        const s2tJson = await speechToText.recognize(recognizeParams);
        key = key.replace('.wav', '');
        await s3.put(bucketName, key + '_s2t.json', JSON.stringify(s2tJson));
    }
    return {};
};

class S3Service {
    constructor(private s3: S3) {
    }

    async get(bucketName, key) {
        const params = {
            Bucket: bucketName,
            Key: key
        };
        return await this.s3.getObject(params).promise();
    }

    async put(bucketName, key, body) {
        const params = {
            Bucket: bucketName,
            Key: key,
            Body: body,
        };
        return await this.s3.putObject(params).promise();
    }
}

serverless.ymlは一部抜粋
bucket名をAmazonConnectで音声データが保存されるbucket名に変更すること
S3をトリガーとするため、sls deployの後、sls s3deployを行うこと

serverless.yml
plugins:
  - serverless-plugin-existing-s3

functions:
  speechToText:
    handler: src/sample.handler
    events:
      - existingS3:
          bucket: connect-XXXXXXXXXXXXXX
          events:
            - s3:ObjectCreated:*
          rules:
            - suffix: .wav

おまけ

Amazon Kinesis Video Streamsを使用すれば問い合わせフロー内の顧客音声も.WAVファイルに出来るので、組み合わせると文字起こし出来る範囲が広がります。

(2019/11/25)Amazon Transcribeが日本語対応された為、AWS内で文字起こしを完結出来そうですね。

2
1
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
2
1