簡體   English   中英

獲取long_running_recognize操作的進度(Google Cloud Speech API)

[英]Get progress of a long_running_recognize operation (Google Cloud Speech API)

我的代碼很簡單。 它在單個通道的FLAC音頻文件上運行long_running_recognize ,並使用Google Cloud Speech API存儲結果。 我正在嘗試找到一種方法來獲取long_running_recognize操作的當前進度。 我找到了有關該主題的一些文檔,但是很難理解。

client = speech.SpeechClient()
operation = client.long_running_recognize(
  audio = speech.types.RecognitionAudio(
    uri = str('gs://speech-clips/'+self.audio_fqid),
  ),
  config = speech.types.RecognitionConfig(
    encoding = enums.RecognitionConfig.AudioEncoding.FLAC,
    sample_rate_hertz = sample_rate,
    enable_word_time_offsets = True,
    language_code = 'en-US',
  ),
)
response = operation.result()

這是我發現的一些文檔:

任何幫助將不勝感激。

由於長時間的音頻識別是一個漫長的過程,因此API將為您提供一個令牌,該令牌即為操作響應中的name ,一旦該過程完成,則done將變為true 我使用RESTful API來執行請求,希望它可以為您工作:

這是偽代碼:

    import googleapiclient.discovery
    import time
    service = googleapiclient.discovery.build('speech', 'v1')
    service_request = service.speech().longrunningrecognize(
        body= {
            "config": {
                "encoding": "FLAC",
                "languageCode": "en-US",
                "enableWordTimeOffsets": True
            },
            "audio": {
                "uri": str('gs://speech-clips/'+self.audio_fqid)
            }
        }
    )
    operation = service_request.execute()

    name = operation['name']
    service_request = service.operations().get(name=name)
    while True:

        # Give the server a few seconds to process.

        print('Waiting for server processing...')

        time.sleep(1)

        # Get the long running operation with response.

        response = service_request.execute()

        if 'done' in response and response['done']:
            break
    return response  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM