基于python和深度学习(语音识别、NLP)实现本地离线智能语音控制终端(带聊天功能和家居控制功能)

首先,我们来捋一下一个好的智能控制终端需要什么功能:

1.得随叫随到,不需要的时候就休眠,省电又省心

2.得会听,听得懂人的需求

3.得会控制智能家居,实现人的需求

4.还得会说,更好的与人进行交流和交互

5.得能和人进行聊天,进行交流

6.无论在有没有网络的条件下也能正常的为人类服务,容错率高

7.外加需求:能白嫖就白嫖,最好不花钱

基于以上需求,得出思路以下:

这里我主要实现自己训练出来的离线版本的,而使用各个互联网大厂API的联网版本的我这里将不使用,在网上有很多教程,我这里就不再详细说明了。

先看效果:

智能语音助手

这个项目的各个环节是我自己一个个逐个实现的,再通过一个主函数联系起来的;

可能做的有些瑕疵,但是主体运行是完全没有问题。

我们动手让来一个个功能实现。

一、学会休眠和语音唤醒

这里我以pocketsphinx实现语音唤醒为例子:

1.环境配置:

pip install pocketsphinx 
pip install pyaudio     

如果这样安装失败的,就需要我们到网上下载好指定whl文件,再进行离线安装。

https://www.lfd.uci.edu/~gohlke/pythonlibs/

到上面的那个网站找到 pocketsphinx 和  pyaudio  的whl文件,这里要注意选择对应  你的操作系统

  python 的版本,下载后放到项目文件夹下,在pycharm的终端中输入:

pip install 包的全名

进行本地离线安装。

比如:我在window上python3.7 进行离线安装pocketsphinx,这里要注意文件的路径,否则会报错找不到指定文件

pip install pocketsphinx-0.1.15-cp37-cp37m-win_amd64.whl

2.如何定制训练自己的唤醒词:

建立一个名为keyword的文件,里面输入你想要的唤醒词,以及读音相近的词(相似读音的词越多灵敏度越高),比如:我想训练的终端名为 COCO,则我的keyword内容为:

gogo
coco
yoyo
bobo
lolo
momo
nono
hoho

打开网站:Sphinx Knowledge Base Tool VERSION 3

 

 选择 keyword.txt 进行上传,得到对应的压缩包,下载后放到项目文件夹下,并解压得:

 这里的数字都是网站随机生成的,不一样是正常的。

测试代码:

import os
from pocketsphinx import LiveSpeech, get_model_path

def wakeup_co():
    model_path = get_model_path()
    speech = LiveSpeech(
        verbose=False,
        sampling_rate=16000,
        buffer_size=2048,
        no_search=False,
        full_utt=False,
        hmm=os.path.join(model_path, 'en-us'),
        lm=os.path.join('.\\Sphinx_keyword\\keyword_COCO\\', '5995.lm'),
        dic=os.path.join('.\\Sphinx_keyword\\keyword_COCO\\', '5995.dic')
    )
    for phrase in speech:
        #print("phrase:", phrase)
        #print(phrase.segments(detailed=True))
        if str(phrase) in ["GOGO", "COCO", "YOYO",
                           "BOBO", "LOLO", "MOMO",
                           "NONO", "HOHO"]:
              print('我是COCO')

这里 '.\\Sphinx_keyword\\keyword_COCO\\' 是我存放文件的地址,5995   也需要根据你的文件来更改,需要根据你的地址来更改。

测试结果:

Allocating 32 buffers of 2500 samples each
我是COCO

进程已结束,退出代码为 0

若想使用中文词作为唤醒词,则需要下载好相关的中文文件:

Download CMU Sphinx from SourceForge.net

下载后放到项目文件夹中解压,得:cmusphinx-zh-cn-5.2  文件夹

和训练英文唤醒词一样,需要建立  keyword.txt   文件,我训练了一个名为“佩佩”的唤醒词:

贝贝
佩佩
蕾蕾
内内
嘿嘿
忒忒
得得
贼贼
尅尅

 打开网站:Sphinx Knowledge Base Tool VERSION 3

上传keyword.txt后得到一个压缩包,下载后放到项目路径下并解压。

 这里需要更改 dic 为后缀的文件内容,需要在中文后面按格式添加上拼音声调,所有间隔均为一个空格,修改后,例如:

佩佩 p ei4 p ei3	
内内 n ei4 n ei3	
嘿嘿 h ei4 h ei3	
尅尅 k ei4 k ei3	
得得 d ei4 d ei3	
忒忒 t ei4 t ei3	
蕾蕾 l ei4 l ei3	
贝贝 b ei4 b ei3	
贼贼 z ei4 z ei3	

测试代码:

import os
from pocketsphinx import LiveSpeech, get_model_path

model_path = '.\\Sphinx_keyword\\cmusphinx-zh-cn-5.2\\'

speech = LiveSpeech(
    verbose=False,
    sampling_rate=16000,
    buffer_size=2048,
    no_search=False,
    full_utt=False,
    hmm=os.path.join(model_path ,'zh_cn.cd_cont_5000'),
    lm=os.path.join('.\\Sphinx_keyword\\keyword_PeiPei\\', '0738.lm'),
    dic=os.path.join('.\\Sphinx_keyword\\keyword_PeiPei\\', '0738.dic')
)
for phrase in speech:
    print("phrase:", phrase)
    print(phrase.segments(detailed=True))
    if str(phrase) in ["贝贝", "佩佩", "蕾蕾",
                       "内内", "嘿嘿", "忒忒",
                       "得得", "贼贼", "尅尅",]:
        print("我是佩佩")

这里  model_path 的路径需要  指向解压后中文文件夹  cmusphinx-zh-cn-5.2  里面的 zh_cn.cd_cont_5000  文件夹。

'.\\Sphinx_keyword\\keyword_PeiPei\\'  和  0738    需要根据你的路径和文件名来修改指向你下载解压好的文件。

测试结果:

Allocating 32 buffers of 2500 samples each
phrase: 尅尅
[('<s>', 0, 4359242, 4359325), ('<sil>', -1331, 4359326, 4359479), ('<sil>', -1331, 4359480, 4359801), ('<sil>', -1331, 4359802, 4359940), ('尅尅', 0, 4359941, 4360080)]
我是佩佩
phrase: 得得
[('<s>', 0, 8516377, 8516528), ('<sil>', -5375, 8516529, 8516683), ('<sil>', -2035, 8516684, 8516764), ('得得', 0, 8516765, 8516968), ('</s>', 0, 8516969, 8516980)]
我是佩佩
phrase: 嘿嘿
[('<s>', 0, 10674834, 10675304), ('嘿嘿', -3628, 10675305, 10675382), ('</s>', 0, 10675383, 10675385)]
我是佩佩

中英两种唤醒词使用一个就可以了,英文版的比较灵敏,中文版的可能比较迟钝,所以这里我还是推荐训练使用英文版的,快捷且灵敏。

二、学会听

这里我基于pytorchspeechbrain的预训练模型,训练得出一个中文语音识别系统,

可以实现中文语音转转化为文字输出。

1.基本的环境配置,后面的缺什么就 pip 什么

pip install speechbrain
pip install SoundFile
pip install sox
pip install speech_recognition

2.实现语音的接收并保存为wav文件

这里基于speech_recognition包进行实现。

在读取语音等到没有语音输入时,自动停止并保存。

测试代码:

import speech_recognition as sr   #pyaudio SpeechRecognition模块

from myself_word_to_voice import speakout

def rec(rate=16000):     #从系统麦克风拾取音频数据,采样率为 16000
    r = sr.Recognizer()
    with sr.Microphone(sample_rate=rate) as source:
        sayword = 'coco在听呢'
        print(sayword)  #这里会打印please say something,提示你说话进行录音
        audio = r.listen(source)

    with open("recording.wav", "wb") as f:   #把采集到的音频数据以 wav 格式保存在当前目录下的recording.wav 文件
        f.write(audio.get_wav_data())
        print('您说的我已经收到了')
    return 1

rec()

测试结果:打开项目文件夹,直接双击打开recording.wav文件即可听到你说的话。

3.接收好中文语音的wav文件读取转化为文本输出

这里我给出官网和github的地址,各位可以自行尝试:

SpeechBrain: A PyTorch Speech Toolkit

GitHub - speechbrain/speechbrain: A PyTorch-based Speech Toolkit

 这里我选择的是普通话,下载好预训练模型后,网站里也有教程,各位可以自行尝试训练使用。

这里我直接给出实现的码源和模型,配置好环境后可以直接使用。

SpeechBrain(中文语音识别).zip-深度学习文档类资源-CSDN下载

 测试结果:

The torchaudio backend is switched to 'soundfile'. Note that 'sox_io' is not supported on Windows.
The torchaudio backend is switched to 'soundfile'. Note that 'sox_io' is not supported on Windows.
start...
有奶会有的面包括有的一切都会有的调解里数和语音可以达到更好的效果

进程已结束,退出代码为 0

三、学会聊天

这里我基于chatterbot和第三方的语义库,高度定制化地 训练自己的机器人聊天对话系统

1.环境配置:

pip install chatterbot
pip install chatterbot_corpus

可能的报错:

OSError: [E053] Could not read config.cfg from C:\Users\pc\AppData\Local\Programs\Python\Python38\Lib\site-packages\en_core_web_md\en_core_web_md-2.2.5\config.cfg。

解决办法:

pip uninstall spacy
pip install -i https://pypi.douban.com/simple spacy==2.2.2

2.尝试训练官方的中文数据集并使用

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
import logging

'''
This is an example showing how to train a chat bot using the
ChatterBot Corpus of conversation dialog.
'''

# Enable info level logging
# logging.basicConfig(level=logging.INFO)
chatbot = ChatBot('Example Bot')

# Start by training our bot with the ChatterBot corpus data
trainer = ChatterBotCorpusTrainer(chatbot)

def train():
    trainer.train(
        'chatterbot.corpus.chinese'
    )

def chat(word = ''):
    word = chatbot.get_response(word)
    return word

def test1():
    train()
    while 1:
        print(chatbot.get_response(input(">")))

test1()

若无报错则,可以继续下一步,训练自己的数据集,实现高度定制化。

3.我提供一个数据集:

corpus.txt用于训练自己的聊天机器人-深度学习文档类资源-CSDN下载

大概长这样:

 下载后,在你的项目文件夹中建立一个名为 corpus  的文件夹,把下载好的  corpus.txt 放进去。

进行训练,代码:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.trainers import ChatterBotCorpusTrainer


# 构建ChatBot并指定Adapter
my_bot = ChatBot(
    'COCO',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch',
            'threshold': 0.65,#低于置信度,则默认回答
            'default_response':'coco没听懂'
        }
    ]
)


def train_myword():
    file = open("./corpus/corpus.txt", 'r', encoding='utf-8')
    corpus = []
    print('开始加载语料!')
    # 导入语料库
    while 1:
        try:
            line = file.readline()
            if not line:
                break
            if line == '===\n':
                continue
            temp = line.strip('\n')
            # print(temp)
            corpus.append(temp)
        except:
            pass
    file.close()
    print('语料加载完毕')
    print('》'*30)

    #my_bot = ChatBot("coco")
    #my_bot.set_trainer(ListTrainer)
    trainer = ListTrainer(my_bot)
    print('开始训练!')
    trainer.train(corpus[:10000])
    print('训练完毕!')

def chat1():
    while True:
        print(my_bot.get_response(input("user:")))

def chat_my(word = ''):
    word = my_bot.get_response(word)
    return word

def test1():
    train_myword()
    chat1()

test1()

训练完毕:

开始加载语料!
语料加载完毕
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
开始训练!
List Trainer: [####################] 100%
训练完毕!

这里我只训练语库的前10000条对话,建议不要训练太多的条对话,贪多嚼不烂,就算训练出来了也有很高的回复延迟,甚至直接无法运行回复,非常影响用户体验。

4.进行数学运算和时间查询:

# -*- coding: utf-8 -*-
from chatterbot import ChatBot

bot = ChatBot(
    "Math & Time Bot",
    logic_adapters=[
        "chatterbot.logic.MathematicalEvaluation",
        "chatterbot.logic.TimeLogicAdapter"
    ],
    input_adapter="chatterbot.input.VariableInputTypeAdapter",
    output_adapter="chatterbot.output.OutputAdapter"
)


def chot_math_time(text=''):
    response = bot.get_response(text)
    return response

print(chot_math_time('what is 1 + 1'))
print(chot_math_time('现在几点了'))

结果:

1 + 1 = 2
The current time is 05:32 PM

进程已结束,退出代码为 0

由于该模块只支持英文,我们使用中文语音时,需要将语音中的数字进行剥离,具体代码如下:

def Split_num_letters(astr):
    nums = []
    astr = astr +'无'
    num1 = ''
    for i in range(len(astr)-1):
        if astr[i].isdigit()== True and astr[i+1].isdigit()==False:
            nums.append(num1)
            num1 = ''
        elif astr[i].isdigit() == False and astr[i+1].isdigit() == True:
            num1 = num1 + astr[i+1]
        elif astr[i].isdigit() == True and astr[i+1].isdigit() ==True:
                num1 = num1 + astr[i+1]
    if astr[0].isdigit():
        nums[0] = astr[0] + nums[0]
    print(nums)
    return nums


Split_num_letters('你知道120乘20等于多少吗')
Split_num_letters('120乘20等于多少吗')

结果: 该函数配合其他函数使用,即可实现语音识别进行简单运算

['120', '20']

进程已结束,退出代码为 0

四、学会控制智能家电

基本原理:在arduino配置好代码,连接好电路,通过python向arduino在一定情况下传输某个信号,arduino在接收到指定信号时,执行指定动作。

1.环境配置:

pip install pyserial

2.实现python控制arduino

这里可以参考我的另一篇文章:

python与arduino通讯(windows和linux)_Leonard2021的博客-CSDN博客_树莓派和arduino通讯

这样来实现python与arduino的交互,arduino可以控制众多的电器,比如:灯,舵机,风扇等,通过舵机的旋转实现可以开关门,打开关闭各种大型电器的电闸等等;

arduino的众多传感器配件也可以为  智能语音系统 提供相关数据,比如:空气湿度,温度等,让它能更好的来控制相关的电器,让实现智能家居一体化控制。

有很大的想象空间和发展空间,这里我仅仅实现通过语音控制arduino自带的LED灯的开关,其他的控制只需要以相同的原理配置即可。

a.python的代码:

import serial  # 导入串口通讯库
import time

def try2():
    ser = serial.Serial("COM3", 9600, timeout=1)
    c = ''
    while 1:

        wakeup_co() #语音唤醒
        rec()  #将语音转化为wav文件
        listenword = listen() #将wav文件中的语音转化为中文文字 
        #这三个都是上面给出了的,需要自己命名后,导入

        if '灯' in listenword and '开' in listenword:
            c = '1'
        elif '灯' in listenword and '关' in listenword:
            c = '0'
        if (c == '0'):
            ser.write('0'.encode('utf-8'))
        if (c == '1'):
            ser.write('1'.encode('utf-8'))

try2()

b.arduino的代码:

void setup(){
  Serial.begin(9600);
  pinMode(13,OUTPUT);//设置13号端口作为输出端口
  //digitalWrite(13,HIGH);
}
char var;
void loop(){
  while(Serial.available()>0)
  {
      var=Serial.read();
      if(var == '0'){
        digitalWrite(13,LOW);
      }
      if(var== '1'){
        digitalWrite(13,HIGH);
      }
  }
}

五、学会说

这里我基于pyttsx3实现了文字转语音,让智能终端学会“说”

1.环境配置:

pip install pyttsx3

2.代码实现

import pyttsx3

def speakout(workText):
        # 初始化语音
        engine = pyttsx3.init()  # 初始化语音库
        # 设置语速
        rate = engine.getProperty('rate')
        engine.setProperty('rate', rate - 50)
        # 输出语音
        engine.say(workText)  # 合成语音
        engine.runAndWait()
        
speakout('你好')

测试结果:听到一个有点别扭的女声,说了句“你好”。这里也可以自行调节参数,让语音听起来更舒服一点。

这里我给出我的完整码源,需要的可以自取,理解地来使用:

温馨提示,需要先接入arduino版,配置完端口后,再运行,否则会报错,也可以先注释掉arduino与python通讯的代码,先尝试其他功能。

自制的智能语音助手.zip-深度学习文档类资源-CSDN下载

如果本文对你有帮助,欢迎一键三连!!!

  • 18
    点赞
  • 134
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
《唐宇迪 深度学习-语音识别项目实战(python版)》是一本研究深度学习应用于语音识别的实战指南。在传统的语音识别技术中,主要采用了基于统计模型的方法,而深度学习的出现在语音识别领域产生了巨大的影响。 该书将深度学习技术应用于语音识别领域的实际项目,以Python语言为工具,深入探讨了从数据准备到模型设计、训练和应用的全过程。它以实战为基础,通过大量的示例代码和实际案例,帮助读者更好地理解和应用深度学习语音识别中的优势和挑战。 本书首先介绍了语音识别的基本概念和流程,并详细解释了深度学习语音识别中的应用原理。随后,它引导读者使用Python语言实现基于深度学习语音识别模型,包括搭建神经网络模型、数据预处理、训练和评估模型等步骤。 此外,该书还介绍了一些常用的深度学习框架和工具,如Keras、TensorFlow和pytorch,帮助读者更好地使用这些工具来构建和优化语音识别模型。同时,书中还介绍了一些与语音识别相关的技术,如语音端点检测和声学建模,以帮助读者更全面地理解和应用深度学习语音识别中的方法。 总的来说,《唐宇迪 深度学习-语音识别项目实战(python版)》是一本深入浅出的实战指南,适合对深度学习语音识别感兴趣的读者学习和参考。通过实际项目案例的介绍和示例代码的演示,读者可以更好地理解和应用深度学习语音识别中的技术和方法。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leonard2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值