Python语音识别基础实践-SpeechRecognition

声明:参考B站视频,自学成长记录
https://www.bilibili.com/video/BV1Jk4y1R7a5?p=2
并参考博客:https://blog.csdn.net/Datapad/article/details/82970253

安装SpeechRecognition

C:\Users\Administrator>pip3 install SpeechRecognition
......
Installing collected packages: SpeechRecognition
Successfully installed SpeechRecognition-3.8.1

安装成功入下图

在这里插入图片描述


安装pocketsphinx

C:\Users\Administrator>pip install pocketsphinx
......
Installing collected packages: pocketsphinx
Successfully installed pocketsphinx-0.1.15

安装成功入下图

在这里插入图片描述


代码示例

实现将wav格式的语音文件读取并进行整体 / 部分识别

import speech_recognition as sr

r = sr.Recognizer()     # 调用识别器
harvard = sr.AudioFile('E:\speek\harvard.wav')      # 导入语音文件

# 上下文管理器打开文件并读取其内容
with harvard as source:
    all_audio = r.record(source)    # 使用record()从文件中捕获数据

# 查看类型
print(type(all_audio))      # <class 'speech_recognition.AudioData'>

all_text = r.recognize_sphinx(all_audio)    # 识别输出
print(all_text)
# this they'll smell of old we're lingers it takes heat to
# bring out the odor called it restores health and zest
# case all the colt is fine with him couples all pastore
# my favorite is as full food is the hot cross mon


# 识别部分文件并输出
with harvard as source:
    # 分割视频文件   指定偏移量及持续时间
    audio = r.record(source, offset=4, duration=3)  # 从第4秒开始,持续时间3秒

text = r.recognize_sphinx(audio)    # 识别输出
print(text)     # it takes heat to bring out the odor

注意事项

1、预先知道音频文件中语音的结构,那么offset和duration关键字参数对于分割音频文件非常有用。然而,匆忙使用它们会导致转录不良
2、音频文件类型以PCM WAV、AIFF/AIFF- c或本机FLAC读取音频文件,不然会报错
3、声音文件链接:https://pan.baidu.com/s/10oClt_NWgjOsDmIPuqQGzg 提取码:0wv4

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值