Contents

首先去IBM注册一下账号,拿到调用接口的凭证用户名密码
https://console.bluemix.net/catalog/services/speech-to-text
步骤我大概分为几个截图来展示。创建(多种规格可选)->新建凭证->查看信息

Aarron Swartz
Aarron Swartz
Aarron Swartz
Aarron Swartz

以下脚本为转换本地wav格式的普通话音频为文字txt。需要翻墙。
别忘了

1
pip install SpeechRecognition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#! /usr/bin/env python
#coding:utf-8
import os
import speech_recognition as sr
import time
import datetime
starttime = datetime.datetime.now()
i = 1
for name in os.listdir(r'C:\Users\Administrator\Desktop\a'):
print("%d %s 开始转换" % (i, name))
##音频分块识别
r = sr.Recognizer()
try:
with sr.WavFile(r'C:\Users\Administrator\Desktop\a\%s' % name) as source:
audio = r.record(source)
IBM_USERNAME = 'XXXXXXXXXXXXXXXXXXXXXXXX'
IBM_PASSWORD = 'XXXXXXXXX'
text = r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD, language='zh-CN')
print(text)
open(r'C:\Users\Administrator\Desktop\a\%s.txt' % name, 'a+').write(text)
time.sleep(5)
temptime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print('%s %d %s 已完成' % (temptime,i, name))

except Exception as e:
print(e)
temptime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print('%s %d %s 未完成' % (temptime, i, name))
continue
i += 1
jietime = datetime.datetime.now()
last=jietime-starttime
print('总共花费时间:%s'%last)
Contents