Welcome to the MegaDetector Python package
You’ve found the documentation for the MegaDetector Python package. If you are an ecologist looking to run MegaDetector on your camera trap images, you don’t need to write (or even run) any Python code, so you probably don’t want this Python package. Instead, head over to the “Getting started with MegaDetector” page.
If you’re a programmer-type looking to (a) run MegaDetector from Python code or (b) use other tools from the MegaDetector repo, e.g. for manipulating common camera trap data formats, you’re in the right place!
Installation
You likely already guessed it:
pip install megadetector
Examples
Run MegaDetector on one image
from megadetector.utils import url_utils
from megadetector.visualization import visualization_utils as vis_utils
from megadetector.detection import run_detector
# This is the image at the bottom of this page, it has one animal in it
image_url = 'https://github.com/agentmorris/MegaDetector/raw/main/images/orinoquia-thumb-web.jpg'
temporary_filename = url_utils.download_url(image_url)
image = vis_utils.load_image(temporary_filename)
# This will automatically download MDv5a; you can also specify a filename.
model = run_detector.load_detector('MDV5A')
result = model.generate_detections_one_image(image)
detections_above_threshold = [d for d in result['detections'] if d['conf'] > 0.2]
print('Found {} detections above threshold'.format(len(detections_above_threshold)))
Run MegaDetector on a folder of images
from megadetector.detection.run_detector_batch import \
load_and_run_detector_batch, write_results_to_file
from megadetector.utils import path_utils
import os
# Pick a folder to run MD on recursively, and an output file
image_folder = os.path.expanduser('~/megadetector_test_images')
output_file = os.path.expanduser('~/megadetector_output_test.json')
# Recursively find images
image_file_names = path_utils.find_images(image_folder,recursive=True)
# This will automatically download MDv5a; you can also specify a filename.
results = load_and_run_detector_batch('MDV5A', image_file_names)
# Write results to a format that Timelapse and other downstream tools like.
write_results_to_file(results,
output_file,
relative_path_base=image_folder,
detector_file=detector_filename)
Package documentation
Contents
- Home
- Getting Started
- MegaDetector User Guide
- Table of contents
- MegaDetector overview
- Our ask to MegaDetector users
- Using MegaDetector
- Using a GPU
- How do I use the results?
- Downloading MegaDetector (optional)
- MegaDetector versions
- How fast is MegaDetector, and can I run it on my giant/small computer?
- Is there a GUI?
- Who is using MegaDetector?
- What about species classification?
- Pro tips for large workloads
- Pro tips for coaxing every bit of accuracy out of MegaDetector
- Have you evaluated MegaDetector’s accuracy?
- What is MegaDetector bad at?
- Citing MegaDetector
- Pretty picture and mesmerizing video
- Can you share the training data?
- I’m partying like it’s 2019, and I want to run MDv4
- Subpackages
Gratuitous camera trap image
Image credit University of Minnesota, from the Orinoquía Camera Traps data set.