google语音识别API的使用

参考文章:

http://blog.laobubu.net/546?replytocom=367

http://people.csail.mit.edu/hubert/pyaudio/

http://code.google.com/p/pygalaxy/wiki/SWMixer

http://audiotools.sourceforge.net/install.html

http://flac.sourceforge.net/

首先安装工具软件:

sudo apt-get install flac python-pyaudio

然后执行如下python脚本:

 

#!/usr/bin/python
""" Record a few seconds of audio and save to a WAVE file. """
 
import pyaudio
import wave
import sys,os
import urllib2
 
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
#RATE = 44100
RATE = 16000
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
FLAC_OUTPUT_FILENAME = "output.flac"
 
p = pyaudio.PyAudio()
 
stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                frames_per_buffer = chunk)
 
print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
    data = stream.read(chunk)
    all.append(data)
print "* done recording"
 
stream.close()
p.terminate()
 
# write data to WAVE file
data = ''.join(all)
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
 
cmd = 'flac ' + WAVE_OUTPUT_FILENAME + ' -f -o ' + FLAC_OUTPUT_FILENAME;
#print cmd
os.system(cmd);
os.remove(WAVE_OUTPUT_FILENAME);
 
url = 'http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN'
audio=open(FLAC_OUTPUT_FILENAME ,'rb').read()
headers = {'Content-Type' : 'audio/x-flac; rate=16000'}
req = urllib2.Request(url, audio, headers)
response = urllib2.urlopen(req)
print response.read().decode('UTF-8')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值