如何在Python中使用谷歌语音识别API? [关闭]

2024-03-26

我有一个 mp3 文件,我想使用 Google 的语音识别功能从该文件中获取文本。任何我可以找到文档或示例的想法将不胜感激。


看一眼谷歌云语音API https://cloud.google.com/speech/使开发人员能够将音频转换为文本 [...] API 可识别 80 多种语言和变体 [...] 您可以创建一个免费帐户来获取有限数量的 API 请求。

HOW TO:

你需要先安装gcloud python 模块 http://googlecloudplatform.github.io/gcloud-python/#/ & 谷歌 API python 客户端 https://developers.google.com/api-client-library/python/start/installation模块:

pip install --upgrade gcloud
pip install --upgrade google-api-python-client

然后在云平台控制台中,进入项目页面,选择或创建一个新项目。当您需要为您的项目启用计费后,然后启用云语音API https://console.cloud.google.com/flows/enableapi?apiid=speech.googleapis.com.

启用 Google Cloud Speech API 后,单击“转到凭据”按钮以设置您的 Cloud Speech API 凭据

See 设置服务帐户 https://cloud.google.com/speech/docs/common/auth#set_up_a_service_account有关如何通过代码授权 Cloud Speech API 服务的信息

您应该获取服务帐户密钥文件(JSON 格式)和 GOOGLE_APPLICATION_CREDENTIALS 环境变量,以允许您对 Speech API 进行身份验证

全部完成后,下载音频原始文件 https://cloud.google.com/speech/docs/samples/audio.raw来自谷歌以及语音发现_google_rest_v1.json https://cloud.google.com/speech/docs/samples/speech-discovery_google_rest_v1.json来自谷歌

修改之前下载的 JSON 文件以设置您的凭据密钥 然后确保您已将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为 .json 文件的完整路径:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_file.json

also

确保您已将 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Python中使用谷歌语音识别API? [关闭] 的相关文章

随机推荐