Skip to content

Instantly share code, notes, and snippets.

@mpco
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpco/e0094e7fb2d2b072845c to your computer and use it in GitHub Desktop.
Save mpco/e0094e7fb2d2b072845c to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
__author__ = 'm'
__time__ = '15-4-20'
网络限制,未测试
"""
import os
import re
import wave
import pyaudio
import requests
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 8000
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
# 录音并写入文件
def record_wave():
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def speech2text(flac_file, rate, yourkey):
"""
使用Google Speech API进行语音识别
"""
url = 'https://www.google.com/speech-api/v2/recognize?output=json&lang=zh-CN&key=' + yourkey
files = {'file': open(flac_file, 'rb')}
headers = {'Content-Type': 'audio/x-flac; rate='+str(rate)+';'}
# 网络不通,则可设置requests代理,https://requests-docs-cn.readthedocs.org/zh_CN/latest/user/advanced.html#id11
# Socks5代理需要使用额外的包,http://blog.gohjkl.com/coder/2015/03/09/Python-Requests-socks-proxy/
return_text = requests.post(url, files=files, headers=headers).text
# 获取识别的文本内容
trans_text = re.findall('transcript\":\"(.*?)\"', return_text)
return ''.join(trans_text)
def text2cmd(cmds):
if cmds.find("谷歌") > -1:
os.system("chrome https://www.google.com")
if cmds.find("百度") > -1:
os.system("chrome https://www.baidu.com")
if __name__ == '__main__':
while True:
"""
Enter your Google API Key
"""
key = 'xxxxxxxxxxxxxxxxxxxx'
# 录音并写入文件
record_wave()
# 转换
os.system('flac output.wav')
# 语音识别
cmds_text = speech2text('output.flac', RATE, key)
# 执行指令
text2cmd(cmds_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment