美文网首页码农的世界程序员
Python神技之利用百度AI声控电脑关机

Python神技之利用百度AI声控电脑关机

作者: b4a0155c6514 | 来源:发表于2019-01-04 10:53 被阅读7次
Python神技之利用百度AI声控电脑关机

这次我们来看看如何利用百度AI来声控电脑关机。首先需要安装百度AI的Python SDK,并且创建语音识别的应用,获取AppID、API Key、Secret Key这三项内容,以便在我们写的程序里使用,详情可见上上篇文章,这里就不赘述了。

Python神技之利用百度AI声控电脑关机

完整代码如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1# coding: utf-8
2"""
3@author: Kevin Wong
4@function: python实现的录音及语音识别程序
5@time: 2018/11/15 23:14
6"""
7import os
8import wave
9import numpy as np
10from pyaudio import PyAudio, paInt16
11from aip import AipSpeech
12import os
13
14
15class Recorder(object):
16 def init(self):
17 # pyaudio内置缓冲大小
18 self.num_samples = 2000
19 # 取样频率
20 self.sampling_rate = 8000
21 # 声音保存的阈值
22 self.level = 1500
23 # count_num个取样之内出现COUNT_NUM个大于LEVEL的取样则记录声音
24 self.count_num = 20
25 # 声音记录的最小长度:save_length * num_samples 个取样
26 self.save_length = 8
27 # 录音时间,单位s
28 self.time_count = 8
29 self.voice_string = []
30
31 # 保存为音频文件
32 def save_wav(self, filename):
33 wf = wave.open(filename, 'wb')
34 wf.setnchannels(1)
35 wf.setsampwidth(2)
36 wf.setframerate(self.sampling_rate)
37 wf.writeframes(np.array(self.voice_string).tostring())
38 wf.close()
39
40 # 读取音频
41 def recorder(self):
42 pa = PyAudio()
43 stream = pa.open(format=paInt16, channels = 1, rate = self.sampling_rate, input = True, frames_per_buffer = self.num_samples)
44 save_count = 0
45 save_buffer = []
46 time_count = self.time_count
47 while True:
48 time_count -= 1
49 # 读入num_samples个取样
50 string_audio_data = stream.read(self.num_samples)
51 # 将读入的数据转换为数组
52 audio_data = np.fromstring(string_audio_data, dtype = np.short)
53 # 计算大于 level 的取样的个数
54 large_sample_count = np.sum(audio_data > self.level)
55 print(np.max(audio_data)), "large_sample_count=>", large_sample_count
56 # 如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块
57 if large_sample_count > self.count_num:
58 save_count = self.save_length
59 else:
60 save_count -=1
61 if save_count < 0:
62 save_count = 0
63 if save_count > 0:
64 save_buffer.append(string_audio_data)
65 else:
66 if len(save_buffer) > 0:
67 self.voice_string = save_buffer
68 save_buffer = []
69 print("Recode a piece of voice successfully!")
70 return True
71 if time_count == 0:
72 if len(save_buffer) > 0:
73 self.voice_string = save_buffer
74 save_buffer = []
75 print("Recode a piece of voice successfully!")
76 return True
77 else:
78 return False
79 return True
80
81
82# 读取本地音频文件
83def get_file_content(filePath):
84 with open(filePath, 'rb') as fp:
85 return fp.read()
86
87if name == 'main':
88 """ 你的 APPID AK SK """
89 APP_ID = '14810929'
90 API_KEY = 'hD1sGacRqCWybF9lBqumMriS'
91 SECRET_KEY = 'zKtG8uv3mv4tKqC5avL1ua9YGM38YAAG'
92 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
93
94 while True:
95 recorder = Recorder()
96 # 开始录音
97 recorder.recorder()
98 # 保存音频文件
99 recorder.save_wav("01.wav")
100 # 识别本地文件
101 res = client.asr(get_file_content('01.wav'), 'wav', 8000, {
102 'dev_pid': 1536,
103 })
104 print(res['result'][0])
105 try:
106 if "计算器" == res['result'][0]:
107 os.system("calc")
108 elif "关机" == res['result'][0]:
109 os.system("shutdown -s -t 300")
110 elif "取消关机" == res['result'][0]:
111 os.system("shutdown -a")
112 elif "退出程序" == res['result'][0]:
113 break
114 except:
115 pass
</pre>

这里采用了面向对象的编程风格,第15-79行定义了一个Recorder类,其主要功能是对音频文件进行处理,包括将程序运行后将用户的声音以二进制流的形式读取并保存为wav格式的音频文件, 第82行到第85行读取生成的音频文件,并返回文件内容。第87行是主线程入口,只要用户没有对电脑说“退出程序”,就会一直执行while循环读取用户的声音,将音频文件交给百度AI的语音识别接口,并返回识别的文字内容。根据识别的内容,调用Python的os库执行相应的操作。

运行程序后,对电脑说一声“关机”,运行结果如下:

Python神技之利用百度AI声控电脑关机

完结,撒花,ye~

相关文章

网友评论

    本文标题:Python神技之利用百度AI声控电脑关机

    本文链接:https://www.haomeiwen.com/subject/hcrhrqtx.html