Python练习册,每天一个小程序
第 0025 题: 使用 Python 实现:对着电脑吼一声,自动打开浏览器中的默认网站。
例如,对着笔记本电脑吼一声“百度”,浏览器自动打开百度首页。
关键字:Speech to Text
1. 申请API使用权限
参考:
- API Keys
- 利用Google Speech API实现Speech To Text(注意:API链接已经变为v2了)
2. 使用Curl命令进行测试
- 如果你能无障碍使用Google,那么:
|
|
- 或者,你可以代理:
|
|
3. 使用PyAudio进行录音
|
|
参考:
4. 调用API进行语音识别
在进行识别之前,还需要将录音文件转换为Flac格式。目前没有找到可用于转换Flac格式的Python包,所以只好调用外部命令进行转换:
|
|
这会生成同名的flac文件
若Google连接不顺畅,则可设置requests代理,仅支持http & https
Socks5代理需要使用额外的包,相关教程
|
|
5. 执行指令
极简单,应该扩展下
|
|
参考:对着电脑吼一声,自动打开谷歌网站或者自动打开命令行终端-CSDN
源码:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |