Skip to main content

A python api for BirdNET-Lite and BirdNET-Analyzer

Project description

birdnetlib

PyPI Test

A python api for BirdNET-Analyzer and BirdNET-Lite

Installation

birdnetlib requires Python 3.9+ and prior installation of Tensorflow Lite, librosa and ffmpeg. See BirdNET-Analyzer for more details on installing the Tensorflow-related dependencies.

pip install birdnetlib

Documentation

birdnetlib provides a common interface for BirdNET-Analyzer and BirdNET-Lite.

Using BirdNET-Analyzer

To use the newer BirdNET-Analyzer model, use the Analyzer class.

from birdnetlib import Recording
from birdnetlib.analyzer import Analyzer
from datetime import datetime

# Load and initialize the BirdNET-Analyzer models.
analyzer = Analyzer()

recording = Recording(
    analyzer,
    "sample.mp3",
    lat=35.4244,
    lon=-120.7463,
    date=datetime(year=2022, month=5, day=10), # use date or week_48
    min_conf=0.25,
)
recording.analyze()
print(recording.detections)

recording.detections contains a list of detected species, along with time ranges and confidence value.

[{'common_name': 'House Finch',
  'confidence': 0.5744,
  'end_time': 12.0,
  'scientific_name': 'Haemorhous mexicanus',
  'start_time': 9.0},
 {'common_name': 'House Finch',
  'confidence': 0.4496,
  'end_time': 15.0,
  'scientific_name': 'Haemorhous mexicanus',
  'start_time': 12.0}]

Using a custom classifier with BirdNET-Analyzer

To use a model trained with BirdNET-Analyzer, pass your labels and model path to the Analyzer class.

from birdnetlib import Recording
from birdnetlib.analyzer import Analyzer

# Load and initialize BirdNET-Analyzer with your own model/labels.

custom_model_path = "custom_classifiers/trogoniformes.tflite"
custom_labels_path = "custom_classifiers/trogoniformes.txt"

analyzer = Analyzer(
    classifier_labels_path=custom_labels_path, classifier_model_path=custom_model_path
)

recording = Recording(
    analyzer,
    "sample.mp3",
    min_conf=0.25,
)
recording.analyze()
print(recording.detections)

Using BirdNET-Lite

To use the legacy BirdNET-Lite model, use the LiteAnalyzer class.

Note: The BirdNET-Lite project has been deprecated. The BirdNET-Lite model is no longer included in the PyPi birdnetlib package. This model and label file will be downloaded and installed the first time the LiteAnalyzer is initialized in your Python environment.

from birdnetlib import Recording
from birdnetlib.analyzer_lite import LiteAnalyzer
from datetime import datetime

# Load and initialize the BirdNET-Lite models.
# If this is the first time using LiteAnalyzer, the model will be downloaded into your Python environment.
analyzer = LiteAnalyzer()

recording = Recording(
    analyzer,
    "sample.mp3",
    lat=35.4244,
    lon=-120.7463,
    date=datetime(year=2022, month=5, day=10), # use date or week_48
    min_conf=0.25,
)
recording.analyze()
print(recording.detections) # Returns list of detections.

Utility classes

DirectoryAnalyzer

DirectoryAnalyzer can process a directory and analyze contained files.

def on_analyze_complete(recording):
    print(recording.path)
    pprint(recording.detections)

directory = DirectoryAnalyzer(
    "/Birds/mp3_dir",
    patterns=["*.mp3", "*.wav"]
)
directory.on_analyze_complete = on_analyze_complete
directory.process()

See the full example for analyzer options and error handling callbacks.

DirectoryMultiProcessingAnalyzer

DirectoryMultiProcessingAnalyzer can process a directory and analyze contained files, using multiple processes asynchronously.

def on_analyze_directory_complete(recordings):
    for recording in recordings:
        pprint(recording.detections)

directory = "."
batch = DirectoryMultiProcessingAnalyzer(
    "/Birds/mp3_dir",
    patterns=["*.mp3", "*.wav"]
)

batch.on_analyze_directory_complete = on_analyze_directory_complete
batch.process()

See the full example for analyzer options and error handling callbacks.

DirectoryWatcher

DirectoryWatcher can watch a directory and analyze new files as they are created.

def on_analyze_complete(recording):
    print(recording.path)
    pprint(recording.detections)

watcher = DirectoryWatcher("/Birds/mp3_dir")
watcher.on_analyze_complete = on_analyze_complete
watcher.watch()

See the full example for analyzer options and error handling callbacks.

SpeciesList

SpeciesList uses BirdNET-Analyzer to predict species lists from location and date.

species = SpeciesList()
species_list = species.return_list(
    lon=-120.7463, lat=35.4244, date=datetime(year=2022, month=5, day=10)
)
print(species_list)
# [{'scientific_name': 'Haemorhous mexicanus', 'common_name': 'House Finch', 'threshold': 0.8916686}, ...]

Additional examples

About BirdNET-Lite and BirdNET-Analyzer

birdnetlib uses models provided by BirdNET-Lite and BirdNET-Analyzer under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License.

BirdNET-Lite and BirdNET-Analyzer were developed by the K. Lisa Yang Center for Conservation Bioacoustics at the Cornell Lab of Ornithology.

For more information on BirdNET analyzers, please see the project repositories below:

BirdNET-Analyzer

BirdNET-Lite

birdnetlib is not associated with BirdNET-Lite, BirdNET-Analyzer or the K. Lisa Yang Center for Conservation Bioacoustics.

About birdnetlib

birdnetlib is maintained by Joe Weiss. Contributions are welcome.

Project Goals

  • Establish a unified API for interacting with Tensorflow-based BirdNET analyzers
  • Enable python-based test cases for BirdNET analyzers
  • Make it easier to use BirdNET in python-based projects
  • Make it easier to migrate to new BirdNET versions/models as they become available

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

birdnetlib-0.8.0.tar.gz (54.0 MB view details)

Uploaded Source

Built Distribution

birdnetlib-0.8.0-py3-none-any.whl (54.0 MB view details)

Uploaded Python 3

File details

Details for the file birdnetlib-0.8.0.tar.gz.

File metadata

  • Download URL: birdnetlib-0.8.0.tar.gz
  • Upload date:
  • Size: 54.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for birdnetlib-0.8.0.tar.gz
Algorithm Hash digest
SHA256 98b80900e7a058de953c4d2fd06f70c9f3abee5301ea987c573ac4434efd220d
MD5 d9ff05465f2c37db66ec5fd805207853
BLAKE2b-256 0deed3b75849b2a2169867adf45b745eca2e385ea289c4550dbac2bb48b3691c

See more details on using hashes here.

File details

Details for the file birdnetlib-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: birdnetlib-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 54.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for birdnetlib-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e246d460ec9365b751c32b53da43a0116d2d661c006eba3c68f2716254258f41
MD5 f35a449bda56fff517fd6f11239e6296
BLAKE2b-256 f00b866fa75181dfaa6bf46c131c475934fd7816f601e9638087fc7c310ecdfa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page