Learn R Programming

animl v3.1.1

Animl comprises a variety of machine learning tools for analyzing ecological data. The package includes a set of functions to classify subjects within camera trap field data and can handle both images and videos.

Table of Contents

  1. Tips for Use
  2. Models
  3. Installation
  4. Release Notes

Tips for Use

Below are the steps required for automatic identification of animals within camera trap images or videos.

1. File Manifest

First, build the file manifest of a given directory.

library(animl)

imagedir <- "examples/TestData"

#create save-file placeholders and working directories
WorkingDirectory(imagedir, globalenv())

# Read exif data for all images within base directory
files <- build_file_manifest(imagedir, out_file=filemanifest_file, exif=TRUE)

# Process videos, extract frames for ID
allframes <- extract_frames(files, frames=3, out_file=imageframes_file,
                            parallel=T, num_workers=parallel::detectCores())

2. Object Detection

This produces a dataframe of images, including frames taken from any videos to be fed into the classifier. The authors recommend a two-step approach using the 'MegaDector' object detector to first identify potential animals and then using a second classification model trained on the species of interest.

More info on MegaDetector v5/v1000 MegaDetector v6

#Load the Megadetector model
detector <- load_detector("/Models/md_v5b.0.0.pt", model_type = 'mdv5', device='cuda:0')

# Obtain crop information for each image
mdraw <- detect(detector, allframes, resize_width=1280, resize_height=960, batch_size=4, device='cuda:0')

# Add crop information to dataframe
mdresults <- parse_detections(mdraw, manifest = allframes, out_file = detections_file)

3. Classification

Then feed the crops into the classifier. We recommend only classifying crops identified by MD as animals.

# Pull out animal crops
animals <- get_animals(mdresults)

# Set of crops with MD human, vehicle and empty MD predictions. 
empty <- get_empty(mdresults)

# load class list
classes <- load_class_list("/Models/Southwest/v3/southwest_v3_classes.csv")
class_list <- classes$class

# load the model
model_file <- "/Models/Southwest/v3/southwest_v3.pt"
southwest <- load_classifier(model_file, nrow(class_list))

# obtain species predictions likelihoods
pred_raw <- classify(southwest, animals, resize_width=480, resize_height=480, 
                     out_file=predictions_file, batch_size=16, num_workers=8)

# apply class_list labels and combine with empty set
manifest <- single_classification(animals, empty, pred_raw, class_list$class)

If your data includes videos or sequences, we recommend using the sequence_classification algorithm. This requires the raw output of the prediction algorithm.

# Sequence Classification
manifest <- sequence_classification(animals, empty=empty, pred_raw, classes=class_list,
                                    station_col="station", empty_class="empty")

4. Export

You can export the data into folders sorted by prediction:

manifest <- export_folders(manifest, out_dir=linkdir, out_file=results_file)

or into folders sorted by prediction and by station for export to camtrapR:

manifest <- export_camtrapR(manifest, out_dir=linkdir, out_file=results_file,
                            label_col='prediction', file_col="filepath", station_col='station')

You can also export a .json file formatted for COCO

manifest <- export_coco(manifest, class_list=class_list, out_file='results.json')

Or a .csv file for Timelapse

manifest <- export_folders(manifest, out_dir=linkdir)

Models

The Conservation Technology Lab has several models available for use. Detectors: MegaDetector v5/v1000 MegaDetector v6

Installation

Requirements

We recommend running animl on a computer with a dedicated GPU.

Python

animl depends on python and will install python package dependencies if they are not available if installed via miniconda.

The R version of animl also depends on the python version to handle the machine learning: animl-py

Animl-r can be installed through CRAN:

install.packages('animl')

Animl will install animl-py and associated dependencies.

Animl-r can also be installed by downloading this repo, opening the animl.Rproj file in RStudio and selecting Build -> Install Package.

Release Notes

New for 3.1.1

  • compatible with animl-py v3.1.1
  • add export_camtrapR()
  • handle on the fly video frame generation
  • bug fixes
  • correct examples and documentation to reflect above changes

Contributors

Kyra Swanson Mathias Tobler Edgar Navarro Josh Kessler Jon Kohler

Copy Link

Version

Install

install.packages('animl')

Monthly Downloads

798

Version

3.2.0

License

MIT + file LICENSE

Maintainer

Kyra Swanson

Last Published

February 4th, 2026

Functions in animl (3.2.0)

detect

Apply MegaDetector to a Given Batch of Images
extract_frames

Extract frames from video for classification
remove_link

Remove Sorted Links
remove_diagonal

Removes the diagonal elements from a square matrix.
test_main

Test a model with a Config file
single_classification

Get Maximum likelihood label for each Detection
train_main

Model Training
extract_miew_embeddings

Extract Embeddings from MiewID
save_data

Save Data to Given File
train_val_test

Splits the manifest into training validation and test datasets for training
save_classifier

Save model state weights
export_camtrapR

Export data into sorted folders organized by station
load_class_list

Load class list .csv file
load_classifier

Load a Classifier Model and Class_list
load_animl

Load animl-py if available
load_data

Load .csv or .Rdata file
download_model

Download specified model to the given directory.
sequence_classification

Leverage sequences to classify images
save_json

Save data to a JSON file.
plot_box

Plot bounding boxes on image from md results
get_frame_as_image

Given a video path, return a specific frame as an RGB image
plot_all_bounding_boxes

Plot all bounding boxes in a manifest
list_models

List available models for download.
parse_detections

Parse MD results into a simple dataframe
update_animl_py

Update animl-py version for the given environment
update_labels_from_folders

Udate Results from File Browser
load_detector

Load an Object Detector
load_json

Load data from a JSON file.
load_miew

Load MiewID model
classify

Infer Species for Given Detections
WorkingDirectory

Set Working Directory and Save File Global Variables
check_file

Check for files existence and prompt user if they want to load
export_coco

Converts the .csv file to a COCO-formatted .json file.
export_folders

Create SymLink Directories and Sort Classified Images
compute_batched_distance_matrix

Computes the distance matrix in a batched manner to save memory.
check_python

Check that the python version is compatible with the current version of animl-py
animl_install

Load animl-py if available
cosine_distance

Computes cosine distance of two sets of vectors
animl_install_instructions

Installation Instructions for animl-r Python dependencies
compute_distance_matrix

A wrapper function for computing distance matrix.
euclidean_squared_distance

Computes euclidean squared distance of two sets of vectors
create_pyenv

Install python if necessary and create the environment animl_env
build_file_manifest

File Management Module
delete_pyenv

Delete the animl_env environment
get_animals

Return a dataframe of only MD animals
get_empty

Return MD empty, vehicle and human images in a dataframe
export_timelapse

Converts the Manifests to a csv file that contains columns needed for TimeLapse conversion in later step
export_megadetector

Converts the .csv file to the MD-formatted .json file.