LoginSignup
2
2

More than 3 years have passed since last update.

Speech Recognition API でブラウザ音声認識を試す

Last updated at Posted at 2020-04-17

はじめに

WebVRの研究中に音声認識をやりたくなって調べてみたら面白そうだったので試してみた

SpeechRecognition

概要

  • HTML5に備わってるAPI (Mozilla作)
  • 音ブラウザで音声を認識してくれる

準備

なし

使用方法

公式ドキュメント

  • new SpeechRecognition() した後に設定値を入れて、
    (※ chromeでは "webkit" プレフィックスを付けないといけないのでwebkitSpeechRecognition()になる)

  • start() で認識開始

  • onresult()で音声取得結果がわかる

  • stop() で終了

音声認識を動かしてみる

仕様

  1. startボタンを押下 → 音声認識開始
  2. 話す → ログに音声認識結果を出す
  3. stopボタン押下 → 音声認識終了

ソースコード

speakTest.html
<!DOCTYPE html>
<html>

<head lang="ja-JP">
    <meta charset="utf-8">
    <title>Speak Test</title>
</head>

<body>

    <button onclick="startbutton()">start</button>
    <button onclick="stopbutton()">stop</button>

    <script>

        const SpeechRecognition = webkitSpeechRecognition || SpeechRecognition;
        const rec = new SpeechRecognition();
        rec.continuous = true;
        rec.interimResults = false;
        rec.lang = "ja-JP" // "en-US"なら英語で認識してくれる。指定がないとブラウザの言語になる

        rec.onresult = function (e) {
            console.log("on result")
            for (var i = e.resultIndex; i < e.results.length; ++i) {
                if (e.results[i].isFinal) {
                    const str = e.results[i][0].transcript;
                    console.log('Your speaking: ' + str);
                }
            }
        }

        function startbutton() {
            console.log("start")
            rec.start();
        }
        function stopbutton() {
            console.log("stop")
            rec.stop();
        }
    </script>

</body>

</html>

結果

うまくいきました
3.PNG

2
2
1

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
2