Skip to content

Examples

Stefan Taubert edited this page Sep 2, 2024 · 3 revisions

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

from pathlib import Path

from birdnet import (SpeciesPredictions, predict_species_at_location_and_time,
                     predict_species_within_audio_file)

# predict species at location
species_in_area = 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(predict_species_within_audio_file(
  audio_path,
  species_filter=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 import SpeciesPredictions, predict_species_within_audio_file
from birdnet.models.v2m4 import CustomAudioModelV2M4TFLite

# 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(predict_species_within_audio_file(
  audio_path,
  custom_model=audio_model
))

# 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 import SpeciesPredictions, predict_species_within_audio_file
from birdnet.models.v2m4 import CustomAudioModelV2M4Raven

# 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(predict_species_within_audio_file(
  audio_path,
  custom_model=audio_model
))

# 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