"""风火轮语音识别器,去年写的一个python程序,识别语音的,就是把wav,mp3转换成文本,调用的是百度API,有需要的把代码拿去,换成自己的APPID就行.
"""
import tkinter
from tkinter import StringVar,filedialog
import tkinter.messagebox
import os
from aip import AipSpeech
def addToClipBoard(text):
"""把文本增加到剪粘板"""
command = 'echo ' + text.strip() + '| clip'
os.system(command)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
def echo(audiofile,extname):
""" 识别本地未压缩的wav文件和pcm文件,下表是dev_pid参数说明:
1536 普通话(支持简单的英文识别) 搜索模型 无标点 支持自定义词库
1537 普通话(纯中文识别) 输入法模型 有标点 不支持自定义词库
1737 英语 有标点 不支持自定义词库
1637 粤语 有标点 不支持自定义词库
1837 四川话 有标点 不支持自定义词库
1936 普通话远场 远场模型 有标点 不支持
"""
result=""
try:
p=client.asr(get_file_content(audiofile), extname, 16000, {'dev_pid': 1536,})
result = p['result'][0]
except:
pass
return result
def showapp():
# 创建应用程序窗口
root = tkinter.Tk()
root.title("风火轮语音识别器")
varName = StringVar()
varName.set('')
# 设置窗口大小
root.geometry('800x280+300+150')
# 创建标签
labelTitle = tkinter.Label(root, text=appName, justify=tkinter.CENTER , font=("微软雅黑", 24),fg='red')
# 将标签放到窗口上
labelTitle.place(x=280, y=50)
# 创建标签
labelName = tkinter.Label(root, text='结果:', justify=tkinter.RIGHT,width=80, font=("微软雅黑", 14))
# 将标签放到窗口上
labelName.place(x=00, y=150, width=90, height=20)
# 创建标签
labelWait = tkinter.Label(root, text='', justify=tkinter.RIGHT,width=80, font=("微软雅黑", 14),fg='blue')
# 将标签放到窗口上
labelWait.place(x=10, y=100, width=90, height=20)
# 创建文本框,同时设置关联的变量 entryName.get()参获取文本
entryName = tkinter.Entry(root, width=450, textvariable=varName,bg='#E0E0C4',font=("微软雅黑", 16))
entryName.place(x=80, y=140, width=700, height=35)
def cancel():
root.destroy()
def aboutme():
tkinter.messagebox.showinfo(appName,"作者:李兴球")
def helpyou():
helpinfo = "请单击'浏览'按钮选择未经压缩的wav、pcm或amr音频文件。\n\n音频文件的时长不要超过一分钟。\n\n成功后会自动把内容放置在剪粘板里。"
tkinter.messagebox.showinfo(appName,helpinfo)
def select():
filename=filedialog.askopenfilename(filetypes=[("未压缩的wav", "*.wav"),("pcm文件", "*.pcm"),("amr文件", "*.amr"),("所有文件", "*.*")])
extname = os.path.splitext(filename)[1]
extname = extname[1:]
extname = extname.lower()
labelWait.config(text= "请稍候...")
root.update()
result = echo(filename,extname)
labelWait.config(text= "")
if result != "" :
varName.set(result)
addToClipBoard(result) #放到剪粘板上
else:
labelWait.config(text= "无法识别...")
buttonSelect = tkinter.Button(root, text='浏览', bg="#ADA758", font=("微软雅黑", 14),command=select)
buttonSelect.place(x=160, y=220, width=75, height=35)
buttonAbout = tkinter.Button(root, text='关于', bg="#ADA758", font=("微软雅黑", 14),command=aboutme)
buttonAbout.place(x=260, y=220, width=75, height=35)
buttonOk = tkinter.Button(root, text='帮助', bg="#ADA758", font=("微软雅黑", 14),command=helpyou)
buttonOk.place(x=360, y=220, width=75, height=35)
buttonCancel = tkinter.Button(root, text='退出', bg="#ADA758",
font=("微软雅黑", 14), command=cancel)
buttonCancel.place(x=460, y=220, width=75, height=35)
# 启动消息循环
root.mainloop()
if __name__ == "__main__":
appName = '风火轮语音识别器'
""" 百度开发者蓝杰稀饭的 APPID AK SK """
APP_ID = '你的ID'
API_KEY = '你的KEY'
SECRET_KEY = '你的安全key'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
showapp()
