关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
5年前关闭。
Improve this question
我有一个 mp3 文件,我想使用 Google 的语音识别从该文件中获取文本。任何我能找到文档或示例的想法都将不胜感激。
最佳答案
看看Google Cloud Speech API使开发人员能够将音频转换为文本 [...] API 可识别 80 多种语言和变体 [...]
您可以创建一个免费帐户来获得有限数量的 API 请求。
如何:
您需要先安装gcloud python module & google-api-python-client模块:
pip install --upgrade gcloud
pip install --upgrade google-api-python-client
然后在 Cloud Platform Console 中,转到项目页面并选择或创建一个新项目。在您需要为您的项目启用计费后,然后 enable Cloud Speech API .
启用 Google Cloud Speech API 后,单击 Go to Credentials 按钮设置您的 Cloud Speech API 凭据
见 Set Up a Service Account有关如何从您的代码授权 Cloud Speech API 服务的信息
您应该同时获取服务帐户 key 文件(JSON 格式)和 GOOGLE_APPLICATION_CREDENTIALS 环境变量,以允许您对语音 API 进行身份验证
全部完成后,download the audio raw file来自谷歌和speech-discovery_google_rest_v1.json来自谷歌
修改之前下载的 JSON 文件以设置您的凭据 key
然后确保您已将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为 .json 文件的完整路径:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_file.json
还
确保您已将 GCLOUD_PROJECT 环境变量设置为您的 Google Cloud 项目的 ID:
export GCLOUD_PROJECT=your-project-id
假设全部完成 ,您可以创建一个教程.py 文件包含:
import argparse
import base64
import json
from googleapiclient import discovery
import httplib2
from oauth2client.client import GoogleCredentials
DISCOVERY_URL = ('https://{api}.googleapis.com/$discovery/rest?'
'version={apiVersion}')
def get_speech_service():
credentials = GoogleCredentials.get_application_default().create_scoped(
['https://www.googleapis.com/auth/cloud-platform'])
http = httplib2.Http()
credentials.authorize(http)
return discovery.build(
'speech', 'v1beta1', http=http, discoveryServiceUrl=DISCOVERY_URL)
def main(speech_file):
"""Transcribe the given audio file.
Args:
speech_file: the name of the audio file.
"""
with open(speech_file, 'rb') as speech:
speech_content = base64.b64encode(speech.read())
service = get_speech_service()
service_request = service.speech().syncrecognize(
body={
'config': {
'encoding': 'LINEAR16', # raw 16-bit signed LE samples
'sampleRate': 16000, # 16 khz
'languageCode': 'en-US', # a BCP-47 language tag
},
'audio': {
'content': speech_content.decode('UTF-8')
}
})
response = service_request.execute()
print(json.dumps(response))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'speech_file', help='Full path of audio file to be recognized')
args = parser.parse_args()
main(args.speech_file)
然后运行:
python tutorial.py audio.raw
关于python - 如何在python中使用谷歌语音识别api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703853/