So a i have these 2 functions: def audio_classification_thread(): p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("Audio Classification Running...") while not deactivate_camera and not deactivate_actual_microphone: label = classify_audio(stream) print(label[0]) print(int(label[1]*100)) if label[1]*100 > 50 and label[0] != "silence": if (label[0] == "dog"): send_alert(int(label[1]*100), "dog_audio","Audio") else: send_alert(int(label[1]*100), label[0],"Audio") print(f"Audio classified as: {label}") stream.stop_stream() stream.close() p.terminate() #This one below is not that important but to show for what this one above is used def classify_audio(stream): frames = [] for _ in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(np.frombuffer(data, dtype=np.int16)) audio_data_bytes = b''.join(frames) temp_filename = 'temp_audio.mp3' save_audio_to_mp3(audio_data_bytes, temp_filename) mel_spectrogram = create_spectrogram(temp_filename) mel_spectrogram = np.expand_dims(mel_spectrogram, axis=0) prediction = model.predict(mel_spectrogram) print(prediction) predicted_class = np.argmax(prediction, axis=1) predicted_probabilities = prediction[0] max_probability = np.max(predicted_probabilities) if max_probability >= 0.5: return folders[predicted_class[0]], max_probability else: return "Niciun Sunet", 0 The second one: def sp_re_thread(): nltk.download('stopwords') nltk.download('punkt') stop = set(stopwords.words('romanian')) with open("tokenizer.json", "r") as json_file: tokenizer_json = json.load(json_file) tokenizer = tokenizer_from_json(tokenizer_json) r = sr.Recognizer() model_nlp = load_model("nlp_model_shieldwave") while not deactivate_camera and not deactivate_actual_microphone: try: with sr.Microphone(device_index=int(usb_devices[-1])) as source: r.adjust_for_ambient_noise(source, duration=1) audio = r.listen(source, timeout=5) text = r.recognize_google(audio, language="ro-RO") sample_text = text sample_text_processed = preprocess_text(sample_text) sample_seq = tokenizer.texts_to_sequences([sample_text_processed]) sample_pad = pad_sequences(sample_seq, padding='post', maxlen=16) prediction = model_nlp.predict(sample_pad) print(f"{sample_text}") if prediction[0][0] > 0.5: label = "positive" else: label = "negative" if label == "positive": with open("output.wav", "wb") as f: f.write(audio.get_wav_data()) audio_segment = AudioSegment.from_wav("output.wav") audio_segment.export("output.mp3", format="mp3") send_alert(int(prediction[0][0]*100), "bad_intention", "Audio") except sr.UnknownValueError: print("❌ Nu s-a înțeles ce ai spus.") except sr.RequestError as e: print("❌ Eroare cu serviciul Google:", e) except Exception as e: print("❌ Alte probleme:", e) So i get ALSA erros like this one: JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock ALSA lib pcm_oss.c:397_snd_pcm_oss_open) Cannot open device /dev/dsp ALSA lib pcm_oss.c:397_snd_pcm_oss_open) Cannot open device /dev/dsp ALSA lib pcm_a52.c:1001_snd_pcm_a52_open) a52 is only for playback ALSA lib confmisc.c:1369snd_func_refer) Unable to find definition 'cards.0.pcm.iec958.0:CARD=0,AES0=6,AES1=130,AES2=0,AES3=2' ALSA lib conf.c:5180_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:5703snd_config_expand) Evaluate error: No such file or directory ALSA lib pcm.c:2666snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0} ALSA lib confmisc.c:160snd_config_get_card) Invalid field card ALSA lib pcm_usb_stream.c:482_snd_pcm_usb_stream_open) Invalid card 'card' ALSA lib confmisc.c:160snd_config_get_card) Invalid field card ALSA lib pcm_usb_stream.c:482_snd_pcm_usb_stream_open) Invalid card 'card' ALSA lib pcm_dmix.c:999snd_pcm_dmix_open) unable to open slave Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock And this one: Expression 'ret' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1736 Expression 'AlsaOpen( &alsaApi->baseHostApiRep, params, streamDir, &self->pcm )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1904 Expression 'PaAlsaStreamComponent_Initialize( &self->capture, alsaApi, inParams, StreamDirection_In, NULL != callback )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2171 Expression 'PaAlsaStream_Initialize( stream, alsaHostApi, inputParameters, outputParameters, sampleRate, framesPerBuffer, callback, streamFlags, userData )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2839 ❌ Alte probleme: 'NoneType' object has no attribute 'close' Audio classified as: ('footsteps', 0.9989391) Exception in thread Thread-1 (audio_classification_thread): Traceback (most recent call last): File "/home/andrei/.pyenv/versions/3.10.11/lib/python3.10/threading.py", line 1016, in _bootstrap_inner self.run() File "/home/andrei/.pyenv/versions/3.10.11/lib/python3.10/threading.py", line 953, in run self._target(*self._args, **self._kwargs) File "/home/andrei/Desktop/ShieldWaveAI/ShieldWaveAI/MachineLearning/full_program.py", line 352, in audio_classification_thread label = classify_audio(stream) File "/home/andrei/Desktop/ShieldWaveAI/ShieldWaveAI/MachineLearning/full_program.py", line 281, in classify_audio data = stream.read(CHUNK) File "/home/andrei/Desktop/ShieldWaveAI/ShieldWaveAI/MachineLearning/mlenv/lib/python3.10/site-packages/pyaudio/__init__.py", line 570, in read return pa.read_stream(self._stream, num_frames, OSError: [Errno -10000] PortAudio not initialized I also get error like NoneType has no attribute 'close', for my speech recognition program And this happens when the function with speech recognition is working, when is all commented and only the one with audio classification works, everything works well.But i want to try also to make a NLP classification on what people say. Some info here: andrei@raspberrypi:~/Desktop/ShieldWaveAI/ShieldWaveAI $ arecord -l **** List of CAPTURE Hardware Devices **** card 2: Camera [USB 2.0 Camera], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0 card 3: Camera_1 [USB 2.0 Camera], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0 and here i used pyaudio to list all devices: Device 0: vc4-hdmi-1: MAI PCM i2s-hifi-0 (hw:1,0) Device 1: USB 2.0 Camera: Audio (hw:2,0) Device 2: USB 2.0 Camera: Audio (hw:3,0) Device 3: pipewire Device 4: pulse Device 5: default The line int(usb_devices[-1]) takes the index of the last camera, or in my case the last index of the sequence that contains USB, which are my usb cameras with microphones.When the rpi is not connected to hdmi to a monitor the last index is 1 but when is connected is 2, like above. Some times it works, but the sp_re_thread freezes and doesnt listen anymore, sometimes it shows these errors and can completly crash my program.Those 2 functions run in parallal using threading on my rpi5. I want these 2 to work well at the same time. I have tried to turn on pulseaudio when the program starts, when the hdmi is on, it works, but when i remove the hdmi from the rpi5 it doesnt work, i dont know why.Can someone please help? Continue reading...