使用Python将Cloud Speech API的结果导出到JSON文件 [英] Export the result of cloud speech API to JSON file using Python

查看:142
本文介绍了使用Python将Cloud Speech API的结果导出到JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Google Speech API将音频文件转换为印度语言的文本. API返回类型为"google.cloud.speech_v1.types.SpeechRecognitionAlternative"的对象.我正在尝试将结果导出到.json文件.我是python的新手.这是我在python中进行的第一个项目.

I am trying to convert an audio file to text in an Indian language via Google Speech API. The API returns an object of the type "google.cloud.speech_v1.types.SpeechRecognitionAlternative". I am trying to export the result to .json file. I am very new to python. This is the first project I am doing in python.

    import io
    import os
    import pickle

    # Imports the Google Cloud client library
    from google.cloud
    import speech
    from google.cloud.speech
    import enums
    from google.cloud.speech
    import types

    client = speech.SpeechClient()

    audio = types.RecognitionAudio(uri = "gs://storage-staples-canada/client-data/TapTapTap.wav")
    config = types.RecognitionConfig(
      encoding = enums.RecognitionConfig.AudioEncoding.LINEAR16,
      sample_rate_hertz = 16000,
      language_code = 'hi-IN',
      enable_word_time_offsets = True,
      speech_contexts = [speech.types.SpeechContext(phrases = ['एक जंगल था।', 'ख़ूब घना जंगल।', 'एक दिन झमाझम पानी बरसा।', 'थोड़ी देर बाद बारिश रुक गई। सारे जानवर बाहर निकले।', 'तभी आवाज़ आई टप टप टपक सब घबरा गए।', 'सबने सोचा यह कैसी आवाज़ है? सारे बड़े जानवर डर कर भाग गए।', 'चींटी ने कहा- चलो साथियो, हम टप- टप- टपक को ढूँढें। चूहा बोला- शाबाश चींटी बहन!', 'जुगनू बोला- मैं आगे रहूँगा। बिल्ली बोली- मैं साथ हूँ।', 'ख़रगोश ने कहा- मैं भी पीछे नहीं रहूँगा। सब टप- टप- टपक को ढूँढने चले।', 'फिर सबने देखा, बरसात का पानी पत्तों पर टपक रहा था', 'टप- टप- टपक।'])], )

    operation = client.long_running_recognize(config, audio)
    print('Waiting for operation to complete...')
    response = operation.result(timeout = 90)

    # Gets the time - offsets of each of the words in the audio

    for result in response.results:
      alternative = result.alternatives[0]# The first alternative is the most likely one
    for this portion.
    print('Transcript: {}'.format(result.alternatives[0].transcript))
    print('Confidence: {}'.format(result.alternatives[0].confidence))
    for word_info in alternative.words:
      word = word_info.word
    start_time = word_info.start_time
    end_time = word_info.end_time
    print('Word: {}, start_time: {}, end_time: {}'.format(
      word,
      start_time.seconds + start_time.nanos * 1e-9,
      end_time.seconds + end_time.nanos * 1e-9))

当我尝试将API的结果(存储在以上代码中的响应变量中)转换为字典时.我得到的错误消息是"TypeError:'SpeechRecognitionAlternative'对象不可迭代".您能帮我将结果转换并导出为.json文件吗?

When I try to convert the results of the API(stored in response variable in the code above) to the dictionary. The error message I get "TypeError: 'SpeechRecognitionAlternative' object is not iterable". Can you help me convert and export the result to a .json file?

推荐答案

对于此任务,您可以使用命令行工具

For this task you can use command line tool gcloud. For example

gcloud ml speech recognize-long-running \
  gs://storage-staples-canada/client-data/TapTapTap.wav \
  --language-code=hi-IN --encoding=linear16 --sample-rate=16000 \
  --include-word-time-offsets \
  --hints="एक जंगल था।,ख़ूब घना जंगल।"
  --format=json

您可以添加--log-http标志以查看API交互,这可以帮助您修复python代码.

You can add --log-http flag to see API interactions which can help you fix your python code.

这篇关于使用Python将Cloud Speech API的结果导出到JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆