Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

133
LINES

< > BotCompany Repo | #1009578 // Linux: Hot Word Detection ["Computer!"] + "Eve" Icon - using PocketSphinx + Python wrapper

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 4079K of libraries. Click here for Pure Java version (7992L/59K/199K).

1  
!7
2  
3  
sS keyphrase = "computer";
4  
// bigger number means more tolerance
5  
// 1e-40 works pretty well with few false positives
6  
// 1e-30 gets no hits
7  
// 1e-35 doesn't trigger reliably
8  
// Using 1e-39 for now
9  
sS threshold = "1e-39";
10  
11  
// seconds to listen after hot word
12  
// TODO: keep listening when user said something
13  
static int recognitionTimeout = 10;
14  
15  
sS icon = /*#1009669*/ #1009667; //#1009662;
16  
17  
// milliseconds before starting recognition (time for saying "yep" or "ja")
18  
static int delayBeforeRecognition = 300;
19  
20  
sO onKeyPhrase;
21  
sbool exited;
22  
23  
sS python = trim([[
24  
#!/usr/bin/python
25  
from subprocess import Popen, PIPE, STDOUT
26  
import pty
27  
import os
28  
import sys
29  
30  
cmd = 'pocketsphinx_continuous -inmic yes -keyphrase #KEYPHRASE# -kws_threshold #THRESHOLD#'
31  
32  
master, slave = pty.openpty()
33  
34  
p = Popen(cmd, shell=True, stdin=PIPE, stdout=slave, stderr=slave, close_fds=True)
35  
stdout = os.fdopen(master)
36  
while True:
37  
    sys.stdout.write(stdout.readline())
38  
    sys.stdout.flush()
39  
]]);
40  
41  
static File fPython;
42  
static JButton btnStartStop;
43  
sbool paused;
44  
45  
p-subst {
46  
  if (!isLinux()) fail("Get Linux!");
47  
  load('paused);
48  
  
49  
  //onLeftClick(installTrayIcon(icon, "Computer!"), f jump_blip);
50  
  TrayIcon trayIcon = onLeftClick(
51  
    installTrayIcon2(resizeImage(loadImage2(icon), 24), "Computer!",
52  
      makePopupMenu(menuItem("Computer!", f jump_blip))),
53  
      f jump_blip);
54  
      
55  
  trayIcon.displayMessage(
56  
    "", "Click on EVE to activate the assistant!",
57  
    TrayIcon.MessageType.INFO/*NONE*/);
58  
  
59  
  makeBot("Hot Word Detection.");
60  
  unix_killAll("pocketsphinx");
61  
  
62  
  print("Key phrase is: " + keyphrase);
63  
  
64  
  onKeyPhrase = voidfunc(S s) { jump() };
65  
  
66  
  swing {
67  
    showControls(jcenteredline(btnStartStop = jbutton("Listen for hot word", f startStop)));
68  
  }
69  
  
70  
  fPython = getProgramFile("pocketsphinx.py");
71  
  makeExecutable(saveTextFile(fPython, python.replace("#THRESHOLD#", threshold).replace("#KEYPHRASE#", bashQuote(keyphrase))));
72  
  
73  
  titleStatus_setBaseTitle(consoleFrame(), "Hot Word Detection");
74  
  if (!paused) {
75  
    infoBox("Say " + quote(keyphrase) + " to activate me!");
76  
    start();
77  
  }
78  
  
79  
  makeBot("Hot Word Detection.");
80  
}
81  
82  
svoid start {
83  
  setText(btnStartStop, "Don't listen for hot word");
84  
  thread {
85  
    backtickToConsole_lineBuf(f2s(fPython), voidfunc(S line) {
86  
      print("Line read: " + quote(line));
87  
      if (eq(line, keyphrase))
88  
        pcallF(onKeyPhrase, line);
89  
      else if (startsWith(line, "READY."))
90  
        consoleStatus("Ready");
91  
      else if (startsWith(line, "Listening."))
92  
        consoleStatus("Listening");
93  
    });
94  
    setAndSave(paused := true);
95  
    setText(btnStartStop, "Listen for hot word");
96  
    consoleStatus("Stopped");
97  
  }
98  
}
99  
100  
svoid cleanMeUp {
101  
  if (!exited) { exited = true; unix_killAll("pocketsphinx"); }
102  
}
103  
104  
svoid startStop {
105  
  if (paused) {
106  
    setAndSave(paused := false);
107  
    start();
108  
  } else {
109  
    setAndSave(paused := true);
110  
    setText(btnStartStop, "Listen for hot word");
111  
    unix_killAll("pocketsphinx");
112  
  }
113  
}
114  
115  
svoid jump_blip {
116  
  thread {
117  
    infoBox(botAnswerWord());
118  
    blipAndWait();
119  
    sendOpt("Chrome Speech.", "start recognition timeout " + recognitionTimeout);
120  
  }
121  
}
122  
  
123  
svoid jump {
124  
  fS response = botAnswerWord();
125  
  infoBox(response);
126  
  thread { sleep(delayBeforeRecognition); sendOpt("Chrome Speech.", "start recognition timeout " + recognitionTimeout); }
127  
  thread { sendOpt("Mouth.", response); }
128  
}
129  
130  
answer {
131  
  if "listen to hot word" { if (paused) startStop(); ret "OK"; }
132  
  if "don't listen to hot word" { if (!paused) startStop(); ret "OK"; }
133  
}

Author comment

Began life as a copy of #1009577

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1009578
Snippet name: Linux: Hot Word Detection ["Computer!"] + "Eve" Icon - using PocketSphinx + Python wrapper
Eternal ID of this version: #1009578/83
Text MD5: df5bdc785b81a9b59c953a9172ea03bd
Transpilation MD5: 7d0b7081e9d435b7200d79b1df3cf617
Author: stefan
Category: javax / speech
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-19 19:21:54
Source code size: 3643 bytes / 133 lines
Pitched / IR pitched: No / No
Views / Downloads: 862 / 1944
Version history: 82 change(s)
Referenced in: [show references]