Hi everyone ! Hope all are fine.
Today, we're going to try BirdNet. I've already introduced it in my last post. They have a Github repository where they have some script to process simple audio files using the models that anyone can use .
Today, we're going to install it on a Raspberry PI 4 and analyze some recordings - as well some I've already done - and see what we get ; If we get something.
BirdNet on a Raspberry PI 4
First, I'm going to try BirdNet model on a Raspberry PI. They already have the tools build to run inference on the audio files.
Why try here first ?
Because this way, I get a sense on the workings of the model and see if the audio files need some, or have some processing before being submitted to the model . It's like a trial run before going for a small device like the Pico.
If the files need some processing, what kind of processing ? Can the Pico do this ? These are the kind of questions that need answers.
Installation
Python3 must be the default version.
Let's create a virtual environment with PIP to install and use birdNet.
Install some packages
sudo apt-get install python3-pip ffmpeg python3-venv
Create the virtual environment
python -m venv birdNet
activate the virtual environment
source birdNet/bin/activate
We know we have it active, because the name it's appended to the bash
Let's now install some more packages with PIP
update PIP
python -m pip install --upgrade pip
Install wheel because it helps
pip install wheel
pip install librosa tflite-runtime
If everything goes smoothly, we have what we need to start using the model to discover what bird is singing.
Clone the git repository and enter it
sudo apt-get install git
git clone https://github.com/kahst/BirdNET-Analyzer.git
cd BirdNet-Analyzer
Now, let's try to analyze some samples - they have an example directory that we can use.
Let's see what's inside the directory
ls example/
They have an audio wave with some samples (I've attached it to this post for your consultation), a txt file with some species (species_list.txt) and the result of the analysis of the wave file in a file called soundscape.BirdNET.selection.table.txt.
We can delete this file . This is the result generated when using the example audio.
To analyze the file and see what birds are in the sound file, just issue the command:
python analyze.py (in the root of the directory)
Without any arguments, it uses the example directory
But, this is where it gives an error
(birdNET) pi@raspberrypi:~/birdNET/BirdNET-Analyzer $ python analyze.py
Species list contains 2434 species
Found 1 files to analyze
Analyzing example/soundscape.wav
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/lib/python3.9/multiprocessing/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "/usr/lib/python3.9/multiprocessing/pool.py", line 48, in mapstar
return list(map(*args))
File "/home/pi/birdNET/BirdNET-Analyzer/analyze.py", line 238, in analyzeFile
chunks = getRawAudioFromFile(fpath)
File "/home/pi/birdNET/BirdNET-Analyzer/analyze.py", line 206, in getRawAudioFromFile
The error is long, but the main issue I'm guessing it's here:
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.core.typeinfer.CallConstraint object at 0xaa8cccb8>.
Failed in nopython mode pipeline (step: native lowering)
The 'parallel' target is not currently supported on 32 bit hardware.
The bullseye version that I'm running is 32bits.
There's nothing on the BirdNET documentation about this.
I've tried it on my computer (Arch Linux x86_64) and everything went well.
Now, we have the same files already listed above with the species found on the wave file. This is just an example:
The last column is the confidence the model has about a specific bird .
Solving the Raspberry PI problem
To solve this, I went and installed the 64bit version of the bullseye .
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 5.15.32-v8+ #1538 SMP PREEMPT Thu Mar 31 19:40:39 BST 2022 aarch64 GNU/Linux
After updating for the 64bit version, here's the result :
It worked flawlessly . A bit slow (compared with my machine), but it worked.
Now that we have the BirdNET working, let's analyze some personal samples.
For this project, I've collected some audio recordings from birds around me - anytime I could . I have 3 samples, recorded with my mobile phone.
I've created another directory, called example2, with the recordings.
Because the BirdNET analyzer.py only works with certain audio files, I've converted the aac files to wav using faad.
for i in *.aac; do faad $i; done
Now I also have the files in wav format.
Let's see what the BirdNET detects
We can give a directory as input and a directory as output also, where it will keep the text file with the results. Remember, you need the virtual environment active.
python analyze.py --i example2/ --o example2/
Species list contains 2434 species
Found 3 files to analyze
Analyzing example2/sample1.wav
Analyzing example2/sample2.wav
Analyzing example2/sample3.wav
INFO: INFO: INFO: Created TensorFlow Lite XNNPACK delegate for CPU.Created TensorFlow Lite XNNPACK delegate for CPU.
Created TensorFlow Lite XNNPACK delegate for CPU.
Finished example2/sample2.wav in 49.35 seconds
Finished example2/sample1.wav in 49.43 seconds
Finished example2/sample3.wav in 49.60 seconds
Let's see what we got.
cd example2
ls
For each wav file, we have a text file with the results - that are the identification of the birds singing.
Those are recordings of house sparrows - "Pardais" like we call them here in Portugal.
These results are amazing. If you hear the recordings, especially sample3, there's a lot of noise in the background. Call me impressed.
This is a new example, from a earlier post of mine - /challenges-projects/design-challenges/pi-fest/b/blog/posts/songspire---data-augmentation-for-audio with a Cuckoo recording.
(birdNET) pi@raspberrypi:~/birdNET/BirdNET-Analyzer $ python analyze.py --i example2/Cuckoo.wav --o example2/Cuckoo.txt
Species list contains 2434 species
Analyzing example2/Cuckoo.wav
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Finished example2/Cuckoo.wav in 28.32 seconds
And the result is:
(birdNET) pi@raspberrypi:~/birdNET/BirdNET-Analyzer/example2 $ cat Cuckoo.txt
Selection View Channel Begin Time (s) End Time (s) Low Freq (Hz) High Freq (Hz) Species Code Common Name Confidence
1 Spectrogram 1 1 0 3.0 150 12000 comcuc Common Cuckoo 0.9995
2 Spectrogram 1 1 3.0 6.0 150 12000 comcuc Common Cuckoo 0.9978
with a confidence of almost 1. Perfect !
Next steps
Now that I know that the BirdNET works well in identifying the birds, the next step is to record audio on the Pico . Without that, we have nothing.
Stay tuned !
Resources
https://github.com/kahst/BirdNET-Analyzer