找回密码
 立即注册
搜索

教你怎样用Python停止语音辨认!非常精准

网上找到一些例子,有一些成熟的模型,可以将语音转成文字。例如Cloud Speech API,但是需求你运用google云平台的前提。

speech recognition

对于python这一非常成熟的胶水言语,在网上找一些现成的工具包真的不是一个太难的成绩。在GitHub上就发现了这样一个神奇的包:speech recognition

它可以支持实时翻译,当然前提是需求在机器上安装有关麦克风的依赖包;还可以支持将语音文件中的文字直接提取出来。经过speech recognition可以调用多种平台上的模型,比如google API,CMU sphinx,Microsoft Bing Speech,IBM Speech to Text,Wit.ai 等

离线转换

对于国内的网络环境,无法用google API来将语音数据转换成文本文件,由于在调用这个包的时分,需求衔接到google。当然,你可以租用一个国外的VPS来做这件事情。

这里讲一下如何在不联网的状况下,依然可以经过python来将语音文件转换成文字。这里用到的包为sphinx,sphinx是由美国卡内基梅隆大学开发的大词汇量、非特定人、延续英语语音辨认系统。

安装 sphinx

我本人所用的环境为ubuntu。

私信小编:01 支付残缺代码!
imyin@develop:~/Downloads/phinx$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial

在安装sphinx之前需求安装一些软件包
sudo apt-get install gcc automake autoconf libtool bison swig python-dev libpulse-dev

之后可以在相关网站上下载sphinxbase安装包,当然也可以直接clone github上的包

下载完之后停止解压
tar zxpf sphinxbase-5prealpha.tar.gz

修正文件名
mv sphinxbase-5prealpha sphinxbase
ls sphinxbase

AUTHORS doc indent.sh Makefile.am README.md src win32
autogen.sh .git LICENSE NEWS sphinxbase.pc.in swig
configure.ac include m4 README sphinxbase.sln test

如今我们应该运转autogen.sh来生成Makefiles和其他一些脚本以备后续的编译和安装。
./autogen.sh

下面末尾源码安装
make && sudo make install

执行完以上命令之后,假如没有出现什么报错信息,就阐明曾经安装成功了,但是此时你的命令并不可以失效,在运转命令时会出现这样的错误。
imyin@develop:~/Downloads/phinx/sphinxbase$ sphinx_lm_convert
sphinx_lm_convert: error while loading shared libraries: libsphinxbase.so.3: cannot open shared object file: No such file or directory

还需求让系统加载目录/usr/local/lib,为了让系统每次启动时都可以自动加载,可以修正系统配置文件ld.so.conf
sudo echo "/usr/local/lib" >> /etc/ld.so.conf
sudo ldconfig

这时分,就可以经过sphinx_lm_convert命令将模型DMP文件转成bin文件
sphinx_lm_convert -i zh_broadcastnews_64000_utf8.DMP -o zh_CN.lm.bin

下面这行代码是将中文的模型DMP文件转成了bin文件。在安装完sphinx后默许只支持英文,在存放模型的途径下只要一个文件名为en-US,所以这里需求添加一个处理中文的模型,相关文件可以在这个网址中下载。

在python中运用sphinx

想要在python中运用sphinx的话,需求安装一些依赖包。
pip install pydub -U # 担任将MP3文件转换为 wav 文件
pip install SpeechRecognition -U # 担任将语音转换成文字
sudo apt -qq install build-essential swig libpulse-dev # 为后面安装 pocketsphinx 做预备
pip install -U pocketsphinx # 为运用 sphinx
sudo apt-get install libav-tools # 为处理在调用 pydub 时出现的 warning :RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)

这时分,就可以启动ipython来试试效果了。
file_path = '/home/imyin/Downloads/phinx/test_data'
r = sr.Recognizer()
hello_zh = sr.AudioFile(os.path.join(file_path, 'test.wav'))
with hello_zh as source:
audio = r.record(source)

r.recognize_sphinx(audio, language='zh_CN')

'明天 天气 很'

可以看出,这个语音辨认器曾经失效了。但是我说的是“明天天气好热啊”。

看来sphinx中的模型并非很准呐,而且这只是一个短句子。我们接上去看看长句子的效果,我录了村上春树的《当我谈跑步时我谈些什么》中的一段内容。
那一年的七月里,我去了一趟希腊,要独自从雅典跑到马拉松,将那条原始的马拉松道路——马拉松至雅典——逆向跑上一趟。为什么要逆向跑呢?由于清晨便从雅典市中心出发,在道路末尾拥堵、空气被污染之前跑出郊区,一路直奔马拉松的话,道路的交通量远远少得多,跑起来比较温馨。这不是正式的比赛,本人一个人随意去跑,当然不能指望有什么交通管制。

hello_zh = sr.AudioFile(os.path.join(file_path, 'test2.wav'))
with hello_zh as source:
audio = r.record(source)

r.recognize_sphinx(audio, language='zh_CN')

'南 音 扬 的 只要 领 过 球 的 立场 是 希望 让 猪只 处理 渣滓 土木工程 上 打球 运动 充满 温情 能 成功 吗 而 中止 了 对 印尼 商报 称 他 不是 没有 立场 谈 那 一 枚 其中 春天 从 雅典 市中心 出发 寸 厂 都 可 成功 打破 寻求 对 於 能 提升 统筹 署 取缔 一路 直奔 马拉松 和 阿 惹 山 活动 等 二十 个 队 中 重申 这 不是 正常 的 比赛 本人 一个人 却 不断到 当然 不能 阐明 什么 这种 共识'

呃,看到结果,我觉得可以用一个来描画:差劲。两个字来描画:太差劲!

当然,这个模型只是我直接从网上下载上去的。训练它时所用到的语料不会那么完全,所以在测试时难免会出现不准确的状况。要想让模型愈加准确,需求本人在应用sphnix继续训练模型。

相关办法在其官网上可以找到,也有相应的教程。感兴味的冤家可以自行研讨。
Q: Why my accuracy is poor
Speech recognition accuracy is not always great. To test speech recognition you need to run recognition on prerecorded reference database to see what happens and optimize parameters.
You do not need to play with unknown values, the first thing you should do is to collect a database of test samples and measure the recognition accuracy. You need to dump speech utterances into wav files, write the reference text file and use decoder to decode it. Then calculate WER using the word_align.pl tool from Sphinxtrain. Test database size depends on the accuracy but usually it’s enough to have 10 minutes of transcribed audio to test recognizer accuracy reliably. The process is described in tutorialtuning.

文中提到的教程网址是https://cmusphinx.github.io/wiki/tutorialtuning/

Google API

应用google API来处理语音辨认则相当准确,不过需求衔接google,以下是我在VPS中执行的一段代码,可以看出,它将我的录音精准地翻译成了文字。

但是假如录音文件较大的话,会运转工夫很长,并且会前往一个超时的错误,这很是让我苦恼。

不过侥幸的是,speech_recognition支持将语音文件停止截取处理。例如,我可以只处理语音文件中的前15秒钟的内容。
with test as source:
audio = r.record(source, duration=15)
r.recognize_google(audio, language='zh-CN')
'那一年的7月里我去了一趟希腊有独自从雅典跑到马拉松江哪条原始的马拉松道路马拉松直雅典一想跑上一趟'

从下面的结果看,几乎比sphnix处理的效果好太多了。

经过看协助文档发现speech_recognition不只可以截取后面的录音,还可以截取中间的。
In [18]: r.record?
Signature: r.record(source, duration=None, offset=None)
Docstring:
Records up to ``duration`` seconds of audio from ``source`` (an ``AudioSource`` instance) starting at ``offset`` (or at the beginning if not specified) into an ``AudioData`` instance, which it returns.

If ``duration`` is not specified, then it will record until there is no more audio input.

例如我想处理5秒至20秒之间的内容。
with test as source:
audio = r.record(source, offset=5, duration=15)
r.recognize_google(audio, language='zh-CN')

'要独自从雅典跑到马拉松江哪条原始的马拉松道路马拉松直雅典一项跑上一趟为什么要一想到呢由于星斗变从雅典市中心出发'

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

大神点评3

三军i痞 2019-12-4 16:15:01 来自手机 显示全部楼层
无论是不是沙发都得回复下
回复

使用道具 举报

大唐制作 2019-12-5 15:34:37 来自手机 显示全部楼层
我也顶起出售广告位
回复

使用道具 举报

大馋猫子_ 2019-12-6 15:36:44 来自手机 显示全部楼层
众里寻他千百度,蓦然回首在这里!
回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies