python-2.7 - 谷歌云语音转文本 AP
问题描述
我正在使用 Google Cloud Speech-to-Text AP 并尝试转录长音频文件。但是无法检测到存储桶中的音频文件。我收到一条错误消息:IOError: [Errno 2] No such file or directory:
定义转录_gcs(gcs_uri):
time(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))
解决方案
尝试这个
import requests
import json
url = "https://speech.googleapis.com/v1/speech:longrunningrecognize?key=<apiaccesskey>"
payload = {"config": {"encoding": "LINEAR16","sample_rate_hertz": 8000,
"language_code": "en-IN"},
"audio": {"uri": "gs://bucketname/file.flac"}}
r = requests.post(url, data=json.dumps(payload))
json_resp = r.json()
token_resp=json_resp['name']
url = "https://speech.googleapis.com/v1/operations/" + str(token_resp) +
"?key=<apiacesskey>"
content_response = requests.get(url)
content_json = content_response.json()
你的反应是content_json
可变的。
推荐阅读
- php - 哪个是 laravel 正确的数据库表结构?
- git - 由于项目双重克隆,无法运行自动化测试
- c# - if/else 潜在大条件的最佳方法
- java - Spring Boot JPA 设置自定义数据源
- angular - 警告:清理不安全的样式值
- groovy - 功能实现
- javascript - toLocaleString() 不适用于输入更改
- hide - 如何正确使用 react-day-picker-input 中的 hideDayPicker() 公共函数?
- c# - c# Blob Triggered Azure Function V1 with "App Service Plan" 被触发了一些延迟
- azure - Azure 应用服务 - MVC5 WebApp 缺少 css 文件