This repository is a small working project for running wildlife detection and classification on folders of camera-trap images and videos.
The original work comes from the excellent work of AgentMorris (Dan Morris) you can follow him for updates
https://github.com/agentmorris
https://github.com/agentmorris/MegaDetector
Thanks to Google for funding SpeciesNet and Dan Morris for being the primary contributor to the opensource repository.
https://github.com/google/cameratrapai
This particular repository is an adaptation of all the above mentioned work and represents the workflow that I adapted for Michael Anderson's Serengeti camera trap work at Wake Forest University.
https://www.tmandersonlab.com/
It is built around two models:
- MegaDetector for object detection
- SpeciesNet for taxonomic classification of MegaDetector crops
The repo contains a plain MegaDetector batch runner, a combined MegaDetector + SpeciesNet pipeline, and an EXIF-enabled variant that can preserve image timestamps in the final JSON output.
Use this project when you want to:
- run MegaDetector on a folder of images or videos
- classify detected animals with SpeciesNet
- reuse an existing MegaDetector JSON instead of rerunning detection
- include EXIF-derived image timestamps in the final output
The main practical output is a JSON file in MegaDetector-style format with added SpeciesNet classification fields.
- run_detector_batch.py: local MegaDetector batch runner
- run_md_and_speciesnet.py: combined detection + classification pipeline
- run_md_and_speciesnet_exif.py: combined pipeline with optional EXIF timestamp extraction
- run_season.sh: simple shell workflow that runs detection first, then classification using an existing detections file
- total_detection.sh: simple shell wrapper around the EXIF-enabled combined pipeline
- RUN_MD_AND_SPECIESNET.md: detailed documentation for the original combined pipeline
The combined scripts follow this sequence:
- Scan the source folder for images and optionally videos.
- Run MegaDetector to produce bounding boxes.
- For each above-threshold detection, crop and preprocess the region.
- Run SpeciesNet on those crops.
- Merge classification results back into the MegaDetector JSON.
- Write a final combined results file.
For video inputs, MegaDetector operates on sampled frames. SpeciesNet classifications are attached to the detections associated with those frames.
This project assumes:
- Python 3.11 is available
- the
megadetectorPython package and its internal modules are importable - the
speciesnetpackage is installed for classification - model dependencies required by both packages are available in your environment
The shell scripts in this repository assume a local virtual environment at:
./.venvIf you are not using that layout, run the Python entry points directly with the interpreter from your own environment.
python run_detector_batch.py MDv1000-redwood /path/to/images /path/to/output.md.json \
--recursive --include_image_timestamp --threshold 0.05python run_md_and_speciesnet.py /path/to/images /path/to/output.sn.jsonpython run_md_and_speciesnet_exif.py /path/to/images /path/to/output.sn.json \
--detector_model MDv1000-redwood \
--include_image_timestamppython run_md_and_speciesnet_exif.py /path/to/images /path/to/output.sn.json \
--detections_file /path/to/output.md.json \
--include_image_timestampIn that mode, the EXIF-enabled wrapper backfills missing image-level datetime values from the source image files when possible.
The final combined JSON keeps the MegaDetector structure and adds SpeciesNet classification output per detection.
Typical additions include:
classificationsraw_classificationswhen requestedinfo.classifierclassification_categoriesclassification_category_descriptions
When --include_image_timestamp is supplied to run_md_and_speciesnet_exif.py, image records may also contain:
"datetime": "2024:09:24 07:28:16"That value is derived from EXIF DateTimeOriginal when available.
This is the original combined pipeline.
- Default detection model:
MDV5A - Default classifier model: SpeciesNet default model
- Does not include EXIF timestamps in its output
This is the preferred combined pipeline if you need image timestamps in the output.
- Adds
--include_image_timestamp - Preserves image-level
datetimemetadata in the final JSON - Can also populate timestamps when classifying from an existing MegaDetector detections file
This script runs:
- MegaDetector first
- then
run_md_and_speciesnet.pyusing--detections_file
It also applies:
--include_image_timestampin the detection phase--country TZAin the classification phase
This is a minimal shell wrapper around run_md_and_speciesnet_exif.py with:
- detector model forced to
MDv1000-redwood --include_image_timestampenabled
- The repository is a working project, not a packaged application.
- The shell scripts contain hard-coded assumptions about environment and output locations.
- Video EXIF-style timestamps are not produced by the combined pipeline.
- The original combined script and the EXIF-enabled variant are similar but not identical; use the EXIF-enabled script when timestamp preservation matters.
For most new runs, use:
python run_md_and_speciesnet_exif.py <source_folder> <output_json> \
--detector_model MDv1000-redwood \
--include_image_timestampThat gives you:
- one-step detection + classification
- SpeciesNet-enriched output
- EXIF-derived image timestamps when present
For a deeper description of the original combined pipeline, see RUN_MD_AND_SPECIESNET.md.