Skip to main content

Segment Animals

Segment Animals is a Python package for segmenting (extracting) animals from images using deep learning models. It provides a pipeline that combines object detection and segmentation to identify and extract animals from images, making it useful for wildlife research, conservation efforts, and any application where you wish to remove the background from images containing animals.

Segment Animals builds upon the Segment Anything 2 (SAM 2.1) and MegaDetector models.

Installation

Install Segment Animals and its dependencies with pip:

pip install segment-animals

Usage

Here's a quick example of how to use Segment Animals, for a more detailed guide refer to the notebook.

Importing the library and processing an image

from segment_animals import AutoAnimalSegmenter
from segment_animals.util import load_image

model = AutoAnimalSegmenter()

image = load_image("path/to/your/image.jpg")

detections, masks = model.process_image(image)
print(f"Found {len(detections)} animals.")

Choosing a segmentation model

The default is sam2.1_hiera_large. For a smaller model, pass segmentation_model_name when creating the pipeline:

model = AutoAnimalSegmenter(segmentation_model_name="sam2.1_hiera_tiny")

Available models are sam2.1_hiera_tiny, sam2.1_hiera_small, sam2.1_hiera_base_plus, and sam2.1_hiera_large. Checkpoints are downloaded on first use through Hugging Face Transformers and reused from the Hugging Face cache. Set HF_HOME to choose a different cache location. The former vit_h, vit_l, and vit_b names are no longer supported.

For development, install dependencies with uv sync. CPU, CUDA, and MPS devices can be selected with segmentation_device.

Visualizing detections and masks

from segment_animals.viz import plot_detections_and_masks

plot_detections_and_masks(image, detections, masks)

You should then see a visualisation along the lines of this (original image from Wikipedia)...

Example Segmentation

Extracting and saving masks

from segment_animals.viz import extract_masks

# Setting whole_image to False will return individual masks cropped to the extent
# of the predicted masks.
for i, mask_extract in enumerate(extract_masks(image, masks, whole_image=False)):
    # mask_extract is a PIL Image object so you can save it or manipulate it further
    mask_extract.save(f"animal_mask_{i}.png")

Resulting in something like this:

Example Mask

Working with Segment Animals?

It'd be great to hear how you're using Segment Animals! Drop me a line at Benjamin.Evans at ioz.ac.uk or open an issue on the GitHub repository.

Metadata

Release files for segment-animals 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for segment-animals 2.0.0
File Size Uploaded
segment_animals-2.0.0.tar.gz 1.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for segment-animals 2.0.0
File Interpreter ABI Platform
segment_animals-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.6 MB

Release files / segment_animals-2.0.0.tar.gz

Download URL segment_animals-2.0.0.tar.gz
Size 1.6 MB
Tags Source
SHA-256 checksum
How to use checksums
f64ad69bd320c857407db3dc52de466390951f5d44d8748eb965865bde1cca51
BLAKE2b-256 checksum
How to use checksums
832410a1f213fa560d7f00a3ed518610225476a916dcb1eb9dcf56bdbd5c7d79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / segment_animals-2.0.0-py3-none-any.whl

Download URL segment_animals-2.0.0-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f97d98bbc0c300ffae775192cbfe3b8619ae5b862d1355d5090467deacfc3f7b
BLAKE2b-256 checksum
How to use checksums
0de05cbea730a4cf9b33a8340433fe2bb524ed22080d1b0552ce2e503016861d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

2.1.0

2 release files

This release

2.0.0 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page