• python实现语音识别


    1. 首先安装依赖库

    1. pip install playsound # 该库用于播放音频文件
    2. pip install speech_recognition # 该库用于语音识别
    3. pip install PocketSphinx # 语音识别模块中只有sphinx支持离线的,使用该模块需单独安装
    4. pip install pyttsx3 # 该库用于将文本转换为语音播放
    5. pip install comtypes # 该库可以从文本文件中获取输入转换为语音文件

    2. 播放音频文件 

    1. from playsound import playsound
    2. playsound('audio_files\cnhello.mp3')

    3. 语音识别

    默认只识别英文,如果需要支持中文,需要下载中文模型包,下载地址如下:

    CMU Sphinx - Browse /Acoustic and Language Models at SourceForge.net 

    下载完解压到sphinx安装路径下:

    D:\install\Anaconda\Lib\site-packages\speech_recognition\pocketsphinx-data

     

    1. import speech_recognition as sr
    2. r = sr.Recognizer()
    3. harvard = sr.AudioFile('audio_files\harvard.wav')
    4. with harvard as source:
    5. # r.adjust_for_ambient_noise(source) # 消除环境背景音
    6. audio = r.record(source) # record()函数,将整个音频文件读入AudioData实例
    7. print(type(audio))
    8. r.recognize_sphinx(audio)

    4. 通过麦克风输入并识别

    1. import speech_recognition as sr
    2. mic = sr.Microphone()
    3. with mic as source:
    4. r.adjust_for_ambient_noise(source)
    5. audio = r.listen(source)
    6. r.recognize_sphinx(audio)

    5. 文本转语音播放

    1. import pyttsx3
    2. engine = pyttsx3.init()
    3. engine.say("hello world")
    4. engine.say("你好")
    5. engine.runAndWait()
    6. engine.stop()

    6. 文本转语音

    1. # 文本转语音
    2. from comtypes.client import CreateObject
    3. from comtypes.gen import SpeechLib
    4. engine = CreateObject("SAPI.SpVoice")
    5. stream = CreateObject('SAPI.SpFileStream')
    6. infile = 'demo.txt'
    7. outfile = 'demo_audio.wav'
    8. stream.open(outfile, SpeechLib.SSFMCreateForWrite)
    9. engine.AudioOutputStream = stream
    10. f = open(infile, 'r', encoding='utf-8')
    11. theText = f.read()
    12. f.close()
    13. engine.speak(theText)
    14. stream.close()

    7. 语音转文本(英文识别)

    1. # 语音文件转文本文件
    2. import speech_recognition as sr
    3. r = sr.Recognizer()
    4. harvard = sr.AudioFile('demo_audio.wav')
    5. with harvard as source:
    6. # r.adjust_for_ambient_noise(source)
    7. audio = r.record(source)
    8. r.recognize_sphinx(audio, language='en-US')

    >>'hello everyone my name is bob'

    8. 语音转文本(中文识别)

    1. # 语音文件转文本文件
    2. import speech_recognition as sr
    3. r = sr.Recognizer()
    4. harvard = sr.AudioFile('demo_audio.wav')
    5. with harvard as source:
    6. # r.adjust_for_ambient_noise(source)
    7. audio = r.record(source)
    8. r.recognize_sphinx(audio, language='zh-CN')

     >> '好好 学习 天天 向上'

    参考:

    python实现语音识别功能

    从0开始语音识别

  • 相关阅读:
    IDEA中添加servlet模板
    Hadoop3:MapReduce中的Reduce Join和Map Join
    04_一个字符串是否包含多个子字符串判断
    AWS SAA-C03 #152
    C,C++网络编程学习指南
    【前端验证】被动响应型uvm_model环境搭建——以握手型ram_model为例
    zabbix自定义监控
    原型模式 创建型模式之二
    Stable diffusion采样器详解
    上架即封神!3.6k Star 的开源游戏模拟器,Delta 冲上 App Store 免费榜
  • 原文地址:https://blog.csdn.net/qq_24946843/article/details/132544142