python批量语音识别转文字

注册申请

https://cloud.ibm.com/catalog/services/text-to-speech

# -*- coding:UTF-8 -*-
import os,linecache
import time
import json
import requests
import codecs # 比较方便处理各种非ascii编码的库

for y in linecache.updatecache(r'filelistlog.txt'):
    try:
       xx= ''
       headers = {
         'Content-Type': 'audio/mp3',
       }
       y = y.strip('\n') # 去掉换行符

       data = open(y, 'rb').read()

       r = requests.post('https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?model=zh-CN_BroadbandModel', headers=headers, data=data,auth=('apikey', '*******************************'))

       hjson = json.loads(r.text)
       xx =y + '||'+ hjson['results'][0]['alternatives'][0]['transcript'] 


       print xx

       with codecs.open('filelistlog2.txt','a',encoding='utf-8') as f:  
           f.write(xx)
           f.write('\r\n')

       time.sleep(3)

    except Exception,e:
        xx= ''
        print e

标签:speech-to-text