1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Bing Speech Recognition APIをClojureから使ってみる

Posted at

サブスクリプションキーの発行は以下を参考にしました。

音声のサンプルデータは以下から。

(ns bing-speech.core
  (:gen-class)
  (:require [clj-http.client :as http]
            [clojure.data.json :as json]))

(def issue-token-endpoint
  "アクセストークン発行用のエンドポイント"
  "https://api.cognitive.microsoft.com/sts/v1.0/issueToken")

(def recognize-endpoint
  "音声認識用のエンドポイント"
  "https://speech.platform.bing.com/recognize")

(def subscription-key
  "Bing Speechのサブスクリプションキー"
    "abcdefghijklmnopqrstuvwxyz123456")

(defn issue-token [key]
  "アクセストークンを取得する"
  (let [res (http/post issue-token-endpoint
                       {:headers {"Content-Type" "application/x-www-form-urlencoded"
                                  "Ocp-Apim-Subscription-Key" key}})]
    (res :body)))

(defn uuid []
  "UUIDを生成する"
  (str (java.util.UUID/randomUUID)))

(defn recognize [token path]
  "pathにあるWAVファイルを音声認識する"
  (let [scenarios "scenarios=smd"
        appid "appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5"
        locale "locale=ja-JP"
        os "device.os=MacOS"
        version "version=3.0"
        fmt "format=json"
        instanceid (format "instanceid=%s" (uuid))
        requestid (format "requestid=%s" (uuid))
        url (format "%s?%s&%s&%s&%s&%s&%s&%s&%s" recognize-endpoint scenarios appid locale os version fmt
                    instanceid requestid)
        authorization (format "Bearer %s" token)
        content-type "audio/wav; codec=\"audio/pcm\"; samplerate=44100"]
    (http/post url {:headers {"Authorization" authorization "Content-Type" content-type}
                    :body (clojure.java.io/input-stream path)})))

(let [token (issue-token subscription-key)
      result (recognize token "aiueo1.wav")]
  ((((json/read-str (result :body)) "results") 0) "name"))
;=> "あいうえお"

この分野は全然知らないので認識率についてはなんとも言えませんが、こんなお手軽にできるのは凄いですね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?