Python实现简单的语音识别系统


Posted in Python onDecember 13, 2017

最近认识了一个做Python语音识别的朋友,聊天时候说到,未来五到十年,Python人工智能会在国内掀起一股狂潮,对各种应用的冲击,不下于淘宝对实体经济的冲击。在本地(江苏某三线城市)做这一行,短期可能显不出效果,但从长远来看,绝对是一个高明的选择。朋友老家山东的,毕业来这里创业,也是十分有想法啊。

将AI课上学习的知识进行简单的整理,可以识别简单的0-9的单个语音。基本方法就是利用库函数提取mfcc,然后计算误差矩阵,再利用动态规划计算累积矩阵。并且限制了匹配路径的范围。具体的技术网上很多,不再细谈。

现有缺点就是输入的语音长度都是1s,如果不固定长度则识别效果变差。改进思路是提取有效语音部分。但是该部分尚未完全做好,只写了一个原形函数,尚未完善。

Python实现简单的语音识别系统

Python实现简单的语音识别系统

import wave
import numpy as np
import matplotlib.pyplot as plt
from python_speech_features import mfcc
from math import cos,sin,sqrt,pi
def read_file(file_name):
  with wave.open(file_name,'r') as file:
    params = file.getparams()
    _, _, framerate, nframes = params[:4] 
    str_data = file.readframes(nframes)
    wave_data = np.fromstring(str_data, dtype = np.short)
    time = np.arange(0, nframes) * (1.0/framerate)
    return wave_data, time 
  return index1,index2
def find_point(data):
  count1,count2 = 0,0
  for index,val in enumerate(data):
    if count1 <40:
      count1 = count1+1 if abs(val)>0.15 else 0
      index1 = index
    if count1==40 and count2 <5:
      count2 = count2+1 if abs(val)<0.001 else 0
      index2 = index
    if count2==5:break
  return index1,index2
def select_valid(data):
  start,end = find_point(normalized(data))
  print(start,end)
  return data[start:end]
def normalized(a):
  maximum = max(a)
  minimum = min(a)
  return a/maximum

def compute_mfcc_coff(file_prefix = ''):
  mfcc_feats = []
  s = range(10)
  I = [0,3,4,8]
  II = [5,7,9]
  Input = {'':s,'I':I,'II':II,'B':s}
  for index,file_name in enumerate(file_prefix+'{0}.wav'.format(i) for i in Input[file_prefix]):
    data,time = read_file(file_name)
    #data = select_valid(data)
    #if file_prefix=='II':data = select_valid(data)

    mfcc_feat = mfcc(data,48000)[:75]
    mfcc_feats.append(mfcc_feat)
  t = np.array(mfcc_feats)
  return np.array(mfcc_feats)
def create_dist():

  for i,m_i in enumerate(mfcc_coff_input):#get the mfcc of input
    for j,m_j in enumerate(mfcc_coff):#get the mfcc of dataset
      #build the distortion matrix bwtween i wav and j wav
      N = len(mfcc_coff[0])
      distortion_mat = np.array([[0]*len(m_i) for i in range(N)],dtype = np.double)
      for k1,mfcc1 in enumerate(m_i):
        for k2,mfcc2 in enumerate(m_j):
          distortion_mat[k1][k2] = sqrt(sum((mfcc1[1:]-mfcc2[1:])**2))
      yield i,j,distortion_mat

def create_Dist():

  for _i,_j,dist in create_dist():
    N = len(dist)
    Dist = np.array([[0]*N for i in range(N)],dtype = np.double)
    Dist[0][0] = dist[0][0]
    for i in range(N):
      for j in range(N):
        if i|j ==0:continue
        pos = [(i-1,j),(i,j-1),(i-1,j-1)]
        Dist[i][j] = dist[i][j] + min(Dist[k1][k2] for k1,k2 in pos if k1>-1 and k2>-1)


    #if _i==0 and _j==1 :print(_i,_j,'\n',Dist,len(Dist[0]),len(Dist[1]))
    yield _i,_j,Dist
def search_path(n):
  comparison = np.array([[0]*10 for i in range(n)],dtype = np.double)
  for _i,_j,Dist in create_Dist():
    N = len(Dist)
    cut_off = 5
    row = [(d,N-1,j) for j,d in enumerate(Dist[N-1]) if abs(N-1-j)<=cut_off]
    col = [(d,i,N-1) for i,d in enumerate(Dist[:,N-1]) if abs(N-1-i)<=cut_off]
    min_d,min_i,min_j = min(row+col )
    comparison[_i][_j] = min_d
    optimal_path_x,optimal_path_y = [min_i],[min_j]
    while min_i and min_j:
      optimal_path_x.append(min_i)
      optimal_path_y.append(min_j)
      pos = [(min_i-1,min_j),(min_i,min_j-1),(min_i-1,min_j-1)]
      #try:
      min_d,min_i,min_j = min(((Dist[int(k1)][int(k2)],k1,k2) for k1,k2 in pos\
      if abs(k1-k2)<=cut_off))

    if _i==_j and _i==4:
      plt.scatter(optimal_path_x[::-1],optimal_path_y[::-1],color = 'red')
      plt.show()
  return comparison

mfcc_coff_input = []
mfcc_coff = []

def match(pre):
  global mfcc_coff_input
  global mfcc_coff
  mfcc_coff_input = compute_mfcc_coff(pre)
  compare = np.array([[0]*10 for i in range(len(mfcc_coff_input))],dtype = np.double)
  for prefix in ['','B']:
    mfcc_coff = compute_mfcc_coff(prefix)
    compare += search_path(len(mfcc_coff_input))
  for l in compare:
    print([int(x) for x in l])
    print(min(((val,index)for index,val in enumerate(l)))[1])
data,time = read_file('8.wav')
match('I')
match('II')

总结

以上就是本文关于Python实现简单的语音识别系统的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
一条命令解决mac版本python IDLE不能输入中文问题
May 15 Python
Django 忘记管理员或忘记管理员密码 重设登录密码的方法
May 30 Python
python3使用SMTP发送简单文本邮件
Jun 19 Python
python使用selenium登录QQ邮箱(附带滑动解锁)
Jan 23 Python
python实现文件批量编码转换及注意事项
Oct 14 Python
python制作朋友圈九宫格图片
Nov 03 Python
Python pandas库中的isnull()详解
Dec 26 Python
python 错误处理 assert详解
Apr 20 Python
Python爬虫实现HTTP网络请求多种实现方式
Jun 19 Python
Python爬虫过程解析之多线程获取小米应用商店数据
Nov 14 Python
详解Python调用系统命令的六种方法
Jan 28 Python
PyTorch中permute的使用方法
Apr 26 Python
关于反爬虫的一些简单总结
Dec 13 #Python
Python自动化运维_文件内容差异对比分析
Dec 13 #Python
Python实现自动发送邮件功能
Mar 02 #Python
django站点管理详解
Dec 12 #Python
Django 生成登陆验证码代码分享
Dec 12 #Python
python+django加载静态网页模板解析
Dec 12 #Python
Django入门使用示例
Dec 12 #Python
You might like
php对gzip文件或者字符串解压实例参考
2008/07/25 PHP
PHP 危险函数解释 分析
2009/04/22 PHP
php获取数组中重复数据的两种方法
2013/06/28 PHP
windows下安装php的memcache模块的方法
2015/04/07 PHP
yii2使用gridView实现下拉列表筛选数据
2017/04/10 PHP
在jQuery 1.5中使用deferred对象的代码(翻译)
2011/03/10 Javascript
nodejs 中模拟实现 emmiter 自定义事件
2016/02/22 NodeJs
jquery实现全选功能效果的实现代码
2016/05/05 Javascript
JavaScript的Backbone.js框架环境搭建及Hellow world示例
2016/05/07 Javascript
JavaScript 中有关数组对象的方法(详解)
2016/08/15 Javascript
react性能优化达到最大化的方法 immutable.js使用的必要性
2017/03/09 Javascript
Vue 组件间的样式冲突污染
2017/08/31 Javascript
WebSocket的通信过程与实现方法详解
2018/04/29 Javascript
jQuery实现的五星点评功能【案例】
2019/02/18 jQuery
js纯前端实现腾讯cos文件上传功能的示例代码
2019/05/14 Javascript
vue基于Echarts的拖拽数据可视化功能实现
2020/12/04 Vue.js
利用Python实现简单的相似图片搜索的教程
2015/04/23 Python
Python 中的 else详解
2016/04/23 Python
Python如何实现文本转语音
2016/08/08 Python
Python+Django搭建自己的blog网站
2018/03/13 Python
从django的中间件直接返回请求的方法
2018/05/30 Python
Python解析json时提示“string indices must be integers”问题解决方法
2019/07/31 Python
Python实现socket非阻塞通讯功能示例
2019/11/06 Python
用python解压分析jar包实例
2020/01/16 Python
django2.2 和 PyMySQL版本兼容问题
2020/02/17 Python
Pytest单元测试框架如何实现参数化
2020/09/05 Python
英国护肤品购物网站:Beauty Expert
2016/08/19 全球购物
希尔顿酒店中国网站:Hilton中国
2017/03/11 全球购物
新领导上任欢迎词
2014/01/13 职场文书
小学生迎国庆演讲稿
2014/09/05 职场文书
小学“向国旗敬礼”网上签名寄语活动总结
2014/09/27 职场文书
2014年销售工作总结与计划
2014/12/01 职场文书
经理聘任证明
2015/03/02 职场文书
2015年九一八事变纪念日演讲稿
2015/03/19 职场文书
python实现Nao机器人的单目测距
2021/09/04 Python
MySql中的json_extract函数处理json字段详情
2022/06/05 MySQL