# agentmorris/megadetector architecture

> Architecture diagram of the GitHub repository agentmorris/megadetector, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/agentmorris/megadetector
- Repository: https://github.com/agentmorris/megadetector

## Overview

MegaDetector is a camera-trap image analysis toolkit: users run animal/person/vehicle detection over images, receive standardized detections, then validate, postprocess, and inspect results. The repository also provides video and tiled inference, classification workflows, dataset/annotation format conversion, taxonomy tools, and image metadata utilities. The graph emphasizes batch inference and result handling; other documented or clearly distinct product capabilities are included as separate branches. Some subsystem links are omitted because their implementations were not sampled, so folder proximity is not treated as evidence of calls.

Main entry points: [`megadetector/detection/run_detector_batch.py`](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_detector_batch.py) (Batch inference), [`megadetector/detection/run_detector.py`](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_detector.py) (Single-image inference), [`megadetector/visualization/visualization_utils.py`](https://github.com/agentmorris/megadetector/blob/main/megadetector/visualization/visualization_utils.py) (Detection visualization), [`megadetector/detection/run_tiled_inference.py`](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_tiled_inference.py) (Tiled inference)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_inference["Detection inference"]
  node_batch_runner["Batch inference"]
  node_single_runner["Single-image inference<br/>[run_detector.py]"]
  node_torch_backend["PyTorch detector"]
  node_tf_backend["TensorFlow detector<br/>[tf_detector.py]"]
  node_rfdetr_backend["RF-DETR detector<br/>[rfdetr_detector.py]"]
  node_tiled["Tiled inference"]
end

subgraph group_results["Results and review"]
  node_md_output["Detection results<br/>[cct_json_utils.py]"]
  node_postprocess["Batch postprocessing"]
  node_validation["Result validation"]
  node_repeat_filter["Repeat detection removal"]
  node_visualize["Detection visualization"]
  node_db_visualize["Dataset visualization<br/>[visualize_db.py]"]
end

subgraph group_classification["Classification"]
  node_detect_crop["Detection crop workflow<br/>[detect_and_crop.py]"]
  node_classifier["Classifier training"]
  node_classifier_model["EfficientNet model<br/>[model.py]"]
  node_classification_results["Classification analysis"]
end

subgraph group_data["Data and taxonomy"]
  node_format_conversion["Annotation conversion"]
  node_exif["Image metadata<br/>[read_exif.py]"]
  node_taxonomy["Taxonomy mapping"]
end

subgraph group_media["Media processing"]
  node_video["Video processing<br/>[process_video.py]"]
  node_video_utils["Video utilities<br/>[video_utils.py]"]
end

node_image_data(("Camera-trap images"))
node_speciesnet["SpeciesNet"]

node_image_data -->|"processes images"| node_batch_runner
node_batch_runner -->|"invokes"| node_single_runner
node_single_runner -->|"loads backend"| node_torch_backend
node_single_runner -->|"loads backend"| node_tf_backend
node_single_runner -->|"loads backend"| node_rfdetr_backend
node_single_runner -->|"produces detections"| node_md_output
node_batch_runner -->|"reads metadata"| node_exif
node_batch_runner -->|"converts results"| node_format_conversion
node_batch_runner -->|"renders detections"| node_visualize
node_tiled -->|"runs batch inference"| node_batch_runner
node_tiled -->|"runs inference"| node_single_runner
node_video -->|"uses utilities"| node_video_utils
node_postprocess -->|"validates results"| node_validation
node_repeat_filter -->|"uses batch processing"| node_postprocess
node_repeat_filter -->|"renders results"| node_visualize
node_db_visualize -->|"renders images"| node_visualize
node_detect_crop -.->|"supports species workflows"| node_speciesnet
node_classifier -->|"uses model"| node_classifier_model
node_classification_results -->|"uses taxonomy"| node_taxonomy
node_format_conversion -->|"writes MD format"| node_md_output

click node_batch_runner "https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_detector_batch.py"
click node_single_runner "https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_detector.py"
click node_torch_backend "https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/pytorch_detector.py"
click node_tf_backend "https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/tf_detector.py"
click node_rfdetr_backend "https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/rfdetr_detector.py"
click node_tiled "https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_tiled_inference.py"
click node_video "https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/process_video.py"
click node_video_utils "https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/video_utils.py"
click node_md_output "https://github.com/agentmorris/megadetector/blob/main/megadetector/data_management/cct_json_utils.py"
click node_postprocess "https://github.com/agentmorris/megadetector/blob/main/megadetector/postprocessing/postprocess_batch_results.py"
click node_validation "https://github.com/agentmorris/megadetector/blob/main/megadetector/postprocessing/validate_batch_results.py"
click node_repeat_filter "https://github.com/agentmorris/megadetector/blob/main/megadetector/postprocessing/repeat_detection_elimination/repeat_detections_core.py"
click node_visualize "https://github.com/agentmorris/megadetector/blob/main/megadetector/visualization/visualization_utils.py"
click node_db_visualize "https://github.com/agentmorris/megadetector/blob/main/megadetector/visualization/visualize_db.py"
click node_detect_crop "https://github.com/agentmorris/megadetector/blob/main/megadetector/classification/detect_and_crop.py"
click node_classifier "https://github.com/agentmorris/megadetector/blob/main/megadetector/classification/train_classifier.py"
click node_classifier_model "https://github.com/agentmorris/megadetector/blob/main/megadetector/classification/efficientnet/model.py"
click node_classification_results "https://github.com/agentmorris/megadetector/blob/main/megadetector/postprocessing/analyze_classification_results.py"
click node_format_conversion "https://github.com/agentmorris/megadetector/blob/main/megadetector/data_management/yolo_output_to_md_output.py"
click node_exif "https://github.com/agentmorris/megadetector/blob/main/megadetector/data_management/read_exif.py"
click node_taxonomy "https://github.com/agentmorris/megadetector/blob/main/megadetector/taxonomy_mapping/map_lila_taxonomy_to_wi_taxonomy.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_batch_runner,node_single_runner,node_torch_backend,node_tf_backend,node_rfdetr_backend,node_tiled toneBlue
class node_md_output,node_postprocess,node_validation,node_repeat_filter,node_visualize,node_db_visualize toneAmber
class node_detect_crop,node_classifier,node_classifier_model,node_classification_results toneMint
class node_format_conversion,node_exif,node_taxonomy toneRose
class node_video,node_video_utils,node_image_data,node_speciesnet toneIndigo
```

## Components

### Detection inference

- [Batch inference](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_detector_batch.py): `megadetector/detection/run_detector_batch.py`
- [Single-image inference](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_detector.py): `megadetector/detection/run_detector.py`
- [PyTorch detector](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/pytorch_detector.py): `megadetector/detection/pytorch_detector.py`
- [TensorFlow detector](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/tf_detector.py): `megadetector/detection/tf_detector.py`
- [RF-DETR detector](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/rfdetr_detector.py): `megadetector/detection/rfdetr_detector.py`
- [Tiled inference](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/run_tiled_inference.py): `megadetector/detection/run_tiled_inference.py`

### Results and review

- [Detection results](https://github.com/agentmorris/megadetector/blob/main/megadetector/data_management/cct_json_utils.py): `megadetector/data_management/cct_json_utils.py`
- [Batch postprocessing](https://github.com/agentmorris/megadetector/blob/main/megadetector/postprocessing/postprocess_batch_results.py): `megadetector/postprocessing/postprocess_batch_results.py`
- [Result validation](https://github.com/agentmorris/megadetector/blob/main/megadetector/postprocessing/validate_batch_results.py): `megadetector/postprocessing/validate_batch_results.py`
- [Repeat detection removal](https://github.com/agentmorris/megadetector/blob/main/megadetector/postprocessing/repeat_detection_elimination/repeat_detections_core.py): `megadetector/postprocessing/repeat_detection_elimination/repeat_detections_core.py`
- [Detection visualization](https://github.com/agentmorris/megadetector/blob/main/megadetector/visualization/visualization_utils.py): `megadetector/visualization/visualization_utils.py`
- [Dataset visualization](https://github.com/agentmorris/megadetector/blob/main/megadetector/visualization/visualize_db.py): `megadetector/visualization/visualize_db.py`

### Classification

- [Detection crop workflow](https://github.com/agentmorris/megadetector/blob/main/megadetector/classification/detect_and_crop.py): `megadetector/classification/detect_and_crop.py`
- [Classifier training](https://github.com/agentmorris/megadetector/blob/main/megadetector/classification/train_classifier.py): `megadetector/classification/train_classifier.py`
- [EfficientNet model](https://github.com/agentmorris/megadetector/blob/main/megadetector/classification/efficientnet/model.py): `megadetector/classification/efficientnet/model.py`
- [Classification analysis](https://github.com/agentmorris/megadetector/blob/main/megadetector/postprocessing/analyze_classification_results.py): `megadetector/postprocessing/analyze_classification_results.py`

### Data and taxonomy

- [Annotation conversion](https://github.com/agentmorris/megadetector/blob/main/megadetector/data_management/yolo_output_to_md_output.py): `megadetector/data_management/yolo_output_to_md_output.py`
- [Image metadata](https://github.com/agentmorris/megadetector/blob/main/megadetector/data_management/read_exif.py): `megadetector/data_management/read_exif.py`
- [Taxonomy mapping](https://github.com/agentmorris/megadetector/blob/main/megadetector/taxonomy_mapping/map_lila_taxonomy_to_wi_taxonomy.py): `megadetector/taxonomy_mapping/map_lila_taxonomy_to_wi_taxonomy.py`

### Media processing

- [Video processing](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/process_video.py): `megadetector/detection/process_video.py`
- [Video utilities](https://github.com/agentmorris/megadetector/blob/main/megadetector/detection/video_utils.py): `megadetector/detection/video_utils.py`

### Other components

- **Camera-trap images**
- **SpeciesNet**

## Connections

- Camera-trap images → Batch inference: processes images (evidence: `megadetector/detection/run_detector_batch.py`)
- Batch inference → Single-image inference: invokes (evidence: `megadetector/detection/run_detector_batch.py`)
- Single-image inference → PyTorch detector: loads backend (evidence: `megadetector/detection/run_detector.py`)
- Single-image inference → TensorFlow detector: loads backend (evidence: `megadetector/detection/run_detector.py`)
- Single-image inference → RF-DETR detector: loads backend (evidence: `megadetector/detection/run_detector.py`)
- Single-image inference → Detection results: produces detections (evidence: `megadetector/detection/run_detector.py`)
- Batch inference → Image metadata: reads metadata (evidence: `megadetector/detection/run_detector_batch.py`)
- Batch inference → Annotation conversion: converts results (evidence: `megadetector/detection/run_detector_batch.py`)
- Batch inference → Detection visualization: renders detections (evidence: `megadetector/detection/run_detector_batch.py`)
- Tiled inference → Batch inference: runs batch inference (evidence: `megadetector/detection/run_tiled_inference.py`)
- Tiled inference → Single-image inference: runs inference (evidence: `megadetector/detection/run_tiled_inference.py`)
- Video processing → Video utilities: uses utilities
- Batch postprocessing → Result validation: validates results
- Repeat detection removal → Batch postprocessing: uses batch processing (evidence: `megadetector/postprocessing/repeat_detection_elimination/repeat_detections_core.py`)
- Repeat detection removal → Detection visualization: renders results (evidence: `megadetector/postprocessing/repeat_detection_elimination/repeat_detections_core.py`)
- Dataset visualization → Detection visualization: renders images (evidence: `megadetector/visualization/visualize_db.py`)
- Detection crop workflow → SpeciesNet: supports species workflows (evidence: `README.md`)
- Classifier training → EfficientNet model: uses model (evidence: `megadetector/classification/train_classifier.py`)
- Classification analysis → Taxonomy mapping: uses taxonomy (evidence: `megadetector/postprocessing/analyze_classification_results.py`)
- Annotation conversion → Detection results: writes MD format
