Skip to content

Segment (Extract) Animals from Images - Removing Background

Notifications You must be signed in to change notification settings

bencevans/segment-animals

Repository files navigation

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

fromsegment_animalsimportAutoAnimalSegmenterfromsegment_animals.utilimportload_imagemodel=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

fromsegment_animals.vizimportplot_detections_and_masksplot_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

fromsegment_animals.vizimportextract_masks# Setting whole_image to False will return individual masks cropped to the extent# of the predicted masks.fori, mask_extractinenumerate(extract_masks(image, masks, whole_image=False)): # mask_extract is a PIL Image object so you can save it or manipulate it furthermask_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.

About

Segment (Extract) Animals from Images - Removing Background

Resources

Stars

Watchers

Forks

Sponsor this project