网站首页 > 开源技术 正文
语音识别是家庭自动化、人工智能等应用中的一个重要特性,本文介绍了如何利用Python语言识别库。
所需的组件
必须安装以下组件:
1)Python语音识别模块:
sudo pip install SpeechRecognition
2)PyAudio:对Linux用户使用以下命令
sudo apt-get install python-pyaudio python3-pyaudio
如果存储库中的版本太旧,请使用以下命令安装pyaudio
sudo apt-get install portaudio19-dev python-all-dev python3-all-dev &&
sudo pip install pyaudio
对于Python3,请使用pip3而不是pip。
Windows用户可以通过在终端中执行以下命令来安装pyaudio
pip install pyaudio
使用麦克风的语音输入和语音到文本的翻译
1)配置麦克风(用于外部麦克风):建议在程序执行期间指定麦克风,以免出现毛刺。
在终端中键入lsusb。将显示已连接设备的列表。麦克风名称如下所示
USB Device 0x46d:0x825: Audio (hw:1, 0)
记下该注释,因为它将在程序中使用。
2)设置块大小(Chunk Size):这基本上涉及指定我们一次要读取多少字节的数据。 通常,此值以2的幂指定,例如1024或2048。
3)设置采样率(Sampling Rate):采样率定义记录值进行处理的频率。
4)将设备ID设置为选定的麦克风:在此步骤中,我们指定要使用的麦克风的设备ID,以避免在存在多个麦克风的情况下产生歧义。从某种意义上说,这也有助于调试,在运行程序时,我们将知道是否已识别指定的麦克风。在编程期间,我们指定参数device_id。 如果未识别到麦克风,程序将提示找不到device_id。
5)允许调整环境噪声:由于周围的噪声会发生变化,因此我们必须让程序稍等一秒钟来调整录音的能量阈值,以便根据外部噪声水平对其进行调整。
6)语音到文本的翻译:这是在Google语音识别的帮助下完成的。这需要有效的Internet连接才能工作。但是,有某些脱机识别系统,例如PocketSphinx,但是安装过程非常严格,需要几个依赖项。Google语音识别是最容易使用的一种。
上述步骤实施如下:
#Python 2.x program for Speech Recognition
import speech_recognition as sr
#enter the name of usb microphone that you found
#using lsusb
#the following name is only used as an example
mic_name = "USB Device 0x46d:0x825: Audio (hw:1, 0)"
#Sample rate is how often values are recorded
sample_rate = 48000
#Chunk is like a buffer. It stores 2048 samples (bytes of data)
#here.
#it is advisable to use powers of 2 such as 1024 or 2048
chunk_size = 2048
#Initialize the recognizer
r = sr.Recognizer()
#generate a list of all audio cards/microphones
mic_list = sr.Microphone.list_microphone_names()
#the following loop aims to set the device ID of the mic that
#we specifically want to use to avoid ambiguity.
for i, microphone_name in enumerate(mic_list):
if microphone_name == mic_name:
device_id = i
#use the microphone as source for input. Here, we also specify
#which device ID to specifically look for incase the microphone
#is not working, an error will pop up saying "device_id undefined"
with sr.Microphone(device_index = device_id, sample_rate = sample_rate,
chunk_size = chunk_size) as source:
#wait for a second to let the recognizer adjust the
#energy threshold based on the surrounding noise level
r.adjust_for_ambient_noise(source)
print "Say Something"
#listens for the user's input
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print "you said: " + text
#error occurs when google could not understand what was said
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google
Speech Recognition service; {0}".format(e))
将音频文件转录为文本
如果我们有一个要转换为文本的音频文件,我们只需要将源替换为音频文件而不是麦克风。
为了方便起见,请将音频文件和程序放在同一文件夹中。这适用于FLAC文件的WAV、AIFF。
实现如下所示
#Python 2.x program to transcribe an Audio file
import speech_recognition as sr
AUDIO_FILE = ("example.wav")
# use the audio file as the audio source
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
#reads the audio file. Here we use record instead of
#listen
audio = r.record(source)
try:
print("The audio file contains: " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech
Recognition service; {0}".format(e))
故障排除
通常会遇到以下问题
1)麦克风静音:这导致无法接收输入。要检查这一点,可以使用alsamixer。
它可以使用
sudo apt-get install libasound2 alsa-utils alsa-oss
键入amixer。输出将看起来像这样
Simple mixer control 'Master', 0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 65536
Mono:
Front Left: Playback 41855 [64%] [on]
Front Right: Playback 65536 [100%] [on]
Simple mixer control 'Capture', 0
Capabilities: cvolume cswitch cswitch-joined
Capture channels: Front Left - Front Right
Limits: Capture 0 - 65536
Front Left: Capture 0 [0%] [off] #switched off
Front Right: Capture 0 [0%] [off]
如您所见,捕获设备当前已关闭。要打开它,请键入alsamixer。
如您在第一张图片中所看到的,它正在显示我们的播放设备。按F4键切换到“捕获设备”。
在第二张图片中,突出显示的部分显示捕获设备已静音。要取消静音,请按空格键
如上图所示,突出显示的部分确认捕获设备未静音。
2)未选择当前麦克风作为捕获设备:
在这种情况下,可以通过键入alsamixer并选择声卡来设置麦克风。在这里,您可以选择默认的麦克风设备。
如图所示,突出显示部分是您必须选择声卡的地方。
第二张图片显示了声卡的屏幕选择
3)没有Internet连接:语音到文本的转换需要有效的Internet连接。
猜你喜欢
- 2024-10-12 如何实现语音转文字程序代码(语音转文字的代码)
- 2024-10-12 用树莓派实现会说话的汤姆猫(怎么用树莓派编程)
- 2024-10-12 为了方便跟学姐时刻视频,于是用Python做了个局域网视频工具!
- 2024-10-12 Python奇技淫巧之利用百度AI声控电脑关机!
- 2024-10-12 笑到颤抖的小游戏《不要停下来,八音符酱》,边撕边吼边泪奔
- 2024-10-12 使用Python实现虚拟助手(python 虚拟文件)
- 2024-10-12 实时语音识别 下载文字转语音API库
- 2024-10-12 音频转文字--我们选择faster-whisper
- 2024-10-12 利用Python实现录音播放并翻译,真正的实时进行翻译
- 2024-10-12 Python音频处理的新选择:深入探索PyAudioMixer库
你 发表评论:
欢迎- 最近发表
-
- 使用Python实现图片文件的加密与解密:保障隐私安全的实用方案
- 吴恩达官宣开源,yyds!(吴恩达rnn)
- Python自动化办公实战:效率提升10倍的脚本开发指南
- Android让视图显示未读消息数量(未读消息图标怎么显示)
- docker容器安装与部署,常用命令、容器卷、dockerfile,详细教程
- pytorch的一个最简单的cpp扩展(pytorch cdist)
- 如何编译.Net 6 Runtime源码(编译framework)
- 微软Windows 11将改版BSOD、强制登录账号才能安装
- windows环境下配置sphinx输出html文档
- 超详细windows安装配置WSL2(ubuntu20.04)步骤
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)