{"id":957,"date":"2018-08-04T13:47:29","date_gmt":"2018-08-04T11:47:29","guid":{"rendered":"http:\/\/pythonprogramming.altervista.org\/?p=957"},"modified":"2018-08-27T07:54:40","modified_gmt":"2018-08-27T05:54:40","slug":"python-and-speech-recognition","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/python-and-speech-recognition\/","title":{"rendered":"Python and speech recognition"},"content":{"rendered":"<h2>What did you say?<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-958\" src=\"http:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/Cattura.png\" alt=\"\" width=\"526\" height=\"292\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/Cattura.png 1049w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/Cattura-320x178.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/Cattura-768x427.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/Cattura-960x534.png 960w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n<p>As I often talked about text-to speech, why we do non talk about speech recognition? As also\u00a0<a href=\"https:\/\/realpython.com\/\">https:\/\/realpython.com\/<\/a> talked about this topic, I want to share a little code here to give a sample of its possible utilization.<\/p>\n<pre class=\"lang:default decode:true \">import time\r\nimport speech_recognition as sr\r\n\r\n\r\ndef elaborate_recording(recognizer, microphone):\r\n    if not isinstance(recognizer, sr.Recognizer):\r\n        raise TypeError(\"A problem with the recognizer\")\r\n    if not isinstance(microphone, sr.Microphone):\r\n        raise TypeError(\"A problem with the microphone\")\r\n    with microphone as source:\r\n        recognizer.adjust_for_ambient_noise(source)\r\n        audio = recognizer.listen(source)\r\n    elaboration = {\r\n        \"success\": True,\r\n        \"error\": None,\r\n        \"transcription\": None\r\n    }\r\n    try:\r\n        elaboration[\"transcription\"] = recognizer.recognize_google(audio)\r\n    except sr.RequestError:\r\n        elaboration[\"success\"] = False\r\n        elaboration[\"error\"] = \"API not available\"\r\n    except sr.UnknownValueError:\r\n        elaboration[\"error\"] = \"I did not understand\"\r\n\r\n    return elaboration\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    print(\"I start the recognizer and the mic.. start talking\")\r\n    recognizer = sr.Recognizer()\r\n    microphone = sr.Microphone()\r\n    print(\"Dici qualcosa adesso \/ Say something now ... pause 3 second ... continue talking\")\r\n    time.sleep(3)\r\n    print(\"NOW!\")\r\n    guess = elaborate_recording(recognizer, microphone)\r\n    print(guess[\"transcription\"])\r\n<\/pre>\n<p><iframe loading=\"lazy\" title=\"Speech recognition with Python\" width=\"747\" height=\"560\" src=\"https:\/\/www.youtube.com\/embed\/4aWwmwGxIcY?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"The computer understand what you say\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/python-and-speech-recognition\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":959,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[174],"tags":[223,4,222],"class_list":["post-957","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-speak","tag-google","tag-python","tag-speech-recognition"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=957"}],"version-history":[{"count":3,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/957\/revisions"}],"predecessor-version":[{"id":962,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/957\/revisions\/962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/959"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}