def evaluate(self, sound):

        stgrm = sg.generate_spectrogram(sound)
        stgrm = numpy.asarray(stgrm, dtype=theano.config.floatX)
        elements = [self.evaluate_model(x).tolist() for x in stgrm]
        position = max(set(elements), key=elements.count)
        return self.classes[position]
    def evaluate(self, sound):

        stgrm    = sg.generate_spectrogram(sound)
        stgrm    = numpy.asarray(stgrm, dtype=theano.config.floatX)
        elements = [self.evaluate_model(x).tolist() for x in stgrm]
        position = max(set(elements), key=elements.count)
        return self.classes[position]
def load_data(train_dir, test_dir):

    train_input = [
        f for f in os.listdir(train_dir) if isfile(join(train_dir, f))
    ]
    test_input = [f for f in os.listdir(test_dir) if isfile(join(test_dir, f))]

    classes = []
    train_set = []
    test_set = []

    # Training data
    for element in train_input:

        elem_id = element.split(".")[0].split("_")[0]

        try:
            index = classes.index(elem_id)
        except:
            index = len(classes)
            classes.append(elem_id)

        stgrm = sg.generate_spectrogram(join(train_dir, element))

        train_set.extend(_make_array(stgrm, index))

    # Test data
    for element in test_input:

        elem_id = element.split(".")[0].split("_")[0]

        if elem_id in classes:
            index = classes.index(elem_id)
            stgrm = sg.generate_spectrogram(join(test_dir, element))
            test_set.extend(_make_array(stgrm, index))

    train_min_nr = min([len(x[0]) for x in train_set])
    test_min_nr = min([len(x[0]) for x in test_set])

    train_data = [x[:train_min_nr] for x in train_set]
    test_data = [x[:test_min_nr] for x in test_set]

    return numpy.random.permutation(train_data), test_data, classes
def load_data(train_dir, test_dir):
    
    train_input = [f for f in os.listdir(train_dir) if isfile(join(train_dir, f))]
    test_input  = [f for f in os.listdir(test_dir) if isfile(join(test_dir, f))]
    
    classes   = []
    train_set = []
    test_set  = []

    # Training data
    for element in train_input:
        
        elem_id = element.split(".")[0].split("_")[0]

        try:
            index = classes.index(elem_id)
        except:
            index = len(classes)
            classes.append(elem_id)

        stgrm = sg.generate_spectrogram(join(train_dir, element))

        train_set.extend(_make_array(stgrm, index))

    # Test data
    for element in test_input:
        
        elem_id = element.split(".")[0].split("_")[0]

        if elem_id in classes:
            index = classes.index(elem_id)
            stgrm = sg.generate_spectrogram(join(test_dir, element))
            test_set.extend(_make_array(stgrm, index))

    train_min_nr = min([len(x[0]) for x in train_set])
    test_min_nr  = min([len(x[0]) for x in test_set])

    train_data = [x[:train_min_nr] for x in train_set]
    test_data  = [x[:test_min_nr] for x in test_set]

    return numpy.random.permutation(train_data), test_data, classes
Beispiel #5
0
async def analyze(request):
    data = await request.form()
    audio = await (data['file'].read())
    uploadpath = os.path.join(path, 'uploads')
    try:
        os.makedirs(uploadpath)
        filepath = os.path.join(uploadpath, 'test.wav')
        with open(filepath, 'wb') as f:
            f.write(audio)
        img = open_image(generate_spectrogram(filepath))
    finally:
        shutil.rmtree(uploadpath)

    return JSONResponse({'crying': learn.predict(img)[0]})
def load_data(train_dir, test_dir, noise_dir):
    
    train_input = [f for f in os.listdir(train_dir) if isfile(join(train_dir, f))]
    test_input  = [f for f in os.listdir(test_dir)  if isfile(join(test_dir, f))]

    classes   = []
    train_set = []
    test_set  = []


    # Training data
    for element in train_input:
        
        elem_id = element.split(".")[0].split("_")[0]

        try:
            index = classes.index(elem_id)
        except:
            index = len(classes)
            classes.append(elem_id)

        stgrm = sg.generate_spectrogram(join(train_dir, element))

        train_set.append(_make_array(stgrm, index))

    # Test data

    for element in test_input:        
        elem_id = element.split(".")[0].split("_")[0]
        test_set.append([join(test_dir,element),elem_id])


    train_min_nr = min([len(x) for x in train_set])

    train_data = []

    for x in train_set:
        indexes    = numpy.random.choice(range(len(x)), train_min_nr, replace=False)
        train_data.extend([x[i] for i in indexes])


    return numpy.random.permutation(train_data), test_set, classes
def demo_load(train_dir):
    
    train_input = [f for f in os.listdir(train_dir) if isfile(join(train_dir, f))]

    classes   = []
    train_set = []
    test_set  = []
    print 'loading data'
    # Training data
    ize = 1
    for element in train_input:
        
        elem_id = element.split(".")[0].split("_")[0]
        print elem_id

        try:
            index = classes.index(elem_id)
        except:
            index = len(classes)
            classes.append(elem_id)

        stgrm = sg.generate_spectrogram(join(train_dir, element))
        if ize == 3:
            exit(1)
        ize = ize + 1

        train_set.extend(_make_array(stgrm, index))

    train_min_nr = min([len(x[0]) for x in train_set])

    train_data = [x[:train_min_nr] for x in train_set]
    train_data = numpy.random.permutation(train_data)

    test_data  = train_data[:130]
    train_data = train_data[1300:]


    return train_data, test_data, classes
    def evaluate(self, sound):

        stgrm    = sg.generate_spectrogram(sound)
        elements = [self.evaluate_model(x).tolist() for x in stgrm]
        position = max(set(elements), key=elements.count)
        return self.classes[position]
 def testGenerateSpectrogramReturnsCorrectShapeInCaseOfValidInput(self):
     test = spectrogram.generate_spectrogram('FMEV_Sr10.wav')
     self.assertEqual(test.shape[1], 601)
    def evaluate(self, sound):

        stgrm = sg.generate_spectrogram(sound)
        elements = [self.evaluate_model(x).tolist() for x in stgrm]
        position = max(set(elements), key=elements.count)
        return self.classes[position]
 def testGenerateSpectrogramReturnsCorrectShapeInCaseOfValidInput(self):
     test = spectrogram.generate_spectrogram('FMEV_Sr10.wav')
     self.assertEqual(test.shape[1], 601)