Commit b3c6d369 authored by jezra's avatar jezra

Python3, and better language updating

parent 5cf06444
This diff is collapsed.
import sys
import gi
import sys, gi
gi.require_version('Gtk', '3.0')
from gi.repository import GObject, Gtk
class UI(GObject.GObject):
__gsignals__ = {
'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}
def __init__(self,args, continuous):
GObject.GObject.__init__(self)
self.continuous = continuous
self.statusicon = Gtk.StatusIcon()
self.statusicon.set_title("Blather")
self.statusicon.set_name("Blather")
self.statusicon.set_tooltip_text("Blather - Idle")
self.statusicon.set_has_tooltip(True)
self.statusicon.connect("activate", self.continuous_toggle)
self.statusicon.connect("popup-menu", self.popup_menu)
self.menu = Gtk.Menu()
self.menu_listen = Gtk.MenuItem('Listen')
self.menu_continuous = Gtk.CheckMenuItem('Continuous')
self.menu_quit = Gtk.MenuItem('Quit')
self.menu.append(self.menu_listen)
self.menu.append(self.menu_continuous)
self.menu.append(self.menu_quit)
self.menu_listen.connect("activate", self.toggle_listen)
self.menu_continuous.connect("toggled", self.toggle_continuous)
self.menu_quit.connect("activate", self.quit)
self.menu.show_all()
def continuous_toggle(self, item):
checked = self.menu_continuous.get_active()
self.menu_continuous.set_active(not checked)
def toggle_continuous(self, item):
checked = self.menu_continuous.get_active()
self.menu_listen.set_sensitive(not checked)
if checked:
self.menu_listen.set_label("Listen")
self.emit('command', "continuous_listen")
self.statusicon.set_tooltip_text("Blather - Listening")
self.set_icon_active()
else:
self.set_icon_inactive()
self.statusicon.set_tooltip_text("Blather - Idle")
self.emit('command', "continuous_stop")
def toggle_listen(self, item):
val = self.menu_listen.get_label()
if val == "Listen":
self.emit("command", "listen")
self.menu_listen.set_label("Stop")
self.statusicon.set_tooltip_text("Blather - Listening")
self.set_icon_active()
else:
self.icon_inactive()
self.menu_listen.set_label("Listen")
self.emit("command", "stop")
self.statusicon.set_tooltip_text("Blather - Idle")
def popup_menu(self, item, button, time):
self.menu.popup(None, None, None, None, button, time)
def run(self):
#set the icon
self.set_icon_inactive()
if self.continuous:
self.menu_continuous.set_active(True)
self.set_icon_active()
else:
self.menu_continuous.set_active(False)
self.statusicon.set_visible(True)
def quit(self, item):
self.statusicon.set_visible(False)
self.emit("command", "quit")
def finished(self, text):
if not self.menu_continuous.get_active():
self.menu_listen.set_label("Listen")
self.set_icon_inactive()
self.statusicon.set_tooltip_text("Blather - Idle")
def set_icon_active_asset(self, i):
self.icon_active = i
def set_icon_inactive_asset(self, i):
self.icon_inactive = i
def set_icon_active(self):
self.statusicon.set_from_file( self.icon_active )
def set_icon_inactive(self):
self.statusicon.set_from_file( self.icon_inactive )
__gsignals__ = {
'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}
def __init__(self,args, continuous):
GObject.GObject.__init__(self)
self.continuous = continuous
self.statusicon = Gtk.StatusIcon()
self.statusicon.set_title("Blather")
self.statusicon.set_name("Blather")
self.statusicon.set_tooltip_text("Blather - Idle")
self.statusicon.set_has_tooltip(True)
self.statusicon.connect("activate", self.continuous_toggle)
self.statusicon.connect("popup-menu", self.popup_menu)
self.menu = Gtk.Menu()
self.menu_listen = Gtk.MenuItem('Listen')
self.menu_continuous = Gtk.CheckMenuItem('Continuous')
self.menu_quit = Gtk.MenuItem('Quit')
self.menu.append(self.menu_listen)
self.menu.append(self.menu_continuous)
self.menu.append(self.menu_quit)
self.menu_listen.connect("activate", self.toggle_listen)
self.menu_continuous.connect("toggled", self.toggle_continuous)
self.menu_quit.connect("activate", self.quit)
self.menu.show_all()
def continuous_toggle(self, item):
checked = self.menu_continuous.get_active()
self.menu_continuous.set_active(not checked)
def toggle_continuous(self, item):
checked = self.menu_continuous.get_active()
self.menu_listen.set_sensitive(not checked)
if checked:
self.menu_listen.set_label("Listen")
self.emit('command', "continuous_listen")
self.statusicon.set_tooltip_text("Blather - Listening")
self.set_icon_active()
else:
self.set_icon_inactive()
self.statusicon.set_tooltip_text("Blather - Idle")
self.emit('command', "continuous_stop")
def toggle_listen(self, item):
val = self.menu_listen.get_label()
if val == "Listen":
self.emit("command", "listen")
self.menu_listen.set_label("Stop")
self.statusicon.set_tooltip_text("Blather - Listening")
self.set_icon_active()
else:
self.icon_inactive()
self.menu_listen.set_label("Listen")
self.emit("command", "stop")
self.statusicon.set_tooltip_text("Blather - Idle")
def popup_menu(self, item, button, time):
self.menu.popup(None, None, None, None, button, time)
def run(self):
#set the icon
self.set_icon_inactive()
if self.continuous:
self.menu_continuous.set_active(True)
self.set_icon_active()
else:
self.menu_continuous.set_active(False)
self.statusicon.set_visible(True)
def quit(self, item):
self.statusicon.set_visible(False)
self.emit("command", "quit")
def finished(self, text):
if not self.menu_continuous.get_active():
self.menu_listen.set_label("Listen")
self.set_icon_inactive()
self.statusicon.set_tooltip_text("Blather - Idle")
def set_icon_active_asset(self, i):
self.icon_active = i
def set_icon_inactive_asset(self, i):
self.icon_inactive = i
def set_icon_active(self):
self.statusicon.set_from_file( self.icon_active )
def set_icon_inactive(self):
self.statusicon.set_from_file( self.icon_inactive )
......@@ -8,104 +8,104 @@ gi.require_version('Gtk', '3.0')
from gi.repository import GObject, Gtk, Gdk
class UI(GObject.GObject):
__gsignals__ = {
'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}
def __init__(self,args, continuous):
GObject.GObject.__init__(self)
self.continuous = continuous
#make a window
self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
self.window.connect("delete_event", self.delete_event)
#give the window a name
self.window.set_title("BlatherGtk")
self.window.set_resizable(False)
layout = Gtk.VBox()
self.window.add(layout)
#make a listen/stop button
self.lsbutton = Gtk.Button("Listen")
layout.add(self.lsbutton)
#make a continuous button
self.ccheckbox = Gtk.CheckButton("Continuous Listen")
layout.add(self.ccheckbox)
#connect the buttons
self.lsbutton.connect("clicked",self.lsbutton_clicked)
self.ccheckbox.connect("clicked",self.ccheckbox_clicked)
#add a label to the UI to display the last command
self.label = Gtk.Label()
layout.add(self.label)
#create an accellerator group for this window
accel = Gtk.AccelGroup()
#add the ctrl+q to quit
accel.connect(Gdk.KEY_Q, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE, self.accel_quit )
#lock the group
accel.lock()
#add the group to the window
self.window.add_accel_group(accel)
def ccheckbox_clicked(self, widget):
checked = self.ccheckbox.get_active()
self.lsbutton.set_sensitive(not checked)
if checked:
self.lsbutton_stopped()
self.emit('command', "continuous_listen")
self.set_icon_active()
else:
self.emit('command', "continuous_stop")
self.set_icon_inactive()
def lsbutton_stopped(self):
self.lsbutton.set_label("Listen")
def lsbutton_clicked(self, button):
val = self.lsbutton.get_label()
if val == "Listen":
self.emit("command", "listen")
self.lsbutton.set_label("Stop")
#clear the label
self.label.set_text("")
self.set_icon_active()
else:
self.lsbutton_stopped()
self.emit("command", "stop")
self.set_icon_inactive()
def run(self):
#set the default icon
self.set_icon_inactive()
self.window.show_all()
if self.continuous:
self.set_icon_active()
self.ccheckbox.set_active(True)
def accel_quit(self, accel_group, acceleratable, keyval, modifier):
self.emit("command", "quit")
def delete_event(self, x, y ):
self.emit("command", "quit")
def finished(self, text):
#if the continuous isn't pressed
if not self.ccheckbox.get_active():
self.lsbutton_stopped()
self.set_icon_inactive()
self.label.set_text(text)
def set_icon_active_asset(self, i):
self.icon_active = i
def set_icon_inactive_asset(self, i):
self.icon_inactive = i
def set_icon_active(self):
Gtk.Window.set_default_icon_from_file(self.icon_active)
def set_icon_inactive(self):
Gtk.Window.set_default_icon_from_file(self.icon_inactive)
__gsignals__ = {
'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}
def __init__(self,args, continuous):
GObject.GObject.__init__(self)
self.continuous = continuous
#make a window
self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
self.window.connect("delete_event", self.delete_event)
#give the window a name
self.window.set_title("BlatherGtk")
self.window.set_resizable(False)
layout = Gtk.VBox()
self.window.add(layout)
#make a listen/stop button
self.lsbutton = Gtk.Button("Listen")
layout.add(self.lsbutton)
#make a continuous button
self.ccheckbox = Gtk.CheckButton("Continuous Listen")
layout.add(self.ccheckbox)
#connect the buttons
self.lsbutton.connect("clicked",self.lsbutton_clicked)
self.ccheckbox.connect("clicked",self.ccheckbox_clicked)
#add a label to the UI to display the last command
self.label = Gtk.Label()
layout.add(self.label)
#create an accellerator group for this window
accel = Gtk.AccelGroup()
#add the ctrl+q to quit
accel.connect(Gdk.KEY_Q, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE, self.accel_quit )
#lock the group
accel.lock()
#add the group to the window
self.window.add_accel_group(accel)
def ccheckbox_clicked(self, widget):
checked = self.ccheckbox.get_active()
self.lsbutton.set_sensitive(not checked)
if checked:
self.lsbutton_stopped()
self.emit('command', "continuous_listen")
self.set_icon_active()
else:
self.emit('command', "continuous_stop")
self.set_icon_inactive()
def lsbutton_stopped(self):
self.lsbutton.set_label("Listen")
def lsbutton_clicked(self, button):
val = self.lsbutton.get_label()
if val == "Listen":
self.emit("command", "listen")
self.lsbutton.set_label("Stop")
#clear the label
self.label.set_text("")
self.set_icon_active()
else:
self.lsbutton_stopped()
self.emit("command", "stop")
self.set_icon_inactive()
def run(self):
#set the default icon
self.set_icon_inactive()
self.window.show_all()
if self.continuous:
self.set_icon_active()
self.ccheckbox.set_active(True)
def accel_quit(self, accel_group, acceleratable, keyval, modifier):
self.emit("command", "quit")
def delete_event(self, x, y ):
self.emit("command", "quit")
def finished(self, text):
#if the continuous isn't pressed
if not self.ccheckbox.get_active():
self.lsbutton_stopped()
self.set_icon_inactive()
self.label.set_text(text)
def set_icon_active_asset(self, i):
self.icon_active = i
def set_icon_inactive_asset(self, i):
self.icon_inactive = i
def set_icon_active(self):
Gtk.Window.set_default_icon_from_file(self.icon_active)
def set_icon_inactive(self):
Gtk.Window.set_default_icon_from_file(self.icon_inactive)
......@@ -18,67 +18,67 @@ this_dir = os.path.dirname( os.path.abspath(__file__) )
class Recognizer(GObject.GObject):
__gsignals__ = {
'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}
def __init__(self, language_file, dictionary_file, src = None):
GObject.GObject.__init__(self)
self.commands = {}
if src:
audio_src = 'alsasrc device="hw:%d,0"' % (src)
else:
audio_src = 'autoaudiosrc'
#build the pipeline
cmd = audio_src+' ! audioconvert ! audioresample ! pocketsphinx name=asr ! appsink sync=false'
try:
self.pipeline=Gst.parse_launch( cmd )
except Exception as e:
print( e)
print( "You may need to install gstreamer pocketsphinx")
raise e
'''messages come from the pipeline bus now'''
#get the pipeline bus
bus = self.pipeline.get_bus()
#hey bus, start emitting signals!
bus.add_signal_watch()
#connect messages from elements to our parser
bus.connect('message::element', self.parse_bus_element_message)
#get the Auto Speech Recognition piece
asr=self.pipeline.get_by_name('asr')
asr.set_property('lm', language_file)
asr.set_property('dict', dictionary_file)
def parse_bus_element_message(self, bus, message):
#get the message's structure
message_struct = message.get_structure()
#get the message's ... ahem ... type
message_type = message_struct.get_name()
#is this pocket sphinx?
if message_type != 'pocketsphinx':
#get outa here!
return
# is this the final decided text?
if message_struct.get_value('final'):
#hypothesis is the string we want
text = message_struct.get_value('hypothesis')
#emit finished
self.emit("finished", text)
#TODO: find a way to utilize partial matches and match confidence
def listen(self):
print("listen")
self.pipeline.set_state(Gst.State.PLAYING)
def pause(self):
self.pipeline.set_state(Gst.State.PAUSED)
def result(self, asr, text, uttid):
#emit finished
self.emit("finished", text)
__gsignals__ = {
'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}
def __init__(self, language_file, dictionary_file, src = None):
GObject.GObject.__init__(self)
self.commands = {}
if src:
audio_src = 'alsasrc device="hw:%d,0"' % (src)
else:
audio_src = 'autoaudiosrc'
#build the pipeline
cmd = audio_src+' ! audioconvert ! audioresample ! pocketsphinx name=asr ! appsink sync=false'
try:
self.pipeline=Gst.parse_launch( cmd )
except Exception as e:
print( e)
print( "You may need to install gstreamer pocketsphinx")
raise e
'''messages come from the pipeline bus now'''
#get the pipeline bus
bus = self.pipeline.get_bus()
#hey bus, start emitting signals!
bus.add_signal_watch()
#connect messages from elements to our parser
bus.connect('message::element', self.parse_bus_element_message)
#get the Auto Speech Recognition piece
asr=self.pipeline.get_by_name('asr')
asr.set_property('lm', language_file)
asr.set_property('dict', dictionary_file)
def parse_bus_element_message(self, bus, message):
#get the message's structure
message_struct = message.get_structure()
#get the message's ... ahem ... type
message_type = message_struct.get_name()
#is this pocket sphinx?
if message_type != 'pocketsphinx':
#get outa here!
return
# is this the final decided text?
if message_struct.get_value('final'):
#hypothesis is the string we want
text = message_struct.get_value('hypothesis')
#emit finished
self.emit("finished", text)
#TODO: find a way to utilize partial matches and match confidence
def listen(self):
print("listen")
self.pipeline.set_state(Gst.State.PLAYING)
def pause(self):
self.pipeline.set_state(Gst.State.PAUSED)
def result(self, asr, text, uttid):
#emit finished
self.emit("finished", text)
#!/usr/bin/env python3
import subprocess
import sys, os.path
import re
def update_language():
#set some variables
downloader = None
sentences_file = "~/.config/blather/sentences.corpus"
abs_sentences = os.path.expanduser(sentences_file)
HOST = "www.speech.cs.cmu.edu"
URL = "/cgi-bin/tools/lmtool/run"
TARGET = HOST+URL
#make sure curl or wget is installed
try:
output = subprocess.check_output("which curl", shell = True)
downloader = "curl"
except Exception as e:
print(e)
#if curl wasn't found
if not downloader:
try:
output = subprocess.check_output("which wget", shell = True)
downloader = "wget"
except Exception as e:
print(e)
print("Please install `curl` or `wget`")
#pffftttt we're out of here!
sys.exit()
if downloader == "curl":
cmd = "curl -s -L -F corpus=@{} -F formtype=simple {}".format(abs_sentences,TARGET)
print(cmd)
try:
output = subprocess.check_output(cmd, shell = True)
#decode the output
output = output.decode('ascii')
except Exception as e:
print(e)
print("failed to update language")
sys.exit()
print(output)
else:
#port the curl commands to wget
pass
#create a regex to find the base name
namefinder = re.search(r"The base name for this set is <b>(?P<base_name>.*)<", output)
base_name = namefinder.group("base_name")
print(base_name)
#use regex to find the http path to the files we want
pathfinder = re.search(r"(http://www.speech.cs.cmu.edu/tools/product/.*/)TAR",output)
http_path = pathfinder.group(1)
#where are the files we need?
lm_remote = http_path+base_name+".lm"
dic_remote = http_path+base_name+".dic"
#do more downloading
if downloader == 'curl':
#get the lang file
cmd = "curl -s {} > ~/.config/blather/lm.tmp".format(lm_remote)
try:
output = subprocess.check_output(cmd, shell = True)
except Exception as e:
print(e)
print("Failed to download {}".format(lm_remote))
sys.exit()
cmd = "curl -s {} > ~/.config/blather/dic.tmp".format(dic_remote)
try:
output = subprocess.check_output(cmd, shell = True)
except Exception as e:
print(e)
print("Failed to download {}".format(lm_remote))
sys.exit()
# if we made it this far, mv the temp files to their proper location
cmd = "mv ~/.config/blather/dic.tmp ~/.config/blather/language/dic"
subprocess.call(cmd, shell = True)
cmd = "mv ~/.config/blather/lm.tmp ~/.config/blather/language/lm"
subprocess.call(cmd, shell = True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment