使用Google云API python异步录制示例代码是attribu给我的

2024-06-16 10:41:49 发布

您现在位置:Python中文网/ 问答频道 /正文

链接到代码:

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_async.py

我使用googlespeechapi中的python示例代码将长(大于1分钟)音频文件从语音转换为文本。如何在PyCharm中运行代码,以便它使用我创建的API键(向我的帐户收费)将我的音频文件(wave格式)转换为文本,而不会出现“NoneType”错误?你知道吗

我将音频文件的路径直接添加到代码中(第73行)。我还在'path'前面添加了“--”,以使其处理LOC(第73行)。我得到的错误如下:

**C:\Users\Dave\AppData\Local\Programs\Python\Python37\python.exe C:/Users/Dave/Desktop/mizu/gcapi.py
Traceback (most recent call last):
  File "C:/Users/Dave/Desktop/mizu/gcapi.py", line 75, in <module>
    if args.path.startswith('gs://'):
AttributeError: 'NoneType' object has no attribute 'startswith'
Process finished with exit code1**


import argparse
import io


# [START speech_transcribe_async]
def transcribe_file(speech_file):
    """Transcribe the given audio file asynchronously."""
    from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    client = speech.SpeechClient()

    # [START speech_python_migration_async_request]
    with io.open(speech_file, 'rb') as audio_file:
        content = audio_file.read()

    audio = types.RecognitionAudio(content=content)
    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=16000,
        language_code='en-US')

    # [START speech_python_migration_async_response]
    operation = client.long_running_recognize(config, audio)
    # [END speech_python_migration_async_request]

    print('Waiting for operation to complete...')
    response = operation.result(timeout=90)

    # Each result is for a consecutive portion of the audio. Iterate through
    # them to get the transcripts for the entire audio file.
    for result in response.results:
        # The first alternative is the most likely one for this portion.
        print(u'Transcript: {}'.format(result.alternatives[0].transcript))
        print('Confidence: {}'.format(result.alternatives[0].confidence))
    # [END speech_python_migration_async_response]
# [END speech_transcribe_async]


# [START speech_transcribe_async_gcs]
def transcribe_gcs(gcs_uri):
    """Asynchronously transcribes the audio file specified by the gcs_uri."""
    from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    client = speech.SpeechClient()

    audio = types.RecognitionAudio(uri=gcs_uri)
    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
        sample_rate_hertz=16000,
        language_code='en-US')

    operation = client.long_running_recognize(config, audio)

    print('Waiting for operation to complete...')
    response = operation.result(timeout=90)

    # Each result is for a consecutive portion of the audio. Iterate through
    # them to get the transcripts for the entire audio file.
    for result in response.results:
        # The first alternative is the most likely one for this portion.
        print(u'Transcript: {}'.format(result.alternatives[0].transcript))
        print('Confidence: {}'.format(result.alternatives[0].confidence))
# [END speech_transcribe_async_gcs]


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        '--path', help='C:/Users/Dave/Desktop/mizu/output.wav')
    args = parser.parse_args()
    if args.path.startswith('gs://'):
        transcribe_gcs(args.path)
    else:
        transcribe_file(args.path)

我希望它能输出一个文件,其中包含正在转录的音频文件中的文本,并在此过程中向我的帐户计费。你知道吗


Tags: thepathfromimportcloudforasyncgoogle
1条回答
网友
1楼 · 发布于 2024-06-16 10:41:49

https://docs.python.org/3/library/argparse.html

通过parser.add_argument(' path', help='C:/Users/Dave/Desktop/mizu/output.wav'),您刚刚定义了脚本在从命令行调用参数 path之后可以接受该参数,并且如果有人用 help参数启动脚本,则该文本只是一个帮助文本。你知道吗

因此,如果if __name__ == '__main__'所在的脚本具有myscript.py文件,您实际上必须像这样开始您的脚本:

python myscript.py  path C:/Users/Dave/Desktop/mizu/output.wav

但是,这个例程在这种情况下没有任何意义,您的解决方案是去掉多余的代码。只需:

if __name__ == '__main__':
    transcribe_file('C:/Users/Dave/Desktop/mizu/output.wav')

相关问题 更多 >