如何使用IBM的Watson Speech to Text服务将音频文件转换为文本

时间:2019-02-07 21:52:20

标签: python ibm-watson

我运行了这段代码:

import json
import os
import sys
from watson_developer_cloud import SpeechToTextV1

def transcribe_audio(audio_file_name) :
    IBM_USERNAME = "apikey"
    IBM_PASSWORD = "Password"
    stt = SpeechToTextV1(username=IBM_USERNAME, password=IBM_PASSWORD)
    audio_file = open(audio_file_name, "rb")        
    json_file = os.path.abspath("audio")+".json"; 
    with open(json_file, 'w') as fp:
        result = stt.recognize(audio_file,timestamps=True, content_type='audio/wav', inactivity_timeout =-1,word_confidence = True)
        json.dump(result, fp, indent=2)
    script = "Script is : "
    for rows in result['results']:
        script += rows['alternatives'][0]['transcript']
    #print(script);

transcribe_audio("audio.wav")

我得到了这个错误:

Object of type 'DetailedResponse' is not JSON serializable

我试图只打印音频文件中的文本,但这也不起作用。 我只是想获取文本。

1 个答案:

答案 0 :(得分:0)

好的样本here。看起来您在get_result()通话中丢失了stt.recognize()

相关问题