Jarvis重新营业

当然,每个人都梦见他的语音助手,这是著名电影中的另一种“贾维斯”的实现。

图片

很长一段时间以来,我一直没有离开过我的“ Jarvis”想法,也没有用声音控制房屋中的设备。最后,人们创造了这个奇迹。我不必长时间考虑“大脑”,Raspberry Pi非常适合。

因此,铁:

  • Raspberry Pi 3模型B
  • USB罗技相机

实作


我们的助手将按照Alexa / Hub的原则工作:

  1. 离线激活特定单词
  2. 认识云中的团队
  3. 运行命令
  4. 报告进度或告知请求信息

因为 我的相机是开箱即用的,不需要打扰驱动程序,所以我们立即进入软件部分。

离线激活


激活将使用CMU Sphinx进行,一切都会好起来,但是开箱即用的识别速度非常慢,超过10秒钟,这绝对不合适,要解决此问题,您需要清除不必要的单词词典。

安装所需的一切:

pip3 install SpeechRecognition
pip3 install pocketsphinx

进一步

sudo nano /usr/local/lib/python3.4/dist-packages/speech_recognition/pocketsphinx-data/en-US
/pronounciation-dictionary.dict

删除除我们需要的Jarvis之外的所有内容:

 jarvis JH AA R V AH S

现在,pocketsphinx可以很快识别出。

语音识别


最初,有一个使用Google服务的想法,除了对SpeechRecognition的支持外。但事实证明,谷歌为此花了钱,但无法与实体机配合使用。人。

幸运的是,Yandex还免费提供了这种机会,而且非常简单。

注册,获取API KEY。所有工作都可以通过curl'om完成。

curl -X POST -H "Content-Type: audio/x-wav" --data-binary "@file" «https://asr.yandex.net/asr_xml?uuid=ya_uid&key=yf_api_key&topic=queries»

语音合成


Yandex在这里将再次帮助我们。我们发送文本作为响应,我们收到包含合成文本的文件

curl «https://tts.voicetech.yandex.net/generate?format=wav&lang=ru-RU&speaker=zahar&emotion=good&key=ya_api_key» -G --data-urlencode "text=text" > file

贾维斯


将它们放在一起并获得这样的脚本。

#! /usr/bin/env python
# -*-coding:utf-8-*-
import os
import speech_recognition as sr
from xml.dom import minidom
import sys
import random

r = sr.Recognizer()
ya_uuid = ''
ya_api_key = ''


# os.system('echo "+ +" |festival --tts --language russian')


def convert_ya_asr_to_key():
    xmldoc = minidom.parse('./asr_answer.xml')
    itemlist = xmldoc.getElementsByTagName('variant')
    if len(itemlist) > 0:
        return itemlist[0].firstChild.nodeValue
    else:
        return False


def jarvis_on():
    with sr.WavFile("send.wav") as source:
        audio = r.record(source)

    try:
        t = r.recognize_sphinx(audio)
        print(t)
    except LookupError:
        print("Could not understand audio")

    return t == ("jarvis")


def jarvis_say(phrase):
    os.system(
        'curl "https://tts.voicetech.yandex.net/generate?format=wav&lang=ru-RU&speaker=zahar&emotion=good&key='+ya_api_key+'" -G --data-urlencode "text=' + phrase + '" > jarvis_speech.wav')
    os.system('aplay jarvis_speech.wav')


def jarvis_say_good():
    phrases = ["", "", "", "", "- ?", ]
    randitem = random.choice(phrases)
    jarvis_say(randitem)





try:
    while True:
        os.system('arecord -B --buffer-time=1000000 -f dat -r 16000 -d 3 -D plughw:1,0 send.wav')
        if jarvis_on():
            os.system('aplay jarvis_on.wav')
            os.system('arecord -B --buffer-time=1000000 -f dat -r 16000 -d 3 -D plughw:1,0 send.wav')
            os.system(
                'curl -X POST -H "Content-Type: audio/x-wav" --data-binary "@send.wav" "https://asr.yandex.net/asr_xml?uuid='+ya_uuid+'&key='+ya_api_key+'&topic=queries" > asr_answer.xml')
            command_key = convert_ya_asr_to_key()
            if (command_key):
                if (command_key in [‘key_word', ‘key_word1’, ‘key_word2']):
                    os.system(‘’)
                    jarvis_say_good()
                    continue

              

except Exception:
jarvis_say('-   ')

这是怎么回事。我们开始一个无休止的循环,记录三秒钟,如果在文件中找到单词“ jarvis”,则发送狮身人面像进行识别

 if jarvis_on():

播放预先录制的激活通知文件。

我们再次记录3秒,并将其发送给Yandex,作为回应,我们得到了我们的团队。接下来,我们根据命令执行操作。

仅此而已。执行脚本可以提出很多。

用例


现在我实际使用的一些例子

飞利浦色相


安装

pip install phue

在Hue应用程序中,设置一个静态IP:

图片

运行:


#!/usr/bin/python
import sys
from phue import Bridge

b = Bridge('192.168.0.100') # Enter bridge IP here.

#If running for the first time, press button on bridge and run with b.connect() uncommented
#b.connect()

print (b.get_scene())

我们以“ 470d4c3c8-on-0”的形式写出必要电路的ID

,该脚本的最终版本是:

#!/usr/bin/python
import sys
from phue import Bridge


b = Bridge('192.168.0.100') # Enter bridge IP here.

#If running for the first time, press button on bridge and run with b.connect() uncommented
#b.connect()


if (sys.argv[1] == 'off'):
    b.set_light([1,2,3],'on', False)
else:
    b.activate_scene(1,sys.argv[1])

在jarvis中添加:

                if (command_key in [' ', ' ', '']):
                    os.system('python3 /home/pi/smarthome/hue/hue.py a1167aa91-on-0')
                    jarvis_say_good()
                    continue

                if (command_key in [' ', ' ']):
                    os.system('python3 /home/pi/smarthome/hue/hue.py ac637e2f0-on-0')
                    jarvis_say_good()
                    continue

                if (command_key in [' ', ' ']):
                    os.system('python3 /home/pi/smarthome/hue/hue.py  "off"')
                    jarvis_say_good()
                    continue

LG电视


我们从这里获取脚本首次启动并输入配对代码后,该代码本身不会更改,因此您可以从脚本中删除此部分,只保留控件一个。

在jarvis中添加:

 
                #1 - POWER 
                #24 - VOLUNE_UP
                #25 - VOLUME_DOWN
                #400 - 3D_VIDEO
                if (command_key in [' ', ' ']):
                    os.system('python3 /home/pi/smarthome/TV/tv2.py 1')
                    jarvis_say_good()
                    continue

	        if (command_key in [‘ ', '']):
                    os.system('python3 /home/pi/smarthome/TV/tv2.py 24')
                    jarvis_say_good()
                    continue

广播电台


sudo apt-get install mpg123

在jarvis中添加:

 
               if (command_key in ['', ‘ ’,’ ’]):
                    os.system(‘mpg123 URL')
                    continue

如果您不向Jarvis喊叫, 您还可以设置Sbridge并通过Siri管理所有内容。

至于语音识别的质量,当然不是Alexa,而是在5米以内的肯定命中百分比是不错的。主要问题是来自电视/扬声器的语音与命令一起被记录,并且干扰识别。

就这样,谢谢。

Source: https://habr.com/ru/post/zh-CN401049/


All Articles