# encoding=utf-8
import jieba from aip
import AipSpeech
import os
import sys
import time
import win32con
import wave
import numpy as np from pyaudio
import PyAudio,paInt16
import pygame from pynput.keyboard
import Key, Controller from random
import choice
#开始录音标识
APP_ID = '15118279'#百度分配的APP_ID注册时可以看到,这里是我的
API_KEY ='xUx0Gm2AG2YMtA3FnGfwoKdP'#百度分配的API_KEY注册时可以看到,这里是我的
SECRET_KEY = 'hdxyMvABhUD4xnacGtDdeHbEOUGmdjNx'#百度分配的SECRET_KEY注册时可以看到,这里是我的
然后用代码client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)实现对百度语音识别的调用;result = client.synthesis('语音控制开始', 'zh', 1, {'vol': 5,})用以标志语音控制的开始语句,同时接上下面的判断语句判断模块调用是否成功:
# 识别正确返回语音二进制错误则返回dict 参照下面错误码
if not isinstance(result,dict):
with open('audio.mp3', 'wb') as f:
f.write(result)#将“语音控制开始”这段文字语音翻译成音频文件audio.mp3
pygame.mixer.init()
track = pygame.mixer.music.load('audio.mp3')
pygame.mixer.music.play()
time.sleep(2)
pygame.mixer.music.stop()
print("开始")
#############################
class GenAudio(object):
def __init__(self):
self.num_samples = 2000 # pyaudio内置缓冲大小
self.sampling_rate = 8000 # 取样频率
self.level = 1500 # 声音保存的阈值
self.count_num = 20 # count_num个取样之内出现COUNT_NUM个大于LEVEL的取样则记录声音
self.save_length = 8 # 声音记录的最小长度:save_length?*?num_samples?个取样
self.time_count = 8 # 录音时间,单位s
self.voice_string = []
def save_wav(self, filename):
wf = wave.open(filename, 'wb')
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(self.sampling_rate)
wf.writeframes(np.array(self.voice_string).tostring())
wf.close()
def read_audio(self):
pa = PyAudio()
stream = pa.open(format=paInt16,channels=1, rate=self.sampling_rate, input=True,
frames_per_buffer=self.num_samples)
save_count = 0
save_buffer = []
time_count = self.time_count
while True:
time_count -= 1
# ?读入num_samples个取样
string_audio_data =stream.read(self.num_samples)
# ?将读入的数据转换为数组
audio_data =np.fromstring(string_audio_data, dtype=np.short)
# 计算大于?level?的取样的个数
large_sample_count =np.sum(audio_data > self.level)
print(np.max(audio_data)), "large_sample_count=>",large_sample_count
# ?如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块
if large_sample_count >self.count_num:
save_count = self.save_length
else:
save_count -= 1
if save_count < 0:
save_count = 0
if save_count > 0:
save_buffer.append(string_audio_data)
else:
if len(save_buffer) > 0:
self.voice_string =save_buffer
save_buffer = []
print("Recode?a?piece?of??voice?successfully!")
return True
if time_count == 0:
if len(save_buffer) > 0:
self.voice_string =save_buffer
save_buffer = []
print("Recode?a?piece?of??voice?successfully!")
return True
else:
return True
r = GenAudio()
r.read_audio()
#百度语音识别
# 读取文件
def get_file_content(filePath):
cmd_str = "ffmpeg -y -i %s -acodec pcm_s16le -f s16le -ac 1 -ar 16000 %s.pcm"%(filePath,filePath)
os.system(cmd_str) # 调用系统命令ffmpeg,传入音频文件名即可
with open(filePath + ".pcm",'rb') as fp:
return fp.read()
然后用# 识别本地文件
a =client.asr(get_file_content('test.wav'), 'pcm', 16000, {
'dev_pid': 1536,
})
if a.get('result'):
a=a.get('result')[0]
print(a)即可实现对录音文件的识别,此时会输出识别的结果,例如a=“我想问下徐州的天气”。再接着依靠jieba分词对你说的话分段
seg_list = jieba.cut(a)
s=("".join(seg_list))
print(s)
a="["+s+"]"
此时就会将你说的话一个字一个字的分开,并保存为数组a。
再定义几个数组为
song=["歌","歌曲","音乐","听"]
movie=["播放","电视","看","电影"]
brower=["搜索","浏览"]
if "酷狗" in a:
os.startfile("D:\program files(x86)\kugou\KuGou.exe")
if "优酷" in a:
os.startfile(r"E:\YouKu\YoukuClient\proxy\YoukuDesktop.exe")
if "关机" in a:
os.system("shutdown -p")
如果你说的话中有“wifi”这两个字的话,就连接wifi,具体代码如下:
if "wifi" in a:
os.startfile(r'F:\代码\python\连接WiFi\jgh.exe')
for i in brower:
if i in a:
os.startfile(r"D:\program files(x86)\360se6\Application\360se.exe")
time.sleep(20)
keyboard = Controller()
keyboard.type(s)
time.sleep(2)
keyboard.press(Key.enter)
for i in song:
if i in a:
Newdir = []
path = r'C:\Users\asus\Music'
filelist = os.listdir(path) # 该文件夹下所有的文件(包括文件夹)
for files in filelist: # 遍历所有文件
Olddir = os.path.join(path,files) # 原来的文件路径
if os.path.isdir(Olddir): # 如果是文件夹则跳过
continue
filename =os.path.splitext(files)[0] # 文件名
filetype = os.path.splitext(files)[1] # 文件扩展名
new = path + "\\" + files
if filetype == ".mp3":
Newdir.append(new)
os.startfile(choice(Newdir))
◆
精彩推荐
◆
2019 中国大数据技术大会(BDTC)再度来袭!豪华主席阵容及百位技术专家齐聚,15 场精选专题技术和行业论坛,超强干货+技术剖析+行业实践立体解读,深入解析热门技术在行业中的实践落地。
即日起,限量 5 折票开售,数量有限,扫码购买,先到先得!