首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中使用关键字或唤醒字激活Tkinter窗口

在python中使用关键字或唤醒字激活Tkinter窗口
EN

Stack Overflow用户
提问于 2018-06-27 05:45:52
回答 2查看 232关注 0票数 0

我发现了一个很棒的唤醒词程序叫做豪猪。我得到了它的工作,并希望实现它到一个Tkinter窗口。这个想法是有一个开/关的状态,唤醒字打开它。

这个唤醒词将把我放入一个将创建其他框架和特征的循环中。我假设有一个线程化的解决方案,但我想不出来。

代码语言:javascript
复制
from Tkinter import *
import threading
import ttk
import os

import sys
sys.path.append('Porcupine/demo/python')

import porcupine__demo

class FullscreenWindow:

    def __init__(self):
        self.state = False
        self.tk = Tk()
        self.tk.configure(background='black')

        self.listen()

        print(self.state)

    def listen(self):
        self.state = porcupine_demo.listen_for_keyword()



if __name__ == '__main__':
    w = FullscreenWindow()
    w.tk.mainloop()

方法*.listen_for_keyword()是我编写的一个方法,它在捕获唤醒字时返回True。

EN

回答 2

Stack Overflow用户

发布于 2018-06-27 08:08:16

我发现了一篇令人惊叹的博客文章,它回答了这个问题。我已经更改了他们的代码以反映我的需求。该博客为here

代码语言:javascript
复制
from Tkinter import *
import threading

import sys
sys.path.append('Porcupine/demo/python')

import porcupine_demo

class App(threading.Thread):

    def __init__(self, tk_root):
        self.root = tk_root
        threading.Thread.__init__(self)
        self.start()

    def run(self):
        listening = True
        while listening:
            self.state = porcupine_demo.listen_for_keyword()
            if self.state:
                print("heard you:",self.state)
                LABEL = Label(self.root, text="Hello, world!")
                LABEL.pack()

ROOT = Tk()
ROOT.configure(background='black')
APP = App(ROOT)
ROOT.mainloop()
票数 1
EN

Stack Overflow用户

发布于 2018-06-27 08:23:01

豪猪包附带了如何在非阻塞模式下使用豪猪的演示,这是允许mainloop处理tkinter事件所必需的。关键是在音频流上附加一个回调。

我以前从来没有用过豪猪,所以我不知道它在现实世界中有多好用,但它在我的mac上可以用,而且不需要显式的线程。

代码语言:javascript
复制
import sys
sys.path.append("/tmp/Porcupine/binding/python")

import Tkinter as tk
import struct
from datetime import datetime
from porcupine import Porcupine
import pyaudio

library_path = "/tmp/Porcupine/lib/mac/x86_64/libpv_porcupine.dylib"
model_file_path = "/tmp/Porcupine/lib/common/porcupine_params.pv"
keyword_file_paths = ["/tmp/Porcupine/words_mac.ppn",]
num_keywords = len(keyword_file_paths)
sensitivities = [0.5] * num_keywords

class TkPorcupine(object):

    def __init__(self):
        self.initialize_ui()
        self.initialize_porcupine()

    def initialize_ui(self):
        self.state = False
        self.root = tk.Tk()
        self.text = tk.Text(self.root, width=60, height=8)
        self.vsb = tk.Scrollbar(self.root, orient="vertical", command=self.text.yview)
        self.text.configure(yscrollcommand=self.vsb.set)

        self.vsb.pack(side="right", fill="y")
        self.text.pack(fill="both", expand=True)

    def initialize_porcupine(self):
        self.porcupine = Porcupine(library_path, model_file_path,
                                   keyword_file_paths=keyword_file_paths,
                                   sensitivities=sensitivities)
        self.audio_stream = pyaudio.PyAudio().open(
            rate=self.porcupine.sample_rate,
            channels=1,
            format=pyaudio.paInt16,
            input=True,
            frames_per_buffer=self.porcupine.frame_length,
            input_device_index=None,
        stream_callback=self.audio_callback)
        self.audio_stream.start_stream()

    def audio_callback(self, in_data, frame_count, time_info, status):
        if frame_count >= self.porcupine.frame_length:
            pcm = struct.unpack_from("h" * self.porcupine.frame_length, in_data)
            result = self.porcupine.process(pcm)
            if num_keywords == 1 and result:
                message = '[%s] detected keyword' % str(datetime.now())
                self.text.insert("end", message + "\n")
                self.text.see("end")
            elif num_keywords > 1 and result >= 0:
                message = '[%s] detected keyword #%d' % (str(datetime.now()), result)
                self.text.insert("end", message + "\n")
                self.text.see("end")

        return None, pyaudio.paContinue


if __name__ == '__main__':
    demo = TkPorcupine()
    tk.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51051770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档