{"id":208015,"date":"2025-07-08T10:53:28","date_gmt":"2025-07-08T02:53:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208015\/"},"modified":"2025-07-08T10:53:28","modified_gmt":"2025-07-08T02:53:28","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e8%bf%99%e4%b8%aa-google-%e8%af%ad%e9%9f%b3%e4%bb%a3%e7%a0%81%e8%bf%94%e5%9b%9e%e4%b8%80%e4%b8%aa%e7%a9%ba%e5%af%b9%e8%b1%a1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208015\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u8fd9\u4e2a Google \u8bed\u97f3\u4ee3\u7801\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4e3a\u4ec0\u4e48\u8fd9\u4e2a Google \u8bed\u97f3\u4ee3\u7801\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 22:06:20<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u4e3a\u4ec0\u4e48\u8fd9\u4e2a Google \u8bed\u97f3\u4ee3\u7801\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u6d4b\u8bd5\u8bed\u97f3\u8bc6\u522b\u3002\u6211\u68c0\u67e5\u4e86\u8c37\u6b4c\u793a\u4f8b\u4ee3\u7801\u7f51\u7ad9\u3002\u6211\u5df2\u7ecf\u5c1d\u8bd5\u4e86\u51e0\u79cd\u4e0d\u540c\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u4f46\u8fd8\u6ca1\u6709\u627e\u5230\u4e00\u4e2a\u53ef\u884c\u7684\u3002\u8fd9\u662f\u6700\u65b0\u7684\u4e00\u5f20\u3002\u5b83\u4e0d\u8fd4\u56de\u9519\u8bef\uff0c\u53ea\u662f\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684\u54cd\u5e94\u5bf9\u8c61\u3002\u6211\u5c1d\u8bd5\u8fc7\u4e0d\u540c\u7248\u672c\u7684\u8bed\u97f3\u8bc6\u522b\uff0c\u4f46\u8c37\u6b4c\u7684\u6837\u672c\u5747\u4e0d\u9002\u7528\u4e8e\u8fd9\u4e9b\u7248\u672c\u3002\u4e0b\u9762\u662f\u6211\u5728\u7f51\u4e0a\u627e\u5230\u7684\u6700\u7b80\u5355\u7684\u6d4b\u8bd5\u4ee3\u7801\u3002\u8c37\u6b4c\u793a\u4f8b\u7ad9\u70b9\u6ca1\u6709\u6307\u5b9a\u4f7f\u7528\u54ea\u79cd\u97f3\u9891\u6587\u4ef6\uff0c\u56e0\u6b64\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u95ee\u9898\u3002\u4f46 .wav \u6587\u4ef6\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6807\u5934\uff0c\u6307\u5b9a\u7f16\u7801\u3001\u91c7\u6837\u7387\u7b49\u3002\u6211\u66fe\u4f7f\u7528 .wav \u6587\u4ef6\u6d4b\u8bd5\u5176\u4ed6\u8bed\u8a00\uff08\u4f8b\u5982 python\uff09\u7684\u8bed\u97f3\u8bc6\u522b\uff0c\u5b83\u4ece\u672a\u5f15\u8d77\u95ee\u9898\u3002\u6211\u5c1d\u8bd5\u7701\u7565\u53ef\u9009\u7684 encoding \u548c sampleratehertz \u5b57\u6bb5\uff0c\u4f46\u50cf\u5f80\u5e38\u4e00\u6837\u8fd4\u56de\u4e86\u76f8\u540c\u7684\u7a7a\u54cd\u5e94\u5bf9\u8c61\u3002\u6ca1\u6709\u9519\u8bef\u6216\u5f02\u5e38\uff0c\u53ea\u662f\u4e00\u4e2a\u7a7a\u54cd\u5e94\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"context\"\n    \"io\"\n    \"io\/ioutil\"\n    \"os\"\n\n    speech \"cloud.google.com\/go\/speech\/apiv1\"\n    speechpb \"google.golang.org\/genproto\/googleapis\/cloud\/speech\/v1\"\n)\n\nfunc send(w io.Writer, client *speech.Client, filename string) error {\n    ctx := context.Background()\n    data, err := ioutil.ReadFile(filename)\n    if err != nil {\n        return err\n    }\n\n    \/\/ Send the contents of the audio file with the encoding and\n    \/\/ and sample rate information to be transcripted.\n    req := &amp;speechpb.LongRunningRecognizeRequest{\n        Config: &amp;speechpb.RecognitionConfig{\n                Encoding:        speechpb.RecognitionConfig_LINEAR16,\n                SampleRateHertz: 16000,\n                LanguageCode:    \"en-US\",\n        },\n        Audio: &amp;speechpb.RecognitionAudio{\n                AudioSource: &amp;speechpb.RecognitionAudio_Content{Content: data},\n        },\n    }\n\n    op, err := client.LongRunningRecognize(ctx, req)\n    if err != nil {\n        return err\n    }\n    resp, err := op.Wait(ctx)\n    if err != nil {\n        return err\n    }\n\n    \/\/ Print the results.\n    fmt.Println(resp,\"is response from Google\")\n    for _, result := range resp.Results {\n        for _, alt := range result.Alternatives {\n                fmt.Fprintf(w, \"\\\"%v\\\" (confidence=%3f)\\n\", alt.Transcript, alt.Confidence)\n        }\n    }\n    return nil\n}\n\nfunc main() {\n    ctx := context.Background()\n    var speech_client,err = speech.NewClient(ctx)\n    if err != nil {\n        fmt.Println(\"error creating speech client\")\n    }\n    send(os.Stdout,speech_client,\"hello.wav\")\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u662f\u8fd9\u6837\u5de5\u4f5c\u7684\uff0c\u5982\u4e0b\u6240\u793a\u3002\u4e0d\u8981\u6307\u5b9a\u53ef\u9009\u7684 sampleratehertz &#8211; \u5c06\u4ece *.wav \u6587\u4ef6\u7684\u6807\u5934\u8bfb\u53d6\u6b63\u786e\u7684\u503c\u3002\u6ce8\u610f\uff1a\u5fc5\u987b\u5c06\u6211\u7684\u7acb\u4f53\u58f0\u6587\u4ef6\u8f6c\u6362\u4e3a\u5355\u58f0\u9053\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 audacity \u6765\u505a\u5230\u8fd9\u4e00\u70b9\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"io\/ioutil\"\n    \"golang.org\/x\/net\/context\"\n    speech \"cloud.google.com\/go\/speech\/apiv1\"\n    speechpb \"google.golang.org\/genproto\/googleapis\/cloud\/speech\/v1\"\n)\n\nfunc main () {\n\n    \/\/ Once you are all set up, you need to start a new Context. A context is essentially an object that allows for more effective interaction with 3rd party APIs, by having individual handlers for when an API has completed a request, failed, or taken too long. Because we are going to be making requests to the Google Speech API, it is important to have handlers for these scenarios.\n    \n    ctx := context.Background() \n    \n    client, err := speech.NewClient(ctx)\n    if(err != nil){ \n        fmt.Println(err)\n    }\n    \n    \/\/ The first line starts a new empty context, named ctx. This then gets passed into the NewClient method, which will use your authentication setup from earlier to confirm your specific account. The next block just catches any error that may be returned from the Speech API. If you do get an error, you may need to retry configuring your authentication keys. We now need to specify a sound file to use, and process it into an Audio data object:\n    \n    fileDir := \"hello.wav\"\/\/;\n    \n    audioData, err := ioutil.ReadFile(fileDir)\/\/;\n    if(err != nil){\n         fmt.Println(err)\n    }\n    \n    \/\/ The module used is ioutil, and is responsible for formatting audio data into a kind of buffer, ready to be sent to the API. Next, we need to provide certain presets as a speechpb.RecognitionConfig object which is passed through as a parameter to the main method client.Recognize (which actually carries out the request):\n    \n        \/\/SampleRateHertz: 22050,\n    response, err := client.Recognize(ctx, &amp;speechpb.RecognizeRequest{\n        Config: &amp;speechpb.RecognitionConfig{\n        Encoding: speechpb.RecognitionConfig_LINEAR16,\n        LanguageCode: \"en-US\",\n    },\n        Audio: &amp;speechpb.RecognitionAudio{\n        AudioSource: &amp;speechpb.RecognitionAudio_Content{Content: audioData},\n    },\n    })\n    \n    \/\/ Most of the presets are self-explanatory, and easy to experiment with. The sample rate is essentially the quality of data, where the highest value allowed is 16000. The encoding field informs the program how to represent the data, where LINEAR16 refers to uncompressed 16-bit signed samples.\n    \n    if(err != nil){\n         fmt.Println(err)\n    }\n    \n    \/\/ Again, we check and output any errors from the response. Finally, we can output the transcript:\n    \n    if response != nil {\n        fmt.Println(\"response is\", response)\n        for _, result := range response.Results{\n            for _, alt := range result.Alternatives{\n                 fmt.Println(alt.Transcript)\n        \n            }\n        }\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4e3a\u4ec0\u4e48\u8fd9\u4e2a Google \u8bed\u97f3\u4ee3\u7801\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208015","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208015"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208015\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}