Ejemplo n.º 1
0
    def extract(self, path):
        index_of_sirens = []
        index_of_not_sirens = []
        rows = []
        files_frequencies_ar = []
        time = []
        sm = specgram_maker.SpecgramMaker()

        directory = os.listdir(path)
        l = 0  # used to count the index of the files.
        for filename in directory:
            if not filename.endswith(".wav"):
                continue
            if filename.__contains__("siren"):
                index_of_sirens.append(l)
            else:
                index_of_not_sirens.append(l)

            spec, freq, t = sm.get_specgram_data_from_wav(path, filename)
            for i in range(len(spec[1])):  # iterating over coloums.
                max_frequency_value = 0
                row = 0
                for j in range(FREQUENCY_INTEVALS
                               ):  # Finding the row with highest frequency.
                    if spec[j][i] > max_frequency_value:
                        max_frequency_value = spec[j][i]
                        row = j
                rows.append(50 * row)
            files_frequencies_ar.append(rows.copy())
            rows.clear()
            time.append(t)
            l += 1
        return files_frequencies_ar, time, index_of_sirens, index_of_not_sirens
Ejemplo n.º 2
0
def _find_vertical_maxima(path,
                          directory,
                          min_freq=750,
                          max_freq=1600,
                          split=True,
                          training=True):
    specmaker = sm.SpecgramMaker()
    labels = []
    directory = os.listdir(path)
    filenames = []
    time = []
    rows = []
    files_frequencies_array = []

    for filename in directory:
        # We only consider .wav-files
        if not filename.endswith(".wav"):
            continue
        filenames.append(filename)
        # We append whether or not the filename says there is a siren in the clip. There have been many ways to
        # indicate this, so this line is kind of long. :(
        labels.append("sirenAt" in filename or "siren" in filename
                      or "SPCSiren" in filename)

        # We convert the .wav-file into a matrix with specmaker.
        spec, freq, t = specmaker.get_specgram_data_from_wav(path, filename)

        # For each column:
        for col in range(len(spec[1])):
            max_dB = 0
            max_row = 0
            # For each row:
            for row in range(int(min_freq / 50), int(
                    max_freq /
                    50)):  # We divide with 50, because the frequencies
                # are in multiples of 50.
                # We find the highest value in the column.
                if spec[row][col] > max_dB:
                    max_dB = spec[row][col]
                    max_row = row
            # When we have found the highest column, we append that number times 50.
            # This is because the y-axis works in multiples of 50.
            rows.append([50 * max_row, max_dB])
        # files_frequencies_array now contains rows, which is a list of the highest values in each column.
        # I'm not sure, why the rows are copied and then cleared.
        files_frequencies_array.append(rows.copy())
        rows.clear()
        # We would like to keep track of the times for future use.
        time.append(t)
        # Here we print, to keep track of how long we are in importing.
        print("Done importing " + filename + ".", labels[len(time) - 1],
              str(len(time) * 100 / len(directory)) + " %")
    # This is where we split,, if split is true
    if split:
        # We call different methods dependant on whether it is used for training or not.
        if training:
            split_waves, labels = _split_training(files_frequencies_array,
                                                  labels, filenames, time,
                                                  divisions)
        else:
            split_waves, labels = _split_testing(files_frequencies_array,
                                                 labels, filenames, time,
                                                 divisions)
    # If we don't split, we simply copy it into split_waves.
    else:
        split_waves = files_frequencies_array

    new_waves = []
    for feature_set in split_waves:
        new_feature_set = []
        for tubel_element in feature_set:
            new_feature_set.append(tubel_element[0])
            new_feature_set.append(tubel_element[1])
        new_waves.append(new_feature_set)

    return new_waves, time, labels
Ejemplo n.º 3
0
import clip_split
import matplotlib.pyplot as plot
import specgram_maker as sm
import os

if __name__ == "__main__":
    spec = sm.SpecgramMaker()
    directory = "C:\\Users\\Magnus\\Desktop\\GetPatterns\\"
    for filename in os.listdir(directory):
        matrix, freq, t = spec.get_specgram_data_from_wav(directory, filename)
        min_freq = 14
        max_freq = 32
        rows = []
        for col in range(len(matrix[1])):  # iterating over coloums.
            max_dB = 0
            max_row = 0
            for row in range(
                    min_freq,
                    max_freq):  # Finding the row with highest frequency.
                if matrix[row][col] > max_dB:
                    max_dB = matrix[row][col]
                    max_row = row
            rows.append(50 * max_row)
        plot.scatter(t, rows)
        plot.xlabel("time / seconds")
        plot.ylabel("Frequency / Hz")
        plot.title(filename)
        plot.show()
if __name__ == "__main__":
    f = open("Log.txt", "wb")
    # GPIO.setwarnings(False)  # Ignore warning for now
    # GPIO.setmode(GPIO.BOARD)  # Use physical pin numbering
    # GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)  # Set the light to off as default
    is_recording = True  # A boolean value that runs the while loop below.
    # gau = pickle.load(open("Jamesnb.v.1.pkl", "rb"))
    lr = pickle.load(open("Jameslr.v.1.pkl", "rb"))
    ran = pickle.load(open("JamesrandomForest.v.1.pkl", "rb"))
    # svm = pickle.load(open("Jamessvm.v.1.pkl", "rb"))
    tree = pickle.load(open("Jamestree.v.1.pkl", "rb"))
    models = [[lr, "logistic regression"], [tree, "decision tree"],
              [ran, "random forest"]]
    mic = MicRecorder(20 / 6)
    stream = mic.get_stream()
    sgm = specgram_maker.SpecgramMaker()

    while is_recording:  # Vi bør finde en måde at gøre den False på
        rows = []
        predictions = []
        print("Begin recording")
        matrix, freq, t = sgm.make_specgram_from_mic_matrix(mic, stream)
        print("End recording")
        for col in range(len(matrix[1])):  # iterating over coloums.
            max_dB = 0
            max_row = 0
            for row in range(14,
                             32):  # Finding the row with highest frequency.
                if matrix[row][col] > max_dB:
                    max_dB = matrix[row][col]
                    max_row = row