Skip to main content

Segment (Extract) Animals from Images - Removing Background

Project description

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 and MegaDetector models.

Installation

You can install Segment Animals using 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.")

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.

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

segment_animals-1.1.0.tar.gz (1.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

segment_animals-1.1.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file segment_animals-1.1.0.tar.gz.

File metadata

  • Download URL: segment_animals-1.1.0.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

File hashes

Hashes for segment_animals-1.1.0.tar.gz
Algorithm Hash digest
SHA256 0aab811c321ec9a83a5d6bba9c55e6747e72fd267951f5b49a75fc04ed81eac1
MD5 a6489c8cf3f39983e05dd675a63bc0f0
BLAKE2b-256 4e7010346a25cb26eea14fe82f618ad2c41e4437076837712435c430b7130e22

See more details on using hashes here.

File details

Details for the file segment_animals-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: segment_animals-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

File hashes

Hashes for segment_animals-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c2bd13b4effa05b305f6443993fb3d2b9bcc670864e1d88415a5a0843e7cd0a
MD5 109261d2408b1cb2341168f32638012a
BLAKE2b-256 d6850c7da88c3b662b39df7aa6760465dc913844b897af51953a6b97549d1f8c

See more details on using hashes here.

Supported by

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