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)
<- "examples/TestData"
imagedir
#create save-file placeholders and working directories
setupDirectory(imagedir)
# Read exif data for all images within base directory
<- buildFileManifest(imagedir)
files
# Set Region/Site/Camera names based on folder hierarchy
<- setLocation(files,imagedir)
files
# Process videos, extract frames for ID
<-imagesFromVideos(files,outdir=vidfdir,frames=5) imagesall
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.
MegaDetector can obtained from https://github.com/microsoft/CameraTraps/blob/main/megadetector.md
#Load the Megadetector model
<-loadMDModel("/path/to/megaDetector/megadetector_v4.1.pb")
mdsession
#+++++++++++++++++++++
# 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
<- classifyImagesBatchMD(mdsession,imagesall$Frame,resultsfile=paste0(datadir,mdresults),checkpoint = 2500)
mdres
# Add crop information to dataframe
<- parseMDsimple(imagesall, mdres) imagesall
Then feed the crops into the classifier. We recommend only classifying crops identified by MD as animals.
# Pull out animal crops
<- imagesall[imagesall$max_detection_category==1,]
animals
# Set of crops with MD human, vehicle and empty MD predictions.
<- setEmpty(imagesall)
empty
<- "/Models/Southwest/EfficientNetB5_456_Unfrozen_01_0.58_0.82.h5"
modelfile
# Obtain predictions for each animal crop
<-classifySpecies(animals,modelfile,resize=456,standardize=FALSE,batch_size = 64,workers=8)
pred
# Apply human-readable class name to dataframe
# Classes are stored as text file
# Returns a table with number of crops identified for each species
<- applyPredictions(animals,empty,"/Models/Southwest/classes.txt",pred, counts = TRUE)
alldata
# Lastly pool crops to get one prediction per file
<- poolCrops(alldata) alldata
All of our pre-trained classification models can be obtained at [https://]
Geographical regions represented: * South America * African Savanna * Southwest United States
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.