【问题标题】:progress bar of google speech-to-text api long_running_recognize operationgoogle Speech-to-Text api long_running_recognize 操作进度条
【发布时间】:2019-08-19 14:46:54
【问题描述】:

我的问题类似于this 在 SO 上提出的问题,但由于最新的答案已有一年多的历史,并且 API 发生了很大变化,所以再次提出问题。 (我相信)

我正在运行long_running_recognize 操作,想知道它的进度。

from google.cloud import speech_v1 as speech
from google.cloud.speech_v1 import enums
from google.cloud.speech_v1 import types

gcs_uri = 'gs://my-new-videos/a49e0bf49a2e4d95b322bbf802e09d0e.wav'
client = speech.SpeechClient()

audio = types.RecognitionAudio(uri=gcs_uri)
config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=44100,
    language_code='en-US',
    audio_channel_count=2,
    enable_separate_recognition_per_channel=False,
    model='video',
    enable_word_time_offsets=False)

# ideally a way to get some sort of progress bar to know how long to wait.
operation = client.long_running_recognize(config, audio) 
print('Waiting for operation to complete...')
response = operation.result(timeout=90)

显然可以运行operation.running()operation.done() 来获取operation 的状态,但我无法弄清楚如何使用它来告诉我需要等待多长时间或已经等待了多少完毕。任何帮助将不胜感激。

【问题讨论】:

    标签: python speech-to-text google-speech-api google-cloud-speech


    【解决方案1】:

    我尝试了您的示例,但直到运行 response = operation.result(timeout=90) 才开始处理,然后它似乎阻止了代码执行。相反,如果我们使用回调方法,例如here 中的方法,我们可以在等待操作完成时访问Operation.metadata.progress_percent。作为示例,我每 5 秒检查一次进度:

    import time
    
    from google.cloud import speech_v1
    from google.cloud.speech_v1 import enums
    
    
    client = speech_v1.SpeechClient()
    
    encoding = enums.RecognitionConfig.AudioEncoding.FLAC
    sample_rate_hertz = 16000
    language_code = 'en-US'
    config = {'encoding': encoding, 'sample_rate_hertz': sample_rate_hertz, 'language_code': language_code}
    uri = 'gs://gcs-test-data/vr.flac'
    audio = {'uri': uri}
    
    response = client.long_running_recognize(config, audio)
    
    def callback(operation_future):
        result = operation_future.result()
        progress = response.metadata.progress_percent
        print(result)
    
    response.add_done_callback(callback)
    
    progress = 0
    
    while progress < 100:
        try:
            progress = response.metadata.progress_percent
            print('Progress: {}%'.format(progress))
        except:
            pass
        finally:
            time.sleep(5)
    

    请注意,在这种情况下,我使用了一个简短的公共音频文件,它从 0 到 100%,但似乎可以工作:

    Progress: 0%
    ...
    Progress: 0%
    results {
      alternatives {
        transcript: "it\'s okay so what am I doing here why am I here at GDC talking about VR video it\'s because I believe my favorite games I love games I believe in games my favorite games are the ones that are all about the stories I love narrative game design I love narrative-based games and I think that when it comes to telling stories in VR bring together capturing the world with narrative based games and narrative based game design is going to unlock some of the killer apps and killer stories of the medium"
        confidence: 0.959626555443
      }
    }
    results {
      alternatives {
        transcript: "so I\'m really here looking for people who are interested in telling us or two stories that are planning projects around telling those types of stories and I would love to talk to you so if this sounds like your project if you\'re looking at blending VR video and interactivity to tell a story I want to talk to you I want to help you so if this sounds like you please get in touch please come find me I\'ll be here all week I have pink hair I work for Google and I would love to talk with you further about VR video interactivity and storytelling"
        confidence: 0.954977035522
      }
    }
    
    Progress: 100%
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多