Пример #1
0
def classifyFile(dirs, soundFile, windowFile):
    print '### Classifying:', soundFile
    winsize = windowFile['windows'][0]['size']
    winstride = windowFile['windows'][0]['stride']

    # Preprocess the file
    prepFile(soundFile)

    # Read the file
    filepointer = h5py.File(soundFile, 'r')
    signals = files.signalsFromHDF5(soundFile)

    startEndPairs = makeStartEndPairs(winsize, winstride, signals)
    windowDic = makeWindowDic(startEndPairs, signals, winsize)
    windowPredictions = classifyWindows(dirs, startEndPairs, signals,
                                        windowDic, winsize)

    filePredictions = {}
    for key in windowPredictions.keys():
        filePredictions[key] = sum(windowPredictions[key]) / len(
            windowPredictions[key])

    windowPredictions['windows'] = startEndPairs

    return windowPredictions, filePredictions
Пример #2
0
def classifyFile(dirs, soundFile, windowFile):
    print '### Classifying:', soundFile 
    winsize = windowFile['windows'][0]['size']
    winstride = windowFile['windows'][0]['stride']
    
    # Preprocess the file
    prepFile(soundFile)

    # Read the file
    filepointer = h5py.File(soundFile, 'r')
    signals = files.signalsFromHDF5(soundFile)
    
    startEndPairs = makeStartEndPairs(winsize, winstride, signals)
    windowDic = makeWindowDic(startEndPairs, signals, winsize)
    windowPredictions = classifyWindows(dirs, startEndPairs, signals, windowDic, winsize)
    
    filePredictions = {}
    for key in windowPredictions.keys():
        filePredictions[key] = sum(windowPredictions[key])/len(windowPredictions[key])
    
    windowPredictions['windows'] = startEndPairs
    
    return windowPredictions, filePredictions
Пример #3
0
def prepFile(f):
    print "file: " + f
    if os.path.isfile(f):
        # Read information from the hdf5
        signals = files.signalsFromHDF5(f)

        # Original signal
        original = signals['energy'][:, :]

        # Get the zero/infinity columns out
        original = np.transpose(original)
        zeroColLeft = []
        zeroColRight = []
        while max(original[0]) <= 0:
            zeroColLeft.append(original[0])
            original = original[1:]
        while max(original[-1]) <= 0:
            zeroColRight.append(original[-1])
            original = original[:-2]
        original = np.transpose(original)

        # preprocess signal
        morphology = p.dilate(p.erode(original))

        # Put the zeros/infinities back
        if len(zeroColLeft) > 0 or len(zeroColRight) > 0:
            if len(zeroColLeft) > 0:
                morphology = np.column_stack(
                    (np.transpose(zeroColLeft), morphology))
            if len(zeroColRight) > 0:
                morphology = np.column_stack(
                    (morphology, np.transpose(zeroColRight)))

        # Store the preprocessed signal
        if 'morphology' in signals:
            signals['morphology'] = morphology
        else:
            with h5py.File(f, 'r+') as filepointer:
                filepointer.create_dataset('morphology', data=morphology)

        # Foreground background
        definitions = [
            bgconfig.getDefaults(tau) for tau in bgconfig.tau(0.5, 4)
        ]
        bgmodels = [bgmodel.BGModel(
            d['tau'],
            d,
        ) for d in definitions]
        for model in bgmodels:
            response = model.calculate(original)
            # Put the zeros/infinities back
            if len(zeroColLeft) > 0 or len(zeroColRight) > 0:
                if len(zeroColLeft) > 0:
                    response = np.column_stack(
                        (np.transpose(zeroColLeft), response))
                if len(zeroColRight) > 0:
                    response = np.column_stack(
                        (response, np.transpose(zeroColRight)))
            # np.save(prepDir + 'tau {}'.format(model.name), response)
            if 'tau {}'.format(model.name) in signals:
                signals['tau {}'.format(model.name)] = response
            else:
                with h5py.File(f, 'r+') as filepointer:
                    filepointer.create_dataset('tau {}'.format(model.name),
                                               data=response)
label = 0
images = []
labels = []
for subdir in dirs:
    d = os.path.join(root, subdir)
    hdf5dir = os.path.join(d, 'ptne/hdf5/2016-06-02')

    h5files = [f for f in os.listdir(hdf5dir) if '.wav.3' in f]

    if 'Pouring' in hdf5dir or 'Church bells' in hdf5dir or 'Glass' in hdf5dir:
        print 'skipping ', hdf5dir
        continue

    print 'Working on ', hdf5dir
    for h5file in h5files:
        signals = files.signalsFromHDF5(os.path.join(hdf5dir, h5file))
        energy = signals['energy']
        pulse = signals['tfmax']
        tone = signals['tsmax']

        max = np.max([tone, energy, pulse])
        min = np.min([tone, energy, pulse])

        full_im = (np.stack([tone, pulse, energy], axis=0).transpose(
            (1, 2, 0)) - min) / (max - min)
        full_im = imresize(full_im, (109, 115))
        images.append(full_im)
        labels.append(label)

        #plt.imshow(255 - full_im)
        #plt.pause(0.05)
Пример #5
0
    if len(sys.argv) != 2:
        print "Usage: python processSoundFile.py <filename.hdf5>"

    with open('labeling/windows.yaml', 'r') as f:
        windows = yaml.load(f)
    
    ######### DIRECTORIES #########
    dirs = {}
    dirs['networks'] = 'classifiers/trained_nets/'
    dirs['fihs'] = 'classifiers/fih'
    
    
    ######### READING FILE #########
    soundFile = sys.argv[1]
    filepointer = h5py.File(soundFile, 'r+')
    signals = files.signalsFromHDF5(soundFile)    
    
    
    ######### CLASSIFICATION  #########
    windowPredictions, filePredictions = classifyFile(dirs, soundFile, windows)
    # windowPredictions = pickle.load( open( "windowPredictions.pickle", "rb" ) )
    sudden = True
    loudness = True
    
    ######### Loudness and sudden features #########
    hist = makeWavelet(signals)
    
    if sudden:
        differenceFactors, loudnessFactors = getSuddenSounds(hist, signals)
        for index, window in enumerate(windowPredictions['windows']):
            diffFactor = float(sum(differenceFactors[window[0]:window[1]])) / float(window[1]-window[0])
Пример #6
0
import sys, glob, shutil
import h5py, numpy as np
import os, matplotlib.pyplot as plt

# Change search directory for modules to include pycpsp in parent
sys.path.insert(1, os.path.join(sys.path[0], '..'))
import pycpsp.files as files
import pycpsp.bgmodel.config as bgconfig
import pycpsp.bgmodel.bgmodel as bgmodel

if __name__ == '__main__':
    f = sys.argv[1]
    if os.path.isfile(f):
        # Read information from the hdf5
        signals = files.signalsFromHDF5(f)

        # Original signal
        original = signals['energy'][:, :]
        print original.shape
        # Get the zero/infinity columns out
        original = np.transpose(original)
        zeroColLeft = []
        zeroColRight = []
        while max(original[0]) <= 0:
            zeroColLeft.append(original[0])
            original = original[1:]
        while max(original[-1]) <= 0:
            zeroColRight.append(original[-1])
            original = original[:-2]
        original = np.transpose(original)
        print original.shape
Пример #7
0
    for model in bgmodels:
        response = model.calculate(refsignal)

        savePlot(response, 'tau {}'.format(model.name), filename, metadata, run)
        # plot.plot1D('frequency-averaged energy', np.sum(refsignal, axis=0) / refsignal.shape[0], ylim=[0,60])
        # plot.plot1D('frequency-averaged bg model tau:{}'.format(model.name), np.sum(response, axis=0) / refsignal.shape[0], ylim=[0,60])
        savePlot(refbg - response, 'tau 0.1 minus tau {}'.format(model.name), filename, metadata, run)#, mask=[0,20], scale=None)

if __name__ == '__main__':
    if len(sys.argv) < 1:
        print("usage:", sys.argv[0], "<name.hdf5>")
        sys.exit(0)

    filename = sys.argv[1]
    filepointer = h5py.File(filename, 'r')
    signals = files.signalsFromHDF5(filename)
    metadata = files.metadataFromHDF5(filename)

    # Original image
    original = signals['energy'][:, metadata.get('starttime 0'):]
    refsignal = plot.imgmask(plot.imscale(signals['energy'],[0,60]), [20,80])

    # preprocess images
    eroded = erode(refsignal)
    dilated = dilate(refsignal)
    combined = dilate(erode(refsignal))
    foregroundBackground(refsignal, f)

    # Save the plots
    savePlot(refsignal, 'refsignal', metadata)
    savePlot(original, 'original', metadata)
label = 0
images = []
labels = []
for subdir in dirs:
    d = os.path.join(root, subdir)
    hdf5dir = os.path.join(d, 'ptne/hdf5/2016-06-02')

    h5files = [f for f in os.listdir(hdf5dir) if '.wav.3' in f]

    if 'Pouring' in hdf5dir or 'Church bells' in hdf5dir or 'Glass' in hdf5dir:
        print 'skipping ', hdf5dir
        continue

    print 'Working on ', hdf5dir
    for h5file in h5files:
        signals = files.signalsFromHDF5(os.path.join(hdf5dir, h5file))
        energy = signals['energy']
        pulse = signals['tfmax']
        tone = signals['tsmax']

        max = np.max([tone, energy, pulse])
        min = np.min([tone, energy, pulse])

        full_im = (np.stack([tone, pulse, energy], axis=0).transpose((1, 2, 0)) - min) / (max - min)
        full_im = imresize(full_im, (109, 115))
        images.append(full_im)
        labels.append(label)

        #plt.imshow(255 - full_im)
        #plt.pause(0.05)
    label += 1
Пример #9
0
if __name__ == '__main__':
    if len(sys.argv) != 2:
        print "Usage: python processSoundFile.py <filename.hdf5>"

    with open('labeling/windows.yaml', 'r') as f:
        windows = yaml.load(f)

    ######### DIRECTORIES #########
    dirs = {}
    dirs['networks'] = 'classifiers/trained_nets/'
    dirs['fihs'] = 'classifiers/fih'

    ######### READING FILE #########
    soundFile = sys.argv[1]
    filepointer = h5py.File(soundFile, 'r+')
    signals = files.signalsFromHDF5(soundFile)

    ######### CLASSIFICATION  #########
    windowPredictions, filePredictions = classifyFile(dirs, soundFile, windows)
    # windowPredictions = pickle.load( open( "windowPredictions.pickle", "rb" ) )
    sudden = True
    loudness = True

    ######### Loudness and sudden features #########
    hist = makeWavelet(signals)

    if sudden:
        differenceFactors, loudnessFactors = getSuddenSounds(hist, signals)
        for index, window in enumerate(windowPredictions['windows']):
            diffFactor = float(sum(
                differenceFactors[window[0]:window[1]])) / float(window[1] -