def task_enroll(input_directory, output_model):
    m = ModelInterface()
    for k in input_directory.strip().split():
        input_dirs = [os.path.expanduser(k)]
    for d in input_directory:
        dirs = itertools.chain(*(glob.glob(d)))
    for d in dirs:
        if os.path.isdir(d):
            dirs = [d]
    files = []
    if len(dirs) == 0:
        print "No valid directory found!"
        sys.exit(1)
    for d in dirs:
        label = os.path.basename(d.rstrip('/'))

        wavs = glob.glob(d + '/*.wav')
        if len(wavs) == 0:
            print "No wav file found in {0}".format(d)
            continue
        print "Label {0} has files {1}".format(label, ','.join(wavs))
        for wav in wavs:
            fs, signal = read_wav(wav)
            m.enroll(label, fs, signal)

    m.train()
    m.dump(output_model)
def task_enroll(input_dirs, output_model):
    m = ModelInterface()
    input_dirs = [os.path.expanduser(k) for k in input_dirs.strip().split()]
    dirs = itertools.chain(*(glob.glob(d) for d in input_dirs))
    dirs = [d for d in dirs if os.path.isdir(d)]
    files = []
    if len(dirs) == 0:
        print ("No valid directory found!")
        sys.exit(1)
    for d in dirs:
        label = os.path.basename(d.rstrip('/'))
        #label =d
        wavs = glob.glob(d + '/*.wav')
        #print(wavs)
        if len(wavs) == 0:
            print ("No wav file found in {0}".format(d))
            continue
        print ("Label {0} has files {1}".format(label, ','.join(wavs)))
        for wav in wavs:
            fs, signal = read_wav(wav)
            label=wav.split('/')[-1].split('_')[0]
            #print(label)
            m.enroll(label, fs, signal)

    m.train()
    m.dump(output_model)
Example #3
0
def task_enroll(input_dirs, output_model):
    m = ModelInterface()
    input_dirs = [os.path.expanduser(k) for k in input_dirs.strip().split()]
    dirs = itertools.chain(*(glob.glob(d) for d in input_dirs))
    dirs = [d for d in dirs if os.path.isdir(d)]
    files = []
    if len(dirs) == 0:
        print "No valid directory found!"
        sys.exit(1)
    training_stats = []
    for d in dirs:
        label = os.path.basename(d.rstrip('/'))

        wavs = glob.glob(d + '/*.wav')
        if len(wavs) == 0:
            print "No wav file found in {0}".format(d)
            continue
        print "Label '{0}' has files: {1}".format(label, ', '.join(wavs))
        total_len = 0
        for wav in wavs:
            fs, signal = read_wav(wav)
            print "   File '{}' has frequency={} and length={}".format(
                wav, fs, len(signal))
            total_len += len(signal)
            m.enroll(label, fs, signal)
        training_stats.append((label, total_len))
    print "--------------------------------------------"
    for label, total_len in training_stats:
        print "Total length of training data for '{}' is {}".format(
            label, total_len)
    print "For best accuracy, please make sure all labels have similar amount of training data!"

    m.train()
    m.dump(output_model)
Example #4
0
 def load(self):
     # for user in self.userdata:
     #     if self.userdata.index(user) == self.Userchooser.currentIndex() - 1:
     #         file_name = user[0]
     #         break
     userindex = self.Userchooser.currentIndex() - 1
     if (userindex > -1):
         file_name = self.userdata[userindex][0]
         #self.enroll_checklist()
         if file_name == None:
             self.status("no user")
             return
         fname = "gmms/" + file_name + ".model"
         #fname = QFileDialog.getOpenFileName(self, "Open Data File:", "", "")
         if fname:
             if self.backend == None:
                 try:
                     self.backend = ModelInterface()
                     self.backend.load(fname, file_name)
                 except Exception as e:
                     self.warn(str(e))
             else:
                 try:
                     self.backend.load(fname, file_name)
                 except Exception as e:
                     self.warn(str(e))
         self.status("loaded model " + file_name)
     else:
         self.status("Please select user.")
Example #5
0
def task_enroll(output_model):
    m = ModelInterface()
    train_data = range(1, 4)
    for i in train_data:
        wav = "./data/train/" + str(i) + ".wav"
        print wav
        label = str(i)
        fs, signal = read_wav(wav)
        m.enroll(label, fs, signal)
    m.train()
    m.dump(output_model)
Example #6
0
 def load_check_box(self, label):
     fname = "gmms/" + label + ".model"
     if fname == None:
         self.status("no user")
         return
     #fname = QFileDialog.getOpenFileName(self, "Open Data File:", "", "")
     if fname:
         if self.backend == None:
             try:
                 self.backend = ModelInterface()
                 self.backend.load(fname, label)
             except Exception as e:
                 self.warn(str(e))
         else:
             try:
                 self.backend.load(fname, label)
             except Exception as e:
                 self.warn(str(e))
         self.status("loaded all")
Example #7
0
def task_enroll(input_dirs, output_model):
    m = ModelInterface()
    input_dirs = input_dirs.strip().split()
    dirs = itertools.chain(*(glob.glob(d) for d in input_dirs))
    dirs = [d for d in dirs if os.path.isdir(d)]
    files = []
    for d in dirs:
        label = os.path.basename(d)

        wavs = glob.glob(d + '/*.wav')
        if len(wavs) == 0:
            continue
        print "Label {0} has files {1}".format(label, ','.join(wavs))
        for wav in wavs:
            fs, signal = wavfile.read(wav)
            m.enroll(label, fs, signal)

    m.train()
    m.dump(output_model)
Example #8
0
    parser.add_argument('-t', '--task',
                       help='Task to do. Either "enroll" or "predict"',
                       required=True)

    parser.add_argument('-i', '--input',
                       help='Input Files(to predict) or Directories(to enroll)',
                       required=True)

    parser.add_argument('-m', '--model',
                       help='Model file to save(in enroll) or use(in predict)',
                       required=True)

    ret = parser.parse_args()
    return ret

m_enroll = ModelInterface()

def task_enroll(input_dirs, output_model):
    
    input_dirs = [os.path.expanduser(k) for k in input_dirs.strip().split()]
    dirs = itertools.chain(*(glob.glob(d) for d in input_dirs))
    dirs = [d for d in dirs if os.path.isdir(d)]
    files = []
    if len(dirs) == 0:
        print "No valid directory found!"
        sys.exit(1)
    for d in dirs:
        label = os.path.basename(d.rstrip('/'))

        wavs = glob.glob(d + '/*.wav')
        if len(wavs) == 0:
Example #9
0
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        uic.loadUi("gui/edytor.ui", self)

        self.last_none_start = 0
        self.last_switch_user = 0
        self.last_enter_none_handler = False
        self.last_user_detected = 'None'

        self.statusBar()

        self.check_result = False

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.timer_callback)

        self.noiseButton.clicked.connect(self.noise_clicked)
        self.recording_noise = False
        self.loadNoise.clicked.connect(self.load_noise)

        self.AddUser.clicked.connect(self.add_user)

        self.enrollRecord.clicked.connect(self.start_enroll_record)
        self.stopEnrollRecord.clicked.connect(self.stop_enroll_record)
        self.enrollFile.clicked.connect(self.enroll_file)
        self.enroll.clicked.connect(self.do_enroll)
        self.startTrain.clicked.connect(self.start_train)
        self.dumpBtn.clicked.connect(self.dump)
        self.loadBtn.clicked.connect(self.load)

        self.recoRecord.clicked.connect(self.start_reco_record)
        self.stopRecoRecord.clicked.connect(self.stop_reco_record)
        #        self.newReco.clicked.connect(self.new_reco)
        self.recoFile.clicked.connect(self.reco_file)
        self.recoInputFiles.clicked.connect(self.reco_files)

        #UI.init
        self.userdata = []
        self.loadUsers()
        self.Userchooser.currentIndexChanged.connect(self.showUserInfo)
        self.ClearInfo.clicked.connect(self.clearUserInfo)
        self.UpdateInfo.clicked.connect(self.updateUserInfo)
        self.UploadImage.clicked.connect(self.upload_avatar)
        #movie test
        self.movie = QMovie(u"gui/image/recording.gif")
        self.movie.start()
        self.movie.stop()
        self.Animation.setMovie(self.movie)
        self.Animation_2.setMovie(self.movie)
        self.Animation_3.setMovie(self.movie)

        self.aladingpic = QPixmap(u"gui/image/a_hello.png")
        self.Alading.setPixmap(self.aladingpic)
        self.Alading_conv.setPixmap(self.aladingpic)

        #default user image setting
        self.avatarname = "gui/image/nouser.jpg"
        self.defaultimage = QPixmap(self.avatarname)
        self.Userimage.setPixmap(self.defaultimage)
        self.recoUserImage.setPixmap(self.defaultimage)
        self.convUserImage.setPixmap(self.defaultimage)
        self.load_avatar('gui/avatar/')

        #quick enroll
        self.show_checkbox()
        self.checkbox.setWidget(self.available_to_enroll)
        self.LoadAll.clicked.connect(self.enroll_checklist)

        #Conversation Mode Variables
        self.conv_record = np.array([], dtype=NPDtype)
        self.time_init = QTimer(self)
        self.current_label = None

        # Graph Window init
        self.graphwindow = GraphWindow()
        self.newname = ""
        self.lastname = ""
        self.Graph_button.clicked.connect(self.graphwindow.show)
        self.convRecord.clicked.connect(self.start_conv_record)
        self.convStop.clicked.connect(self.stop_conv)
        self.generateTranscript.clicked.connect(self.generate_transcript)

        self.backend = ModelInterface()

        # debug
        QShortcut(QKeySequence("Ctrl+P"), self, self.printDebug)

        #init
        try:
            fs, signal = read_wav("bg.wav")
            self.backend.init_noise(fs, signal)
        except:
            pass
def handleAction(action):

    if action['type'] == "AUDIO":

        label = action['label']
        sampleRate = action['sampleRate']
        audio = action['audio']
        length = action['length']
        groupid = action['groupid']
        participantid = action['participantid']

        if groupid not in modelDict.keys():
            modelDict[groupid] = ModelInterface()

        if groupid not in bufferDict.keys():
            bufferDict[groupid] = EmitStack(train_second)

        if groupid not in participantDict.keys():
            participantDict[groupid] = {}

        if participantid not in participantDict[groupid].keys():
            participantDict[groupid][participantid] = label

        audioArr = np.empty(length)
        for key, value in audio.iteritems():
            audioArr[int(key)] = value

        bufferDict[groupid].extend(audioArr, length)
        if bufferDict[groupid].canEmit(sampleRate):
            emitArr = bufferDict[groupid].emitLabel()
            modelDict[groupid].enroll(participantid, sampleRate, emitArr)

        print audioArr

    elif action['type'] == "TRAIN_GROUP":
        groupid = action['groupid']

        if groupid in bufferDict.keys():
            bufferDict[groupid].emitLabel()
        if groupid in modelDict.keys():
            modelDict[groupid].train()
            print "Finish training"
        else:
            print "No model with group id {0}".format(groupid)

    elif action['type'] == "STARTOVER":
        groupid = action['groupid']
        modelDict.pop(groupid, None)
        bufferDict.pop(groupid, None)
        participantDict.pop(groupid, None)
        print "group id {0} starts over".format(groupid)

    elif action['type'] == "FINISH":
        groupid = action['groupid']
        modelDict.pop(groupid, None)
        bufferDict.pop(groupid, None)
        participantDict.pop(groupid, None)
        dataPathDict.pop(groupid, None)

        print "group id {0} finish".format(groupid)

    elif action['type'] == "PREDICT":
        sampleRate = action['sampleRate']
        audio = action['audio']
        length = action['length']
        groupid = action['groupid']
        audioArr = np.empty(length)

        for key, value in audio.iteritems():
            audioArr[int(key)] = value

        predict_label = "N/A"

        if groupid in bufferDict.keys() and groupid in modelDict.keys(
        ) and groupid in participantDict.keys():

            bufferDict[groupid].extend(audioArr, length)
            if bufferDict[groupid].canEmit(sampleRate):
                emitArr = bufferDict[groupid].emitLabel()

                voiceThreshold = 0.02
                if np.mean(emitArr[emitArr > 0]) > voiceThreshold:
                    label = modelDict[groupid].predict(sampleRate, emitArr)
                    emitSecond = stack.emitHeight
                    if label in participantDict[groupid].keys():
                        print participantDict[groupid][label]
                        predict_label = label
                        emit(
                            'data', {
                                'type': 'DATA',
                                'label': participantDict[groupid][label],
                                'second': emitSecond
                            })
                else:
                    print "No one is speaking"

                if not "csvFile" in dataPathDict[groupid].keys():
                    dataPathDict[groupid]["csvFile"] = DataFrame(
                        columns=('group id', 'time', 'participant id',
                                 'condition', 'meeting', 'date'))

                tempDict = dataPathDict[groupid]
                tempDict["csvFile"].loc[len(tempDict["csvFile"])] = [
                    groupid, tempDict['time'], predict_label,
                    tempDict['condition'], tempDict['meeting'],
                    tempDict['date']
                ]

                tempDict['time'] = tempDict['time'] + train_second
        else:
            print "Group {0} is not registerd".format(groupid)

        if not "soundFile" in dataPathDict[groupid].keys():
            sound_format = Format('wav')
            dataPathDict[groupid]["soundFile"] = Sndfile(
                dataPathDict[groupid]["soundPath"], 'w', sound_format, 1,
                sampleRate)

        dataPathDict[groupid]["soundFile"].write_frames(audioArr)

    elif action['type'] == "OPEN_MEETING":
        groupid = action['groupid']
        condition = action['condition']
        meeting = action['meeting']
        nowString = datetime.now().strftime('%Y%m%d%H%M%S')
        nowFormat = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        if not os.path.exists(OUTPUTPATH):
            os.mkdir(OUTPUTPATH)

        groupPath = os.path.join(OUTPUTPATH, groupid + '/')
        if not os.path.exists(groupPath):
            os.mkdir(groupPath)

        meetingPath = os.path.join(groupPath, meeting + '/')
        if not os.path.exists(meetingPath):
            os.mkdir(meetingPath)

        filename = groupid + '-' + nowString

        if groupid not in dataPathDict.keys():
            dataPathDict[groupid] = {}

        dataPathDict[groupid]['soundPath'] = os.path.join(
            meetingPath, filename + '.wav')
        dataPathDict[groupid]['csvPath'] = os.path.join(
            meetingPath, filename + '.csv')
        dataPathDict[groupid]['condition'] = condition
        dataPathDict[groupid]['meeting'] = meeting
        dataPathDict[groupid]['date'] = nowFormat
        dataPathDict[groupid]['time'] = 0

    elif action['type'] == "CLOSE_MEETING":
        groupid = action['groupid']

        dataPathDict[groupid]['soundFile'].close()
        dataPathDict[groupid].pop('soundFile', None)

        print "Sound file finish recorded"
        dataPathDict[groupid]['csvFile'].to_csv(
            path_or_buf=dataPathDict[groupid]['csvPath'])
        dataPathDict[groupid].pop('csvFile', None)

        print "CSV finish recorded"

    elif action['type'] == "REGISTER_GROUP":
        groupid = action['groupid']
        join_room(groupid)
        print "{0} is register".format(groupid)

    elif action['type'] == "REGISTER":
        user = action['user']
        room.append(user)
        join_room(user)
        print "{0} is register".format(user)

    elif action['type'] == "LEAVE":
        user = action['user']
        leave_room(user)
        room.remove(user)
        print "{0} leaves".format(user)

    else:
        print "This action is not handled yet"
from pandas import DataFrame, Series

from emitStack import EmitStack

import os
import numpy as np
import sys

sys.path.append(
    os.path.join(os.path.dirname(os.path.realpath(__file__)), 'gui'))

from gui.interface import ModelInterface

modelName = "model.out"
model = ModelInterface(
) if not os.path.isfile(modelName) else ModelInterface.load(modelName)

modelDict = {}
train_second = 2

participantDict = {}

bufferDict = {}
stack = EmitStack(2)
train_stack = EmitStack(2)

dataPathDict = {}

room = []

OUTPUTPATH = 'Output/'