使用 Google Cloud API python 示例异步记录代码给我属性错误
Using Google Cloud API python sample asynchronous recording code is giving me attribute error
Link 编码:
我正在使用 Google Speech API 中的示例 python 代码将长(大于 1 分钟)的音频文件从语音转换为文本。我如何 运行 PyCharm 中的代码,以便它使用我创建的 API 密钥(用于向我的帐户收费)将我的音频文件(波形格式)转换为文本,而无需 '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)
我希望它输出一个文件,其中包含正在转录的音频文件中的文本,并在此过程中向我的帐户收费。
https://docs.python.org/3/library/argparse.html
使用 parser.add_argument('--path', help='C:/Users/Dave/Desktop/mizu/output.wav')
您刚刚定义了您的脚本在从命令行调用后可以接受参数 --path
,并且该文本只是在有人使用 [=14 启动您的脚本时显示的帮助文本=] 参数。
因此,如果 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')
Link 编码:
我正在使用 Google Speech API 中的示例 python 代码将长(大于 1 分钟)的音频文件从语音转换为文本。我如何 运行 PyCharm 中的代码,以便它使用我创建的 API 密钥(用于向我的帐户收费)将我的音频文件(波形格式)转换为文本,而无需 '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)
我希望它输出一个文件,其中包含正在转录的音频文件中的文本,并在此过程中向我的帐户收费。
https://docs.python.org/3/library/argparse.html
使用 parser.add_argument('--path', help='C:/Users/Dave/Desktop/mizu/output.wav')
您刚刚定义了您的脚本在从命令行调用后可以接受参数 --path
,并且该文本只是在有人使用 [=14 启动您的脚本时显示的帮助文本=] 参数。
因此,如果 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')