Editing metadata in trail camera images using R, magick and exiftool
Posted on October 25, 2022 by nsaunders in R bloggers | 0 Comments
[This article was first published on R – What You're Doing Is Rather Desperate , and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here )
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Share Tweet
I have a new hobby: camera traps, also known as trail cameras. Strapped to trees in my local bushland they sit in wait, firing automatically when triggered by a passing animal. Once in a while, something quite magical happens.
The camera model I chose is the Campark T85 which for me, had the right combination of features and price point. One useful feature is the ability to transfer images and video to a phone wirelessly (albeit through a rather clunky phone app). Unfortunately, images retrieved in this way have one major flaw: an almost-complete absence of metadata. There is no GPS in the camera of course, but the EXIF data does not include the date/time of the image, nor the camera make.
With a little research, I found a way to add this information to the images later using R and some additional software named exiftool. Here’s how I did it.
Set up a directory structure
First, I create a directory, ~/Documents/Campark85, containing files and directories that look like this:
There’s an R script – more of that later, and a CSV file in the data directory – more of that later too. Under the cameras directory is a directory for each camera and in those directories, a directory for each deployment. A deployment basically describes the placement and location of the camera and has an ID, corresponding to the directory name.
Within each deployment directory are the image and video files retrieved from the camera.
Create a deployment data file
The deployments.csv file looks like this:
camera_id,deployment_id,latitude,longitude,elevation tp85-001,bvnp-dep001,-33.XX,151.XX,159 tp85-002,bvnp-dep011,-33.XX,151.XX,115
except that ‘XX’ is a decimal value in the real file – I’m not going to publish the locations of my cameras. The key features of the file are: (1) the deployment ID, which matches the directory name where the image and video files live, and (2) the coordinates and elevation, obtained from an image of the deployment site taken on an iPhone, which will be used to create the missing GPS metadata for the trail camera files.
Read files and filenames into R
Alright, let’s get into the R code. Load the required libraries, then read in the deployment data CSV, and create a data frame with a column that contains the full path to the image and video files for each deployment.
library(dplyr) library(readr) library(purrr) library(stringr) library(magick) library(gtools) # get deployment data deployments % select(-FrameFile) %>% write_csv("~/Documents/CamparkT85/exiftool_mp4.csv")
We should inspect the CSV file manually and edit any errors: the OCR is usually very good, but does occasionally make mistakes. The year 2022 may become 2027, for example.
Now we can get into exiftool. This is a terrific piece of open-source software for manipulating metadata in a wide variety of image and video formats. It has far too many features to go into here, so all we need to know is that you can pass a properly-formatted CSV file (which we just made) and an input directory to exiftool, and it will apply the metadata attributes from the CSV file to the image or video files in the directory. For example:
exiftool -csv=exiftool_jpg.csv -overwrite_original cameras/t85-002/bvnp-dep011/
Provided that the first column, SourceFile, contains the complete absolute path to the files, exiftool will run successfully from within any directory, after emitting a warning if the path doesn’t match the current directory, and finish by printing the number of files that were altered.
But did it work? We can use exiftool to look inside the files and return only the tags we specify:
exiftool -DateTimeOriginal -Model -GPSPosition cameras/t85-002/bvnp-dep011/2022-10-09\ 13.09.58.jpg Date/Time Original : 2022:10:07 07:40:12 Camera Model Name : Campark T85 GPS Position : 33 deg 40' 34.43" S, 151 deg 5' 37.89" E
Looks good! The final test: does the date/time and GPS metadata display correctly in Mac Photos, which was a major reason for embarking on this project in the first place.
Success! And sadly, another feral fox roaming the bushland. Drop a camera almost anywhere and one will turn up within days.
In summary: R + magick + exiftool, an excellent combination for adding or correcting metadata in your image files.
Related