VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python语音识别入门及实践(3)

本程序从第 4.7 秒开始记录,从而使得词组 “it takes heat to bring out the odor” ,中的 “it t” 没有被记录下来,此时 API 只得到 “akes heat” 这个输入,而与之匹配的是 “Mesquite” 这个结果。

同样的,在获取录音结尾词组 “a cold dip restores health and zest” 时 API 仅仅捕获了 “a co” ,从而被错误匹配为 “Aiko” 。

噪音也是影响翻译准确度的一大元凶。上面的例子中由于音频文件干净从而运行良好,但在现实中,除非事先对音频文件进行处理,否则不可能得到无噪声音频。

 

噪声对语音识别的影响

噪声在现实世界中确实存在,所有录音都有一定程度的噪声,而未经处理的噪音可能会破坏语音识别应用程序的准确性。

要了解噪声如何影响语音识别,请下载 “jackhammer.wav” (https://github.com/realpython/python-speech-recognition/tree/master/audio_files)文件,并确保将其保存到解释器会话的工作目录中。文件中短语 “the stale smell of old beer lingers” 在是很大钻墙声的背景音中被念出来。

 

尝试转录此文件时会发生什么?

1
2
3
4
5
6
>>> jackhammer = sr.AudioFile('jackhammer.wav')
>>> with jackhammer as source:
...   audio = r.record(source)
...
>>> r.recognize_google(audio)
'the snail smell of old gear vendors'

那么该如何处理这个问题呢?可以尝试调用 Recognizer 类的adjust_for_ambient_noise()命令。

1
2
3
4
5
6
>>> with jackhammer as source:
...   r.adjust_for_ambient_noise(source)
...   audio = r.record(source)
...
>>> r.recognize_google(audio)
'still smell of old beer vendors'

这样就与准确结果接近多了,但精确度依然存在问题,而且词组开头的 “the” 被丢失了,这是什么原因呢?

 

因为使用 adjust_for_ambient_noise()命令时,默认将文件流的第一秒识别为音频的噪声级别,因此在使用 record()获取数据前,文件的第一秒已经被消耗了。

可使用duration关键字参数来调整adjust_for_ambient_noise()命令的时间分析范围,该参数单位为秒,默认为 1,现将此值降低到 0.5。

1
2
3
4
5
6
>>> with jackhammer as source:
...   r.adjust_for_ambient_noise(source, duration=0.5)
...   audio = r.record(source)
...
>>> r.recognize_google(audio)
'the snail smell like old Beer Mongers'

 

现在我们就得到了这句话的 “the”,但现在出现了一些新的问题——有时因为信号太吵,无法消除噪音的影响。

若经常遇到这些问题,则需要对音频进行一些预处理。可以通过音频编辑软件,或将滤镜应用于文件的 Python 包(例如SciPy)中来进行该预处理。处理嘈杂的文件时,可以通过查看实际的 API 响应来提高准确性。大多数 API 返回一个包含多个可能转录的 JSON 字符串,但若不强制要求给出完整响应时,recognition_google()方法始终仅返回最可能的转录字符。

 

通过把 recognition_google()中 True 参数改成 show_all 来给出完整响应。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> r.recognize_google(audio, show_all=True)
{'alternative': [
 {'transcript''the snail smell like old Beer Mongers'}, 
 {'transcript''the still smell of old beer vendors'}, 
 {'transcript''the snail smell like old beer vendors'},
 {'transcript''the stale smell of old beer vendors'}, 
 {'transcript''the snail smell like old beermongers'}, 
 {'transcript''destihl smell of old beer vendors'}, 
 {'transcript''the still smell like old beer vendors'}, 
 {'transcript''bastille smell of old beer vendors'}, 
 {'transcript''the still smell like old beermongers'}, 
 {'transcript''the still smell of old beer venders'}, 
 {'transcript''the still smelling old beer vendors'}, 
 {'transcript''musty smell of old beer vendors'}, 
 {'transcript''the still smell of old beer vendor'}
], 'final': True}

相关教程