audio file isn't being parsed with Google Speech

左心房为你撑大大i 提交于 2019-12-24 06:48:04

问题


This question is a followup to a previous question.

The snippet of code below almost works...it runs without error yet gives back a None value for results_list. This means it is accessing the file (I think) but just can't extract anything from it.

I have a file, sample.wav, living publicly here: https://storage.googleapis.com/speech_proj_files/sample.wav

I am trying to access it by specifying source_uri='gs://speech_proj_files/sample.wav'.

I don't understand why this isn't working. I don't think it's a permissions problem. My session is instantiated fine. The code chugs for a second, yet always comes up with no result. How can I debug this?? Any advice is much appreciated.

from google.cloud import speech
speech_client = speech.Client()

audio_sample = speech_client.sample(
    content=None,
    source_uri='gs://speech_proj_files/sample.wav',
    encoding='LINEAR16',
    sample_rate_hertz= 44100)
results_list = audio_sample.async_recognize(language_code='en-US')

回答1:


Ah, that's my fault from the last question. That's the async_recognize command, not the sync_recognize command.

That library has three recognize commands. sync_recognize reads the whole file and returns the results. That's probably the one you want. Remove the letter "a" and try again.

Here's an example Python program that does this: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe.py

FYI, here's a summary of the other types:

async_recognize starts a long-running, server-side operation to translate the whole file. You can make further calls to the server to see whether it's finished with the operation.poll() method and, when complete, can get the results via operation.results.

The third type is streaming_recognize, which sends you results continually as they are processed. This can be useful for long files where you want some results immediately, or if you're continuously uploading live audio.




回答2:


I finally got something to work:

import time

from google.cloud import speech
speech_client = speech.Client()


sample = speech_client.sample(
      content = None
    , 'gs://speech_proj_files/sample.wav'
    , encoding='LINEAR16'
    , sample_rate= 44100
    , 'languageCode': 'en-US'
)

retry_count = 100

operation = sample.async_recognize(language_code='en-US')

while retry_count > 0 and not operation.complete:
    retry_count -= 1
    time.sleep(10)
    operation.poll()  # API call

print(operation.complete)

print(operation.results[0].transcript)

print(operation.results[0].confidence)

for op in operation.results:
    print op.transcript

Then something like

for op in operation.results:
    print op.transcript


来源:https://stackoverflow.com/questions/43555694/audio-file-isnt-being-parsed-with-google-speech

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!