题目

对着电脑吼一声,自动打开浏览器中的默认网站。

例如,对着笔记本电脑吼一声“百度”,浏览器自动打开百度首页

解题思路

  1. 录制wav文件

    使用pyAudio模块

  2. wav文件转为文本

    使用百度语音识别功能

  3. 打开浏览器

    使用webbrowser模块

安装模块

使用pyAudio模块, mac+python3下安装步骤如下:

1
2
$brew install portaudio
$pip3 install pyaudio

录制wav文件

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import wave, pyaudio
from datetime import datetime


CHUNK = 1024 # 缓存块的大小
FORMAT = pyaudio.paInt16 # 取样值的量化格式
RATE = 8000 # 取样频率,百度语音识别库指定8000
CHANNELS = 1 # 声道数,百度语音识别库指定1
RECORD_SECONDS = 5 # 时间段,秒

def record_wave(to_dir=None):
if to_dir is None:
to_dir = "./"

pa = pyaudio.PyAudio()

# format 取样值的量化格式
# channels 声道数
# rate 取样频率,一秒内对声音信号的采集次数
# input 输入流标志
# frames_per_buffer 底层缓存块的大小
stream = pa.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = CHUNK)

print("* recording")

#
save_buffer = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
audio_data = stream.read(CHUNK)
save_buffer.append(audio_data)

print("* done recording")

# stop
stream.stop_stream()
stream.close()
pa.terminate()

# wav path
file_name = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")+".wav"
if to_dir.endswith('/'):
file_path = to_dir + file_name
else:
file_path = to_dir + "/" + file_name

# save file
wf = wave.open(file_path, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(pa.get_sample_size(FORMAT))
wf.setframerate(RATE)

# 注意join 前的类型,如果是str类型会出错
wf.writeframes(b''.join(save_buffer))
wf.close()

return file_path

wav转为文本

按照官网给的例子,无法正确获取数据

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from urllib import request
import json, base64, uuid, os
import wave
import pycurl
import io

# 以下3个变量是百度相关,请替换成自己的值
bda_app_id = "XXXXXXXXX"
bda_api_key = "XXXXXXXXX"
bda_secret_key = "XXXXXXXXX"

bda_access_token = ""
bda_expires_in = ""
ret_text = ""

# 获取唯一标识符
def get_mac_address():
return uuid.UUID(int=uuid.getnode()).hex[-12:]

# 获取百度token
def get_access_token():
url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=ZrjLfF5Rh7pOL66gaOmDGnXn&client_secret=16bac9645093ca2632ebb81015ff7544"

req = request.Request(url, method="POST")
resp = request.urlopen(req)
data = resp.read().decode('utf-8')
json_data = json.loads(data)

global bda_access_token
bda_access_token = json_data['access_token']

return bda_access_token

# 读取wav文件内容
CHUNK = 1024
def get_wav_data(wav_path):
if wav_path is None or len(wav_path) == 0:
return None

# 使用"rb"(二进制模式)打开文件
wav_file = wave.open(wav_path, 'rb')
nframes = wav_file.getnframes()
audio_data = wav_file.readframes(nframes)

return audio_data, nframes


# 解析返回值
def dump_res(buf):
resp_json = json.loads(buf.decode('utf-8'))
ret = resp_json['result']

global ret_text
ret_text = ret[0]

print(buf)

def wav_to_text(wav_path):
if wav_path is None or len(wav_path) == 0:
return None

if len(bda_access_token) == 0:
get_access_token()
if len(bda_access_token) == 0:
return None

data, f_len = get_wav_data(wav_path)

url = 'http://vop.baidu.com/server_api?cuid=' + get_mac_address() + '&token=' + bda_access_token

# 必须是list,不能是dict
http_header = [
'Content-Type: audio/pcm; rate=8000',
'Content-Length: %d' % f_len
]

c = pycurl.Curl()

# url
c.setopt(pycurl.URL, str(url))

# header
c.setopt(c.HTTPHEADER, http_header)

# Method
c.setopt(c.POST, 1)

# 连接超时时间
c.setopt(c.CONNECTTIMEOUT, 30)

# 请求超时时间
c.setopt(c.TIMEOUT, 30)

# 返回信息回调
c.setopt(c.WRITEFUNCTION, dump_res)

# post 的信息
c.setopt(c.POSTFIELDS, data)

# post的信息长度
c.setopt(c.POSTFIELDSIZE, f_len)

c.perform()

return ret_text

打开浏览器

1
2
3
4
5
6
7
8
9
10
11
12
13
14

import webbrowser

def browser_open_text(text):
if text is None:
return

url = "http://www.baidu.com"
if text.startswith("谷歌") or text.startswith("google"):
url = "http://www.google.com"
elif text.startswith("必应") or text.startswith("bing"):
url = "http://cn.bing.com"

webbrowser.open_new_tab(url)



Published with Hexo and Theme by Kael
X