📜  使用 IBM Watson Studio 的语音转文本

📅  最后修改于: 2022-05-13 01:55:26.364000             🧑  作者: Mango

使用 IBM Watson Studio 的语音转文本

IBM Watson Studio是一个集成环境,旨在开发、训练、管理模型和部署 AI 驱动的应用程序,并且是在 IBM Cloud 上交付的软件即服务 (SaaS)解决方案。 IBM Cloud 提供了许多服务,例如 Speech To Text、Text To Speech、Visual Recognition、Natural Language Classifier、Language Translator 等。

Speech to Text 服务将音频转录为文本以启用应用程序的语音转录功能。

创建服务实例

  1. 转至 IBM Cloud 目录中的 Speech to Text 页面。
  2. 注册一个免费的 IBM Cloud 帐户或登录。
  3. 单击创建

将凭据复制到您的服务实例进行身份验证

  1. 从 IBM Cloud 资源列表中,单击您的 Speech to Text 服务实例以转至 Speech to Text 服务仪表板页面。
  2. 管理页面上,单击显示凭据以查看您的凭据。
  3. 复制API 密钥URL值。

所需模块:

  1. json
  2. ibm_watson:此模块未使用Python预定义。要安装它,请在终端中键入以下命令。
    pip install ibm_watson
    

现在您已准备好使用 IBM Cloud Services。

下面的代码说明了使用Python和 Web 套接字接口使用 IBM Watson Studio 的 Speech To Text Service

#Python Program To Use IBM Watson
# Studio's Speech To Text Below Code
# Accepts only .mp3 Format of Audio
# File 
  
   
import json
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_watson.websocket import RecognizeCallback, AudioSource
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
  
   
# Insert API Key in place of 
# 'YOUR UNIQUE API KEY'
authenticator = IAMAuthenticator('YOUR UNIQUE API KEY') 
service = SpeechToTextV1(authenticator = authenticator)
   
#Insert URL in place of 'API_URL' 
service.set_service_url('API_URL')
   
# Insert local mp3 file path in
# place of 'LOCAL FILE PATH' 
with open(join(dirname('__file__'), r'LOCAL FILE PATH'), 
          'rb') as audio_file:
      
        dic = json.loads(
                json.dumps(
                    service.recognize(
                        audio=audio_file,
                        content_type='audio/flac',   
                        model='en-US_NarrowbandModel',
                    continuous=True).get_result(), indent=2))
  
# Stores the transcribed text
str = ""
  
while bool(dic.get('results')):
    str = dic.get('results').pop().get('alternatives').pop().get('transcript')+str[:]
       
print(str)

输出

The Output will be Transcript (Text) of audio file.