2017-04-18 6 views
1

を持っています。コードは、.wavファイルを転記されたテキストに変換するのに十分でなければなりません。GoogleクラウドスピーチAPI Pythonのコードサンプルは、私はGoogleのスピーチAPI、<a href="https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe.py" rel="nofollow noreferrer">found here</a>から提供されたコードスニペットを通じて働いている可能バグ

注目ブロックはここにある:

def transcribe_file(speech_file): 
    """Transcribe the given audio file.""" 
    from google.cloud import speech 
    speech_client = speech.Client() 

    with io.open(speech_file, 'rb') as audio_file: 
     content = audio_file.read() 
     audio_sample = speech_client.sample(
      content=content, 
      source_uri=None, 
      encoding='LINEAR16', 
      sample_rate_hertz=16000) 

    alternatives = audio_sample.recognize('en-US') 
    for alternative in alternatives: 
     print('Transcript: {}'.format(alternative.transcript)) 

まず、私は、おそらくコードが古いだと思う、とsample_rate_hertz=16000sample_rate=16000に変更する必要がありました。その後

、私は、この行のエラーました:
alternatives = audio_sample.recognize('en-US')

AttributeError: 'Sample' object has no attribute 'recognize'

私はこれを修正する方法についての好奇心をお読みください。私はこの方法についての文書を見つけることができないようです。たぶんそれも交換する必要があります。

+0

がありますので、[ここ](http://stackoverflow.com/questions/38703853/how-to-use-google-speech-recognition-api-in-python/38788928#38788928)を見てください。同様の動作例 –

答えて

1

の1(辞書でなければなりません)、のようにすべての必要な引数が含まれている:

  • エンコーディング、
  • サンプルレート
  • 言語)

あなたが何かしようとする可能性があります。

with open(speech_file, 'rb') as speech: 
    speech_content = base64.b64encode(speech.read()) 

service = get_speech_service() 
service_request = service.speech().syncrecognize(
    body={ 
     'config': { 
      'encoding': 'LINEAR16', # raw 16-bit signed LE samples 
      'sampleRate': 16000, # 16 khz 
      'languageCode': 'en-US', # a BCP-47 language tag 
     }, 
     'audio': { 
      'content': speech_content.decode('UTF-8') 
      } 
     }) 
response = service_request.execute() 
print(json.dumps(response)) 

を同様の作業例があるので、見てhereを取ってください。

1

あなたはgithubのquickstart.py例を使用するので、私はそれがドキュメントGoogle Cloud Speech API class sampleと同期していないのですか。しかし、それはまだBETAです。

で、その後isinstance(audio_sample, <class Sample(object)>) == True
.recognizeを想定すると、あなたの

alternatives = audio_sample.recognize('en-US') 

あなたは、その後、バイナリとしてファイルを読み込む引数でservice.speech().syncrecognizeを使用するnead

async_recognize, streaming_recognize, sync_recognize 
関連する問題

 関連する問題