Skip to content
Snippets Groups Projects
main.py 4.54 KiB
Newer Older
  • Learn to ignore specific revisions
  • Benoit Favre's avatar
    Benoit Favre committed
    #!/usr/bin/env python2
    
    import sys
    import os
    
    Benoit Favre's avatar
    Benoit Favre committed
    import glob, re
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    # set to location of libgstkaldionline2.so
    
    directory = os.path.dirname(__file__) or '.'
    os.environ['GST_PLUGIN_PATH'] = directory + '/asr/'
    
    Benoit Favre's avatar
    Benoit Favre committed
    os.environ['GTK_THEME'] = 'light'
    
    print 'gst plugin path =', os.environ['GST_PLUGIN_PATH']
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    # import gtk stuff
    from threading import Thread
    import gi
    gi.require_version('Gst', '1.0')
    from gi.repository import GObject, Gst, Gtk, Gdk
    GObject.threads_init()
    Gdk.threads_init()
    Gst.init(None)
    
    # make sure ctrl-c works
    import signal
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    
    # import local stuff
    
    Benoit Favre's avatar
    Benoit Favre committed
    import confirm, asr, actions, xmlview
    
    Benoit Favre's avatar
    Benoit Favre committed
    import levenstein, slu, osc
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    class ScriptedASR(Gtk.Window):
    
        def __init__(self, xml_filename, asr_config_file, osc_host, osc_port):
    
    Benoit Favre's avatar
    Benoit Favre committed
            super(ScriptedASR, self).__init__()
    
            self.connect("destroy", self.quit)
    
            self.set_default_size(1024, 768)
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.set_border_width(10)
    
            self.set_title('ASR Transcript [xml=%s asr=%s osc=%s:%s]' % (xml_filename, asr_config_file, osc_host, osc_port))
    
    Benoit Favre's avatar
    Benoit Favre committed
            vbox = Gtk.VBox()
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.xmlview = xmlview.XmlView(xml_filename)
    
    Benoit Favre's avatar
    Benoit Favre committed
            vbox.pack_start(self.xmlview, True, True, 5)
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.confirmer = confirm.ConfirmationBox()
            vbox.pack_start(self.confirmer, False, True, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
            # transcript view
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.asr = asr.ASR(asr_config_file, self.hyp_changed, self.hyp_changed)
    
    Benoit Favre's avatar
    Benoit Favre committed
            vbox.pack_start(self.asr, False, True, 5)
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            # slu
    
            #prefix = 'slu/automate/homeostasis_25nov_%s'
            #library = 'slu/src.new/librocio_slu.so'
    
    Benoit Favre's avatar
    Benoit Favre committed
            prefix = 'slu/automate/homeostasis_25nov_%s'
    
            library = 'slu/src/librocio_slu.so'
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.slu = {}
            for section_fst in glob.glob(prefix % 'section*.fst'):
                found = re.search('section(\d+)\.fst$', section_fst)
                if found:
                    section_id = int(found.group(1))
    
                    self.slu[section_id - 1] = slu.SLU(prefix % 'dico_word.txt', prefix % 'dico_action.txt', section_fst, prefix % 'clean_tail.fst', library=library)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.add(vbox)
            self.show_all()
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.confirmer.hide()
    
    Benoit Favre's avatar
    Benoit Favre committed
    
            # load css style
            style_provider = Gtk.CssProvider()
            style_provider.load_from_data(open('data/style.css', 'rb').read())
            Gtk.StyleContext.add_provider_for_screen( Gdk.Screen.get_default(), style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            # setup singletons 
            osc.setup(osc_host, osc_port)
            actions.setup(self.confirmer, self.xmlview)
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.current_section = 0
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    
        def set_section(self, section_id):
            self.xmlview.set_section(section_id)
    
    Benoit Favre's avatar
    Benoit Favre committed
        def slu_finished(self, model, slu_output):
            for action_id in range(self.previous_actions, model.num_actions()):
    
                action = actions.parse_slu_action(model.get_action(action_id))
                print action.text
                if action.text.startswith('#ENDSEQUENCE('):
                    pass
                elif action.text.startswith('#ENDSECTION('):
                    new_section = self.xmlview.get_section() + 1
                    self.confirmer.confirm('Go to section %d?' % (new_section + 1), 3, lambda: self.set_section(new_section))
                else:
                    self.xmlview.highlight(action)
                    actions.perform_action(action, False)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    
    Benoit Favre's avatar
    Benoit Favre committed
        def hyp_changed(self, hyp):
    
            #hyp = ' '.join(hyp).replace('[noise]', ' ').split()
    
            words = hyp[-1].strip().replace('_', ' ').split()
    
    Benoit Favre's avatar
    Benoit Favre committed
            section_id = self.xmlview.get_section()
    
    Benoit Favre's avatar
    Benoit Favre committed
            if self.current_section != section_id:
                self.previous_actions = 0
                self.current_section = section_id
    
            #print section_id
    
    Benoit Favre's avatar
    Benoit Favre committed
            if section_id in self.slu:
    
    Benoit Favre's avatar
    Benoit Favre committed
                model = self.slu[section_id]
                self.previous_actions = model.num_actions()
                model.process(words, self.slu_finished)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    
    Benoit Favre's avatar
    Benoit Favre committed
        def quit(self, window):
    
    Benoit Favre's avatar
    Benoit Favre committed
            for slu in self.slu.values():
                slu.shutdown()
    
    Benoit Favre's avatar
    Benoit Favre committed
            Gtk.main_quit()
    
    
    if __name__ == '__main__':
    
    Benoit Favre's avatar
    Benoit Favre committed
        import selector
    
        xml_filename = 'data/homeostasis_25nov.xml'
    
    Benoit Favre's avatar
    Benoit Favre committed
        asr_config_file = 'asr/mika-fred-1.cfg'
    
    Mickael Rouvier's avatar
    Mickael Rouvier committed
        asr_config_file = 'asr/mika-fred-2.cfg'
    
    Benoit Favre's avatar
    Benoit Favre committed
        asr_config_file = 'asr/fisher-benoit-1.cfg'
    
    Benoit Favre's avatar
    Benoit Favre committed
        if len(sys.argv) > 1:
            xml_filename = sys.argv[1]
    
        if len(sys.argv) > 2:
    
    Benoit Favre's avatar
    Benoit Favre committed
            asr_config_file = sys.argv[2]
    
        xml_filename, asr_config_file, osc_host, osc_port = selector.ModelSelector(xml_filename, asr_config_file).run()
        if xml_filename == None or asr_config_file == None or osc_host == None or osc_port == None:
    
    Benoit Favre's avatar
    Benoit Favre committed
            sys.exit(0)
    
        app = ScriptedASR(xml_filename, asr_config_file, osc_host, osc_port)
    
    Benoit Favre's avatar
    Benoit Favre committed
        Gtk.main()