{"id":994490,"date":"2024-12-27T08:55:51","date_gmt":"2024-12-27T00:55:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994490.html"},"modified":"2024-12-27T08:55:54","modified_gmt":"2024-12-27T00:55:54","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%af%ad%e9%9f%b3%e8%af%86%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994490.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u8bed\u97f3\u8bc6\u522b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071442\/a38e9282-e12b-40cb-ba23-38d1f8af40fb.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u8bed\u97f3\u8bc6\u522b\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5b9e\u73b0\u8bed\u97f3\u8bc6\u522b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e00\u4e9b\u5f3a\u5927\u7684\u5e93\uff0c\u5982SpeechRecognition\u3001PyDub\u548cPyAudio\u3002<strong>\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u5f55\u5236\u97f3\u9891\u3001\u5904\u7406\u97f3\u9891\u6587\u4ef6\u3001\u8c03\u7528\u8bed\u97f3\u8bc6\u522bAPI\u3001\u89e3\u6790\u8bc6\u522b\u7ed3\u679c<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5173\u952e\u7684\u4e00\u6b65\u662f\u9009\u62e9\u5408\u9002\u7684\u8bed\u97f3\u8bc6\u522b\u5f15\u64ce\uff0cGoogle Web Speech API \u662f\u4e00\u4e2a\u5e38\u7528\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u6613\u4e8e\u4f7f\u7528\u4e14\u51c6\u786e\u7387\u9ad8\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u8bed\u97f3\u8bc6\u522b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u4e00\u4e9b\u5fc5\u8981\u7684\u5e93\u3002SpeechRecognition\u5e93\u662f\u4e00\u4e2a\u6d41\u884c\u7684Python\u5e93\uff0c\u7528\u4e8e\u6267\u884c\u8bed\u97f3\u8bc6\u522b\u4efb\u52a1\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install SpeechRecognition<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u5b89\u88c5PyAudio\u5e93\u6765\u5904\u7406\u97f3\u9891\u6d41\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyAudio<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u97f3\u9891\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5b89\u88c5PyDub\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pydub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5f55\u5236\u97f3\u9891<\/p>\n<\/p>\n<p><p>\u5728\u8bed\u97f3\u8bc6\u522b\u8fc7\u7a0b\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5f55\u5236\u97f3\u9891\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528PyAudio\u5e93\u6765\u5f55\u5236\u97f3\u9891\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5f55\u5236\u97f3\u9891\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyaudio<\/p>\n<p>import wave<\/p>\n<p>def record_audio(filename, duration=5):<\/p>\n<p>    chunk = 1024  # Record in chunks of 1024 samples<\/p>\n<p>    sample_format = pyaudio.p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nt16  # 16 bits per sample<\/p>\n<p>    channels = 2<\/p>\n<p>    fs = 44100  # Record at 44100 samples per second<\/p>\n<p>    p = pyaudio.PyAudio()  # Create an interface to PortAudio<\/p>\n<p>    print(&#39;Recording&#39;)<\/p>\n<p>    stream = p.open(format=sample_format,<\/p>\n<p>                    channels=channels,<\/p>\n<p>                    rate=fs,<\/p>\n<p>                    frames_per_buffer=chunk,<\/p>\n<p>                    input=True)<\/p>\n<p>    frames = []  # Initialize array to store frames<\/p>\n<p>    # Store data in chunks for the specified duration<\/p>\n<p>    for _ in range(0, int(fs \/ chunk * duration)):<\/p>\n<p>        data = stream.read(chunk)<\/p>\n<p>        frames.append(data)<\/p>\n<p>    # Stop and close the stream<\/p>\n<p>    stream.stop_stream()<\/p>\n<p>    stream.close()<\/p>\n<p>    # Terminate the PortAudio interface<\/p>\n<p>    p.terminate()<\/p>\n<p>    print(&#39;Finished recording&#39;)<\/p>\n<p>    # Save the recorded data as a WAV file<\/p>\n<p>    wf = wave.open(filename, &#39;wb&#39;)<\/p>\n<p>    wf.setnchannels(channels)<\/p>\n<p>    wf.setsampwidth(p.get_sample_size(sample_format))<\/p>\n<p>    wf.setframerate(fs)<\/p>\n<p>    wf.writeframes(b&#39;&#39;.join(frames))<\/p>\n<p>    wf.close()<\/p>\n<p>record_audio(&#39;output.wav&#39;, duration=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5f55\u52365\u79d2\u7684\u97f3\u9891\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3aWAV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u97f3\u9891\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u5f55\u5236\u6216\u83b7\u53d6\u97f3\u9891\u6587\u4ef6\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u97f3\u9891\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u5176\u683c\u5f0f\u548c\u8d28\u91cf\u9002\u5408\u8bed\u97f3\u8bc6\u522b\u4efb\u52a1\u3002\u4f7f\u7528PyDub\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8f6c\u6362\u97f3\u9891\u683c\u5f0f\u3001\u8c03\u6574\u97f3\u91cf\u6216\u526a\u8f91\u97f3\u9891\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pydub import AudioSegment<\/p>\n<p>def convert_audio(input_file, output_file):<\/p>\n<p>    audio = AudioSegment.from_file(input_file)<\/p>\n<p>    audio.export(output_file, format=&quot;wav&quot;)<\/p>\n<p>convert_audio(&#39;input.mp3&#39;, &#39;output.wav&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5c06MP3\u6587\u4ef6\u8f6c\u6362\u4e3aWAV\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8c03\u7528\u8bed\u97f3\u8bc6\u522bAPI<\/p>\n<\/p>\n<p><p>\u4f7f\u7528SpeechRecognition\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u8c03\u7528\u4e0d\u540c\u7684\u8bed\u97f3\u8bc6\u522bAPI\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Google Web Speech API\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import speech_recognition as sr<\/p>\n<p>def recognize_speech_from_file(file_path):<\/p>\n<p>    recognizer = sr.Recognizer()<\/p>\n<p>    with sr.AudioFile(file_path) as source:<\/p>\n<p>        audio = recognizer.record(source)  # Read the entire audio file<\/p>\n<p>    try:<\/p>\n<p>        # Use the Google Web Speech API<\/p>\n<p>        text = recognizer.recognize_google(audio)<\/p>\n<p>        print(f&quot;Recognized text: {text}&quot;)<\/p>\n<p>    except sr.UnknownValueError:<\/p>\n<p>        print(&quot;Google Speech Recognition could not understand the audio&quot;)<\/p>\n<p>    except sr.RequestError as e:<\/p>\n<p>        print(f&quot;Could not request results from Google Speech Recognition service; {e}&quot;)<\/p>\n<p>recognize_speech_from_file(&#39;output.wav&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>recognize_google<\/code>\u65b9\u6cd5\u5c06\u97f3\u9891\u6587\u4ef6\u4e2d\u7684\u8bed\u97f3\u8f6c\u6362\u4e3a\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u89e3\u6790\u8bc6\u522b\u7ed3\u679c<\/p>\n<\/p>\n<p><p>\u8bed\u97f3\u8bc6\u522b\u7684\u6700\u7ec8\u6b65\u9aa4\u662f\u89e3\u6790\u8bc6\u522b\u7ed3\u679c\u3002\u901a\u5e38\uff0c\u8bc6\u522b\u5f15\u64ce\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6587\u672c\u5b57\u7b26\u4e32\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5e94\u7528\u7684\u9700\u6c42\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u8bc6\u522b\u7684\u6587\u672c\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u6216\u7528\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u89e3\u6790\u548c\u5904\u7406\u8bc6\u522b\u7ed3\u679c\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_recognized_text(text):<\/p>\n<p>    # \u7b80\u5355\u5730\u5c06\u8bc6\u522b\u6587\u672c\u8f93\u51fa<\/p>\n<p>    print(&quot;Processed text:&quot;, text)<\/p>\n<h2><strong>\u5728\u8bc6\u522b\u5b8c\u6210\u540e\u8c03\u7528<\/strong><\/h2>\n<p>recognized_text = recognize_speech_from_file(&#39;output.wav&#39;)<\/p>\n<p>process_recognized_text(recognized_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8bc6\u522b\u6587\u672c\u88ab\u7b80\u5355\u5730\u6253\u5370\u8f93\u51fa\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u4f1a\u5c06\u5176\u7528\u4e8e\u66f4\u590d\u6742\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u8bed\u97f3\u8bc6\u522b\u7cfb\u7edf\u3002\u6839\u636e\u5177\u4f53\u7684\u9879\u76ee\u9700\u6c42\uff0c\u60a8\u8fd8\u53ef\u4ee5\u63a2\u7d22\u5176\u4ed6\u8bed\u97f3\u8bc6\u522b\u5f15\u64ce\u6216\u5e93\uff0c\u5982Microsoft Azure Speech\u3001IBM Watson Speech to Text\u6216Mozilla DeepSpeech\uff0c\u4ee5\u83b7\u5f97\u66f4\u9ad8\u7684\u51c6\u786e\u7387\u6216\u7279\u5b9a\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u8bed\u97f3\u8bc6\u522b\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5b9e\u73b0\u8bed\u97f3\u8bc6\u522b\u7684\u57fa\u672c\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3001\u8bbe\u7f6e\u97f3\u9891\u8f93\u5165\u6e90\u3001\u4f7f\u7528\u8bed\u97f3\u8bc6\u522b\u6a21\u5757\u8fdb\u884c\u5904\u7406\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecSpeechRecognition\u548cpyaudio\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u7136\u540e\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\u6765\u6355\u6349\u97f3\u9891\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6587\u672c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u8bed\u97f3\u8bc6\u522b\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5e93\u548c\u5de5\u5177\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u51e0\u4e2a\u6d41\u884c\u7684\u5e93\u53ef\u4ee5\u7528\u4e8e\u8bed\u97f3\u8bc6\u522b\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fSpeechRecognition\uff0c\u5b83\u652f\u6301\u591a\u79cd\u8bc6\u522b\u5f15\u64ce\u548cAPI\u3002\u6b64\u5916\uff0cpyaudio\u5e93\u7528\u4e8e\u6355\u83b7\u97f3\u9891\u6d41\uff0cGoogle\u7684Web Speech API\u4e5f\u662f\u4e00\u4e2a\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u3002\u5176\u4ed6\u9009\u62e9\u5305\u62ecCMU Sphinx\u548cMicrosoft Azure\u7684\u8bed\u97f3\u670d\u52a1\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8Python\u8bed\u97f3\u8bc6\u522b\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u63d0\u9ad8\u8bed\u97f3\u8bc6\u522b\u51c6\u786e\u6027\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u9ad8\u8d28\u91cf\u7684\u9ea6\u514b\u98ce\uff0c\u786e\u4fdd\u73af\u5883\u5b89\u9759\uff0c\u8c03\u6574\u8bed\u97f3\u8bc6\u522b\u6a21\u578b\u7684\u53c2\u6570\uff0c\u4ee5\u53ca\u5728\u9700\u8981\u65f6\u4f7f\u7528\u7279\u5b9a\u9886\u57df\u7684\u8bed\u8a00\u6a21\u578b\u3002\u6b64\u5916\uff0c\u8fdb\u884c\u9002\u5f53\u7684\u97f3\u9891\u9884\u5904\u7406\uff08\u5982\u53bb\u566a\uff09\u4e5f\u80fd\u663e\u8457\u63d0\u5347\u8bc6\u522b\u6548\u679c\u3002\u5b9a\u671f\u66f4\u65b0\u6a21\u578b\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u8bcd\u6c47\u8868\u4e5f\u662f\u6709\u52a9\u4e8e\u63d0\u9ad8\u51c6\u786e\u6027\u7684\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u8bed\u97f3\u8bc6\u522b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e00\u4e9b\u5f3a\u5927\u7684\u5e93\uff0c\u5982SpeechRecognition\u3001PyDub\u548cPy [&hellip;]","protected":false},"author":3,"featured_media":994497,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994490"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=994490"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994490\/revisions"}],"predecessor-version":[{"id":994500,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994490\/revisions\/994500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994497"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}