Skip to content

Examples

Stefan Taubert edited this page Aug 27, 2024 · 3 revisions

Predict species for a given location and time

from birdnet.models.v2m4 import MetaModelV2M4

# create meta model instance for v2.4
meta_model = MetaModelV2M4()

# predict species
prediction = meta_model.predict_species_at_location_and_time(42.5, -76.45, week=4)

# get most probable species
first_species, confidence = list(prediction.items())[0]
print(f"predicted '{first_species}' with a confidence of {confidence:.2f}")
# output:
# predicted 'Cyanocitta cristata_Blue Jay' with a confidence of 0.93

Predict species within an audio file for a given location and time

from pathlib import Path

from birdnet.models.v2m4 import AudioModelV2M4, MetaModelV2M4
from birdnet import SpeciesPredictions

# create model instances for v2.4
audio_model = AudioModelV2M4()
meta_model = MetaModelV2M4()

# predict species at location
species_in_area = meta_model.predict_species_at_location_and_time(42.5, -76.45, week=4)

# predict species within the whole audio file
audio_path = Path("example/soundscape.wav")
predictions = SpeciesPredictions(audio_model.predict_species_within_audio_file(
  audio_path,
  filter_species=set(species_in_area.keys())
))

# get most probable prediction at time interval 0s-3s
prediction, confidence = list(predictions[(0.0, 3.0)].items())[0]
print(f"predicted '{prediction}' with a confidence of {confidence:.2f}")
# output:
# predicted 'Poecile atricapillus_Black-capped Chickadee' with a confidence of 0.81

Identify species within an audio file using a custom classifier (TFLite)

from pathlib import Path

from birdnet.models.v2m4 import CustomAudioModelV2M4TFLite
from birdnet import SpeciesPredictions

# create audio model instance for v2.4
classifier_folder = Path("src/birdnet_tests/test_files/v2m4/custom_model_tflite")
audio_model = CustomAudioModelV2M4TFLite(classifier_folder, "CustomClassifier")

# predict species within the whole audio file
audio_path = Path("example/soundscape.wav")
predictions = SpeciesPredictions(audio_model.predict_species_within_audio_file(audio_path))

# get most probable prediction at time interval 0s-3s
prediction, confidence = list(predictions[(0.0, 3.0)].items())[0]
print(f"predicted '{prediction}' with a confidence of {confidence:.2f}")
# output:
# predicted 'Poecile atricapillus_Black-capped Chickadee' with a confidence of 0.83

Identify species within an audio file using a custom classifier (Raven)

from pathlib import Path

from birdnet.models.v2m4 import CustomAudioModelV2M4Raven
from birdnet import SpeciesPredictions

# create audio model instance for v2.4
classifier_folder = Path("src/birdnet_tests/test_files/v2m4/custom_model_raven")
audio_model = CustomAudioModelV2M4Raven(classifier_folder, "CustomClassifier")

# predict species within the whole audio file
audio_path = Path("example/soundscape.wav")
predictions = SpeciesPredictions(audio_model.predict_species_within_audio_file(audio_path))

# get most probable prediction at time interval 0s-3s
prediction, confidence = list(predictions[(0.0, 3.0)].items())[0]
print(f"predicted '{prediction}' with a confidence of {confidence:.2f}")
# output:
# predicted 'Poecile atricapillus_Black-capped Chickadee' with a confidence of 0.83
Clone this wiki locally