Python Forum
Problems feeding live input from my microphone into a keras model (SegFault: 11)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems feeding live input from my microphone into a keras model (SegFault: 11)
#1
I'm trying to write a voice-activated goal horn in Python.

I'm using my own audio recordings to train the model, but I need my model to make predictions on live input. I believe I've figured out how to store my most recent audio data, but I run into problems when I try to extract the MFCC's.

import pyaudio
import librosa
import numpy as np

class Counter:
    def __init__(self):
        self.value = 0
    
    def add(self):
        self.value += 1
    
    def get(self):
        return self.value

class RingBuffer:
    """ class that implements a not-yet-full buffer """
    def __init__(self,size_max):
        self.max = size_max
        self.data = []

    class __Full:
        """ class that implements a full buffer """
        def append(self, x):
            """ Append an element overwriting the oldest one. """
            self.data[self.cur] = x
            self.cur = (self.cur+1) % self.max
        def get(self):
            """ return list of elements in correct order """
            return self.data[self.cur:]+self.data[:self.cur]

        def clear(self):
            self.data = []
            self.__class__ = RingBuffer


    def append(self,x):
        """append an element at the end of the buffer"""
        self.data.append(x)
        if len(self.data) == self.max:
            self.cur = 0
            # Permanently change self's class from non-full to full
            self.__class__ = self.__Full

    def get(self):
        return self.data

j = Counter()
ringBuffer = RingBuffer(1*22050)

def callback(in_data, frame_count, time_info, flag):
        
    audio_data = np.frombuffer(in_data, dtype=np.float32)
    audio_data = librosa.resample(audio_data, 44100, 22050)

    for i in audio_data:
        ringBuffer.append(i)

    signal = ringBuffer.get()
    signal = np.array(signal)

    if signal.shape[0] == 22050:
        print("\ncheckpoint 1")
        X_new = librosa.feature.mfcc(signal, sr=22050, n_mfcc=44, hop_length=512, n_fft=2048) # causes segfault: 11... WHY?
        print("checkpoint 2")
        print(j.get())

        j.add()
        
    return (in_data, pyaudio.paContinue)

pa = pyaudio.PyAudio()
stream = pa.open(format = pyaudio.paFloat32,
                 channels = 1,
                 rate = 44100,
                 output = False,
                 input = True,
                 stream_callback=callback)

stream.start_stream()

while stream.is_active():
    1
            
stream.close()
pa.terminate()

print("Program terminated. \n")

Output:
... checkpoint 1 checkpoint 2 136 checkpoint 1 checkpoint 2 137 checkpoint 1 checkpoint 2 138 checkpoint 1 Segmentation fault: 11
For some reason, when I call librosa.features.mfcc enough times (the exact number is inconsistent but always appears to be somewhere between 110 and 150), the program crashes. I've pinpointed the line that's producing the segfault, but I don't know why.

How can I resolve this issue?

Thank you.
Reply
#2
It turns out because I was calling librosa.feature.mfcc inside my callback function, that was causing the problem. I still don’t know exactly why that was an issue, but I solved the problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Keras.Predict into Dataframe Finpyth 13 1,209 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  Making a Basic Keras Model - Input Shape and Parameters MattKahn13 0 367 Aug-16-2020, 04:36 PM
Last Post: MattKahn13
  Error when import Keras Azadfalah 1 374 Apr-29-2020, 04:45 AM
Last Post: buran
  Keras + Matplotlib causing crash spearced 3 1,141 Feb-06-2020, 04:54 PM
Last Post: zljt3216
  Feeding new data to a model RawlinsCross 0 507 Feb-01-2020, 10:47 PM
Last Post: RawlinsCross
  Keras Dense layer with wrong input d1r4c 0 345 Jan-02-2020, 02:35 PM
Last Post: d1r4c
  Keras: Time series classification midarq 0 548 Sep-25-2019, 09:03 AM
Last Post: midarq
  Keras. ModuleNotFoundError: No module named 'error' agatte 1 3,456 Sep-18-2019, 12:27 PM
Last Post: shivani
  Keras Target Problem inco 1 1,147 May-05-2019, 10:47 AM
Last Post: scidam
  keras tensor mainpulation output to next input JohnyBeGood 0 720 Nov-21-2018, 03:30 PM
Last Post: JohnyBeGood

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020