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.
Below are the steps required for automatic identification of animals within camera trap images or videos.
First, build the file manifest of a given directory.
library(animl)
imagedir <- "examples/TestData"
#create save-file placeholders and working directories
setupDirectory(imagedir)
# Read exif data for all images within base directory
files <- buildFileManifest(imagedir)
# Set Region/Site/Camera names based on folder hierarchy
files <- setLocation(files,imagedir)
# Process videos, extract frames for ID
imagesall<-imagesFromVideos(files,outdir=vidfdir,frames=5)
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 Microsoft's 'MegaDector' object detector to first identify potential animals and then using a second classification model trained on the species of interest.
A version of MegaDetector compatible with tensorflow can obtained from our server.
More info on MegaDetector.
#Load the Megadetector model
mdsession<-loadMDModel("/path/to/megaDetector/mdv5_.pb")
#+++++++++++++++++++++
# Classify a single image to make sure everything works before continuing
testMD(imagesall,mdsession)
#+++++++++++++++++++++
# Obtain crop information for each image, checkpoint MegaDetector after every 2500 images
mdres <- classifyImagesBatchMD(mdsession,imagesall$Frame,resultsfile=paste0(datadir,mdresults),checkpoint = 2500)
# Add crop information to dataframe
imagesall <- parseMDsimple(imagesall, mdres)
Then feed the crops into the classifier. We recommend only classifying crops identified by MD as animals.
# Pull out animal crops
animals <- imagesall[imagesall$max_detection_category==1,]
# Set of crops with MD human, vehicle and empty MD predictions.
empty <- setEmpty(imagesall)
modelfile <- "/Models/Southwest/EfficientNetB5_456_Unfrozen_01_0.58_0.82.h5"
# Obtain predictions for each animal crop
pred<-classifySpecies(animals,modelfile,resize=456,standardize=FALSE,batch_size = 64,workers=8)
# Apply human-readable class name to dataframe
# Classes are stored as text file
# Returns a table with number of crops identified for each species
alldata <- applyPredictions(animals,empty,"/Models/Southwest/classes.txt",pred, counts = TRUE)
# Lastly pool crops to get one prediction per file
alldata <- poolCrops(alldata)
The Conservation Technology Lab has several models available for use.
- Southwest United States v3
- Amazon
- Savannah
- MegaDetector
- R >= 4.0
- Python >= 3.7
- Tensorflow >= 2.5
We recommend running animl on a computer with a dedicated GPU.
animl depends on python and will install python package dependencies if they are not available if installed via CRAN.
However, we recommend setting up a conda environment using the provided config file.
The file animl-env.yml describes the python version and various dependencies with specific version numbers. To create the enviroment, from within the animl directory run the following line in a terminal:
conda env create -f animl-env.yml
The first line creates the enviroment from the specifications file which only needs to be done once. This environment is also necessary for the python version of animl.
Kyra Swanson
Mathias Tobler
Edgar Navarro
Josh Kessler
Jon Kohler