{"id":3679,"date":"2017-05-16T16:31:26","date_gmt":"2017-05-16T08:31:26","guid":{"rendered":"http:\/\/blog.hoyo.idv.tw\/?p=3679"},"modified":"2018-04-30T10:41:33","modified_gmt":"2018-04-30T02:41:33","slug":"%e4%bd%bf%e7%94%a8-google-speech-api-%e5%b0%87%e8%aa%9e%e9%9f%b3%e8%be%a8%e8%ad%98%e7%82%ba%e6%96%87%e5%ad%97","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=3679","title":{"rendered":"\u4f7f\u7528 Google Speech API \u5c07\u8a9e\u97f3\u6a94\u6848\u8fa8\u8b58\u70ba\u6587\u5b57"},"content":{"rendered":"<p>2018-4-30 \u9019\u500b\u73a9\u610f\u5931\u6548\u4e86\uff0c\u4e0d\u5728\u4e4e\u82b1\u9322\u7684\u8acb\u53c3\u8003\u00a0<a href=\"https:\/\/blog.hoyo.idv.tw\/?p=4544\" target=\"_blank\" rel=\"noopener\">Google Cloud Speech to Text API<\/a><\/p>\n<p>--<\/p>\n<p>\u4e00\u958b\u59cb\u662f\u9019\u500b<\/p>\n<ul>\n<li><a href=\"https:\/\/pypi.python.org\/pypi\/SpeechRecognition\/\" target=\"_blank\" rel=\"noopener\">SpeechRecognition 3.6.5<\/a><\/li>\n<\/ul>\n<p>\u53c3\u8003<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/Uberi\/speech_recognition\/blob\/master\/speech_recognition\/__init__.py\" target=\"_blank\" rel=\"noopener\">speech_recognition\/speech_recognition\/__init__.py<\/a><\/li>\n<li><a href=\"http:\/\/blog.laobubu.net\/archivers\/google-speech-api-pt2\" target=\"_blank\" rel=\"noopener\">Google \u8bed\u97f3\u8bc6\u522b API (Part 2)<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>\u8072\u97f3\u6e96\u5099<\/h2>\n<pre class=\"lang:sh decode:true\">ffmpeg -i \u539f\u59cb\u6587\u4ef6 -ar 16000 \u8f93\u51fa.flac<\/pre>\n<p>\u683c\u5f0f\u4e0d\u6b63\u78ba\u5c31\u5b8c\u5168\u8fa8\u8b58\u4e0d\u51fa\u4f86<\/p>\n<p>--<\/p>\n<h2>curl \u6307\u4ee4\u9001\u51fa\u8fa8\u8b58<\/h2>\n<pre class=\"lang:default decode:true\">curl -X POST --data-binary @1.flac --header 'Content-Type: audio\/x-flac; rate=16000;' 'https:\/\/www.google.com\/speech-api\/v2\/recognize?client=chromium&amp;output=json&amp;lang=zh-TW&amp;key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw'<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>\u54ea\u5929\u639b\u6389\u600e\u9ebc\u8fa6\uff1f<\/h2>\n<p>\u53bb python \u770b\u539f\u59cb\u78bc\uff0c\u76f4\u63a5\u5077\u65b0\u7684\u4f5c\u6cd5\u5c31\u597d\u4e86<\/p>\n<pre class=\"lang:python decode:true \">    def recognize_google(self, audio_data, key=None, language=\"en-US\", show_all=False):\r\n        \"\"\"\r\n        Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Google Speech Recognition API.\r\n        The Google Speech Recognition API key is specified by ``key``. If not specified, it uses a generic key that works out of the box. This should generally be used for personal or testing purposes only, as it **may be revoked by Google at any time**.\r\n        To obtain your own API key, simply following the steps on the `API Keys &lt;http:\/\/www.chromium.org\/developers\/how-tos\/api-keys&gt;`__ page at the Chromium Developers site. In the Google Developers Console, Google Speech Recognition is listed as \"Speech API\".\r\n        The recognition language is determined by ``language``, an RFC5646 language tag like ``\"en-US\"`` (US English) or ``\"fr-FR\"`` (International French), defaulting to US English. A list of supported language tags can be found in this `StackOverflow answer &lt;http:\/\/stackoverflow.com\/a\/14302134&gt;`__.\r\n        Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the raw API response as a JSON dictionary.\r\n        Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the key isn't valid, or if there is no internet connection.\r\n        \"\"\"\r\n        assert isinstance(audio_data, AudioData), \"``audio_data`` must be audio data\"\r\n        assert key is None or isinstance(key, str), \"``key`` must be ``None`` or a string\"\r\n        assert isinstance(language, str), \"``language`` must be a string\"\r\n\r\n        flac_data = audio_data.get_flac_data(\r\n            convert_rate=None if audio_data.sample_rate &gt;= 8000 else 8000,  # audio samples must be at least 8 kHz\r\n            convert_width=2  # audio samples must be 16-bit\r\n        )\r\n        if key is None: key = \"AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw\"\r\n        url = \"http:\/\/www.google.com\/speech-api\/v2\/recognize?{}\".format(urlencode({\r\n            \"client\": \"chromium\",\r\n            \"lang\": language,\r\n            \"key\": key,\r\n        }))\r\n        request = Request(url, data=flac_data, headers={\"Content-Type\": \"audio\/x-flac; rate={}\".format(audio_data.sample_rate)})\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"3679\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;3,329&nbsp;total views, &nbsp;5&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>2018-4-30 \u9019\u500b\u73a9\u610f\u5931...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"3679\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;3,329&nbsp;total views, &nbsp;5&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3679"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3679"}],"version-history":[{"count":8,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3679\/revisions"}],"predecessor-version":[{"id":4553,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3679\/revisions\/4553"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}