{"id":1047,"date":"2023-06-08T16:35:21","date_gmt":"2023-06-08T08:35:21","guid":{"rendered":"http:\/\/xinyixx.com\/?p=1047"},"modified":"2023-08-14T17:08:11","modified_gmt":"2023-08-14T09:08:11","slug":"python10","status":"publish","type":"post","link":"https:\/\/www.xinyixx.com\/index.php\/2023\/06\/08\/python10\/","title":{"rendered":"python\u7a0b\u5e8f10\uff1a\u5199\u4e00\u4e2a\u8bed\u97f3\u8bc6\u522b\u7684\u8f6f\u4ef6"},"content":{"rendered":"<p>\u8fd9\u91cc\u8981\u7528\u5230\u7684\u6a21\u5757\u662fSpeechRecognition\u3002<\/p>\n\n\n\n<p>SpeechRecognition\u662f\u4e00\u4e2aPython\u8bed\u97f3\u8bc6\u522b\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ece\u9ea6\u514b\u98ce\u6216\u97f3\u9891\u6587\u4ef6\u4e2d\u8bc6\u522b\u8bed\u97f3\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6587\u672c\u3002\u5b83\u652f\u6301\u591a\u79cd\u8bed\u97f3\u8bc6\u522bAPI\uff0c\u5305\u62ecGoogle Cloud Speech API\u3001Microsoft Bing Voice Recognition\u3001IBM Speech to Text\u7b49\u7b49\u3002SpeechRecognition\u6a21\u5757\u8fd8\u652f\u6301\u591a\u79cd\u97f3\u9891\u683c\u5f0f\uff0c\u5305\u62ecWAV\u3001AIFF\u3001FLAC\u7b49\u7b49\u3002\u4f7f\u7528SpeechRecognition\u6a21\u5757\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u8bed\u97f3\u8bc6\u522b\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import speech_recognition as sr\n\n# \u521b\u5efaRecognizer\u5bf9\u8c61\nr = sr.Recognizer()\n\n# \u6253\u5f00\u97f3\u9891\u6587\u4ef6\u5e76\u8bfb\u53d6\u97f3\u9891\nwith sr.AudioFile(\"audio.wav\") as source:\n    audio = r.record(source)\n\n# \u4f7f\u7528Google Cloud Speech API\u8bc6\u522b\u97f3\u9891\uff0c\u5e76\u8fd4\u56de\u6587\u672c\u7ed3\u679c\ntext = r.recognize_google_cloud(audio, credentials_json=open(\"google-cloud-credentials.json\").read())\n\n# \u6253\u5370\u6587\u672c\u7ed3\u679c\nprint(text)\n<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aRecognizer\u5bf9\u8c61\uff0c\u7136\u540e\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a\u201caudio.wav\u201d\u7684\u97f3\u9891\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Google Cloud Speech API\u5bf9\u5176\u8fdb\u884c\u8bed\u97f3\u8bc6\u522b\u3002\u6700\u540e\u5c06\u8bc6\u522b\u7ed3\u679c\u6253\u5370\u51fa\u6765\u3002\u4f7f\u7528SpeechRecognition\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8bed\u97f3\u8bc6\u522b\u529f\u80fd\uff0c\u5e76\u4e14\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u8bed\u97f3\u8bc6\u522bAPI\u548c\u97f3\u9891\u683c\u5f0f\u3002<\/p>\n\n\n\n<p>\u6309\u7167\u4e0a\u9762\u7684\u793a\u4f8b\u7a0b\u5e8f\uff0c\u5f00\u59cb\u5199\u81ea\u5df1\u7684\u8bed\u97f3\u8bc6\u522b\u8f6f\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import speech_recognition as sr\nimport tkinter as tk\n\nclass SpeechRecognizerGUI:\n    def __init__(self, master):\n        self.master = master\n        master.title(\"\u8bed\u97f3\u8bc6\u522b\u5668\")\n        \n        self.label = tk.Label(master, text=\"\u8bf7\u5f00\u59cb\u8bf4\u8bdd\")\n        self.label.pack()\n        \n        self.text = tk.Text(master, height=10, width=50)\n        self.text.pack()\n        \n        self.button = tk.Button(master, text=\"\u5f00\u59cb\u5f55\u97f3\", command=self.start_recording)\n        self.button.pack()\n    \n    def start_recording(self):\n        recognizer = sr.Recognizer()\n        with sr.Microphone() as source:\n            audio = recognizer.listen(source)\n            try:\n                text = recognizer.recognize_google(audio, language='zh-CN')\n                self.text.insert(tk.END, text)\n            except sr.UnknownValueError:\n                self.text.insert(tk.END, \"\u62b1\u6b49\uff0c\u65e0\u6cd5\u8bc6\u522b\u60a8\u7684\u8bed\u97f3\")\n            except sr.RequestError as e:\n                self.text.insert(tk.END, \"\u65e0\u6cd5\u8fde\u63a5\u5230Google Speech Recognition\u670d\u52a1\uff1b{0}\".format(e))\n\nroot = tk.Tk()\napp = SpeechRecognizerGUI(root)\nroot.mainloop()\n<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u521b\u5efa\u4e86\u4e00\u4e2a\u57fa\u672c\u7684GUI\u754c\u9762\uff0c\u5305\u62ec\u4e00\u4e2a\u6807\u7b7e\u3001\u4e00\u4e2a\u6587\u672c\u6846\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u7528\u6237\u70b9\u51fb\u201c\u5f00\u59cb\u5f55\u97f3\u201d\u6309\u94ae\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u4f7f\u7528\u9ea6\u514b\u98ce\u5f55\u5236\u7528\u6237\u7684\u8bed\u97f3\uff0c\u5e76\u4f7f\u7528Google\u8bed\u97f3\u8bc6\u522bAPI\u5c06\u5176\u8f6c\u6362\u4e3a\u6587\u672c\u3002\u8bc6\u522b\u7ed3\u679c\u5c06\u663e\u793a\u5728\u6587\u672c\u6846\u4e2d\u3002\u5982\u679c\u65e0\u6cd5\u8bc6\u522b\u7528\u6237\u7684\u8bed\u97f3\u6216\u65e0\u6cd5\u8fde\u63a5\u5230Google\u670d\u52a1\uff0c\u5219\u4f1a\u663e\u793a\u76f8\u5e94\u7684\u9519\u8bef\u6d88\u606f\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u6d4b\u8bd5\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"366\" height=\"229\" src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/06\/image-25.png\" alt class=\"wp-image-1048\" srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/image-25.png 366w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/image-25-300x188.png 300w\" sizes=\"(max-width: 366px) 100vw, 366px\"><\/noscript><img decoding=\"async\" width=\"366\" height=\"229\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20366%20229%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-1048 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20366%20229%22%3E%3C%2Fsvg%3E 366w\" sizes=\"(max-width: 366px) 100vw, 366px\" data-srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/image-25.png 366w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/image-25-300x188.png 300w\" data-src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/06\/image-25.png\"><\/figure>\n\n\n\n<p>\u9700\u8981\u67e5\u770b\u9ea6\u514b\u98ce\u6743\u9650\u548c\u53ef\u8bbf\u95ee\u8c37\u6b4c\u8bed\u97f3\u8bc6\u522b\uff0c\u7ee7\u7eed\u8fd0\u7528pyinstaller\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5927\u529f\u544a\u6210\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u91cc\u8981\u7528\u5230\u7684\u6a21\u5757\u662fSpeechRecognition\u3002 SpeechRecognition\u662f\u4e00\u4e2aPython\u8bed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14,10,7],"tags":[71,69,85,77],"class_list":["post-1047","post","type-post","status-publish","format-standard","hentry","category-teacher","category-coding","category-software","tag-python","tag-learning","tag-exe","tag-coding","entry"],"_links":{"self":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/1047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/comments?post=1047"}],"version-history":[{"count":0,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/1047\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/media?parent=1047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/categories?post=1047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/tags?post=1047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}