Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip #18] python3 update #21

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Blather.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env python2
#!/usr/bin/env python3

# -- this code is licensed GPLv3
# Copyright 2013 Jezra
# Modifications by: Allan Bogh - ajbogh@allanbogh.com

import sys
import signal
import gobject
from gi.repository import GObject
import os.path
import subprocess #used to execute commands
import shutil #used to copy plugins directory
Expand Down Expand Up @@ -50,7 +50,7 @@ def __init__(self, opts):
self.recognizer.connect('finished',self.recognizer_finished)
self.matchTime = 0
self.keywordTimeLimit = opts.keytime #set to 0 to always speak the keyword

self.commandFileTime = 0
#updates language file and commands on start
self.checkCommandFile()
Expand All @@ -65,7 +65,7 @@ def __init__(self, opts):
elif opts.interface == "g":
from GtkUI import UI
else:
print "no GUI defined"
print("no GUI defined")
sys.exit()

self.ui = UI(args,opts.continuous)
Expand All @@ -85,14 +85,14 @@ def read_commands(self):
self.commands = {}
self.keywords = []
for line in file_lines:
print line
print(line)
#trim the white spaces
line = line.strip()
#if the line has length and the first char isn't a hash
if len(line) and line[0]!="#":
#this is a parsible line
(key,value) = line.split(":",1)
print key, value
print((key, value))
#get the keyword out of the commands file
if value == "keyword" and key.strip().lower() not in self.keywords:
self.keywords.append(key.strip().lower())
Expand Down Expand Up @@ -142,25 +142,25 @@ def recognizer_finished(self, recognizer, text):
#call the process
if biggestKeyCount > 0 and ((len(textWords) <= 2 and len(biggestKeySet) == len(textWords)) or percentMatch >= PERCENT_MATCH_LIMIT): #must be equal or a 60% match
self.matchTime = time.time()
print("Best match: " + biggestKey, "Detected: " + text.lower(), "Percent match: " + str(percentMatch));
print("Best match: ", biggestKey, "Detected: ", text.lower(), "Percent match: ", str(percentMatch))
cmd = self.commands[biggestKey]
if cmd == "cancel" and hasattr(self, 'runningProcess'):
print("Cancelling previous command with PID "+str(self.runningProcess.pid))
print("Cancelling previous command with PID ", str(self.runningProcess.pid))

self.terminate_child_processes(self.runningProcess.pid)

#terminate parent process
self.runningProcess.terminate();
elif cmd != "cancel":
print cmd
print(cmd)
if "plugins/" in cmd:
#execute a plugin script
self.runningProcess = subprocess.Popen(os.path.join(file_dir,cmd), shell=True)
else:
self.runningProcess = subprocess.Popen(cmd, shell=True)
self.log_history(text)
else:
print("No matching command", "Percent match: " + str(percentMatch))
print("No matching command\n", "Percent match: ", str(percentMatch))
#if there is a UI and we are not continuous listen
if self.ui:
if not self.continuous_listen:
Expand Down Expand Up @@ -190,7 +190,7 @@ def checkCommandFile(self):
self.read_commands()

def process_command(self, UI, command):
print command
print(command)
if command == "listen":
self.recognizer.listen()
elif command == "stop":
Expand Down Expand Up @@ -258,7 +258,7 @@ def terminate_child_processes(self, pid):
for pid in childProcesses:
#recursive call to kill entire family tree
self.terminate_child_processes(int(pid))
print("Killing child with PID "+str(pid))
print("Killing child with PID ", str(pid))
p = psutil.Process(int(pid))
p.terminate()

Expand All @@ -285,10 +285,10 @@ def terminate_child_processes(self, pid):
(options, args) = parser.parse_args()
#make our blather object
blather = Blather(options)
#init gobject threads
gobject.threads_init()
#init GObject threads
GObject.threads_init()
#we want a main loop
main_loop = gobject.MainLoop()
main_loop = GObject.MainLoop()
#handle sigint
signal.signal(signal.SIGINT, signal.SIG_DFL)
#run the blather
Expand All @@ -298,6 +298,6 @@ def terminate_child_processes(self, pid):
try:
main_loop.run()
except:
print "time to quit"
print("time to quit")
main_loop.quit()
sys.exit()
10 changes: 5 additions & 5 deletions GtkUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
# -- this code is licensed GPLv3
# Copyright 2013 Jezra
import sys
import gobject
from gi.repository import GObject
#Gtk
import pygtk
import gtk

class UI(gobject.GObject):
class UI(GObject.GObject):
__gsignals__ = {
'command' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}

def __init__(self,args, continuous):
gobject.GObject.__init__(self)
GObject.GObject.__init__(self)
self.continuous = continuous
#make a window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
Expand Down Expand Up @@ -84,7 +84,7 @@ def delete_event(self, x, y ):
self.emit("command", "quit")

def finished(self, text):
print text
print(text)
#if the continuous isn't pressed
if not self.ccheckbox.get_active():
self.lsbutton_stopped()
Expand Down
10 changes: 5 additions & 5 deletions QtUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
# -- this code is licensed GPLv3
# Copyright 2013 Jezra
import sys
import gobject
from gi.repository import GObject
# Qt stuff
from PySide.QtCore import Signal, Qt
from PySide.QtGui import QApplication, QWidget, QMainWindow, QVBoxLayout
from PySide.QtGui import QLabel, QPushButton, QCheckBox, QIcon, QAction

class UI(gobject.GObject):
class UI(GObject.GObject):
__gsignals__ = {
'command' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}

def __init__(self,args,continuous):
self.continuous = continuous
gobject.GObject.__init__(self)
GObject.GObject.__init__(self)
#start by making our app
self.app = QApplication(args)
#make a window
Expand Down Expand Up @@ -87,7 +87,7 @@ def run(self):
self.emit("command", "quit")

def finished(self, text):
print text
print(text)
#if the continuous isn't pressed
if not self.ccheckbox.isChecked():
self.lsbutton_stopped()
Expand Down
39 changes: 23 additions & 16 deletions Recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,54 @@
# -- this code is licensed GPLv3
# Copyright 2013 Jezra

import pygst
pygst.require('0.10')
import gst
import gi
gi.require_version('Gst', '1.0')
import os.path
import gobject
from gi.repository import GObject, Gst
Gst.init(None)

#define some global variables
this_dir = os.path.dirname( os.path.abspath(__file__) )


class Recognizer(gobject.GObject):
class Recognizer(GObject.GObject):
__gsignals__ = {
'finished' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
}
def __init__(self, language_file, dictionary_file, src = None):
gobject.GObject.__init__(self)
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 ! vader name=vad ! pocketsphinx name=asr ! appsink sync=false'
self.pipeline=gst.parse_launch( cmd )
cmd = audio_src +' ! audioconvert ! audioresample ' + '! pocketsphinx name=asr ! appsink sync=false'
self.pipeline=Gst.parse_launch( cmd )
#get the Auto Speech Recognition piece
asr=self.pipeline.get_by_name('asr')
asr.connect('result', self.result)
bus=self.pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message::element', self.element_message)
print(language_file)
print(dictionary_file)
asr.set_property('lm', language_file)
asr.set_property('dict', dictionary_file)
asr.set_property('configured', True)
#get the Voice Activity DEtectoR
self.vad = self.pipeline.get_by_name('vad')
self.vad.set_property('auto-threshold',True)
# asr.set_property('configured', True)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got rid of the segfault by commenting out this line.

You'll see a little debugging code and some other minor changes that I made after reading

https://cmusphinx.github.io/wiki/gstreamer/?&#configuring_the_decoder_and_improving_accuracy

But none of that seemed to make a difference until I commented L39 there.

print(language_file)

def listen(self):
self.pipeline.set_state(gst.STATE_PLAYING)
self.pipeline.set_state(Gst.State.PLAYING)

def pause(self):
self.vad.set_property('silent', True)
self.pipeline.set_state(gst.STATE_PAUSED)
self.pipeline.set_state(Gst.State.PAUSED)

def element_message(self, bus, msg):
msgtype = msg.get_structure().get_name()
if msgtype != 'pocketsphinx' and msg.get_structure().get_value('final'):
self.result(self, bus, msg.get_structure().get_value('hypothesis'), msg.get_structure().get_value('confidence'))

def result(self, asr, text, uttid):
#emit finished
Expand Down