AI + Computer Vision Pipeline for Social Network Analysis of Dairy Cows
DairyCow-SNA is an open-source AI pipeline that detects, tracks, and interprets the social behavior of dairy cows using computer vision and machine learning.
It integrates YOLOv11-style detection workflows, ByteTrack, and ZebraPose-derived pose models to identify cows, estimate keypoints, and infer interactions, generating digital social profiles for each animal.
The goal is to advance precision livestock management and improve animal welfare through data-driven behavioral insights.
This repository version is a GitHub-pushable packaging of the full cattle interaction pipeline derived from Interaction_Tracker_2.py.
At a high level, the packaged pipeline does all of the following:
- object detection
- multi-object tracking with ByteTrack
- per-track cattle identity classification
- top-down pose inference
- pairwise interaction gating
- Stage-1 interaction / no-interaction temporal classification
- Stage-2 interaction-type classification
- export of annotated video and CSV results
The packaged project is intended to be portable across Linux and Windows. The runtime code is included in the repository, while the large model binaries are intentionally kept outside GitHub and must be downloaded separately.
| Module | Purpose |
|---|---|
| Object Detection Training | Trains YOLO-based models on barn environments to locate individual cows. |
| ByteTrack Optimization | Optimizes multi-object tracking with Kalman smoothing for ID consistency. |
| Object Identification | Recognizes individual cows via re-identification classification. |
| ZebraPose Keypoint Detection | Detects cow body keypoints for posture and movement tracking. |
| YOLO Keypoint Detection | Alternate pose model path for custom cow keypoint detection workflows. |
| Interaction Inference | Uses temporal keypoint distances and learned classifiers to identify interaction types such as affiliative, neutral, and aggressive behavior. |
| Cattle Monitoring Main Pipeline | Integrates all modules and produces downstream behavioral and social-network-ready outputs. |
Detection -> Tracking -> Identification -> Keypoint Detection -> Interaction Inference -> Social Network Graphs
Each cow becomes a node in a dynamic social graph, and interactions form edges weighted by frequency and duration.
Project root contents:
run_full_interaction_pipeline.pyThe main runner for the full interaction pipeline.inputs/Default location for input videos.outputs/Default location for output run folders.models/Required model directory. In this GitHub-pushable version, the large model files are not committed and must be downloaded separately.Beyond_Proximity_Dataset/Reference folder for the public sample dataset download location and expected structure.vendor/ByteTrack/Vendored ByteTrack runtime code.vendor/ZebraPoseViTPose/Vendored pose runtime code and pose config files.bootstrap_linux.shLinux environment bootstrap.bootstrap_windows.ps1Windows environment bootstrap.requirements.txtLightweight root requirements for CI, dependency submission, and smoke tests.linux-runtime-pins.txtLinux runtime dependency pins used bybootstrap_linux.sh.windows-runtime-pins.txtWindows runtime dependency pins used bybootstrap_windows.ps1.
Model files are stored externally due to GitHub size limits.
This packaged project expects the following required files under models/:
Object_Detection_Trained_Model.ptIdentification_Model_Trained.ptKeypoint_Model_Trained.pthsvm_model.joblibstage1_tcn_best.ptstage2_inception_best.pt
These files are not stored in the GitHub-pushable version of the repository.
Download them from this Google Drive folder:
https://drive.google.com/drive/folders/1im99sooJqAi70oieGIO0xhpFePfS0qoD?usp=sharing
After downloading, place them directly into the models/ folder.
Sample data from the Beyond Proximity dataset is also stored externally and is not committed into this GitHub repository.
Download the sample dataset from this Google Drive folder:
https://drive.google.com/drive/folders/1sP_RqsjEJuXkHw8UJ1wP3fz_waFQeCyj?usp=sharing
The external sample dataset currently contains these top-level directories:
Interaction_Feature_KeypointKeypoint_DetectionVideos
If you want the local project layout to mirror the referenced dataset name, place the downloaded contents under:
./Beyond_Proximity_Dataset/Interaction_Feature_Keypoint./Beyond_Proximity_Dataset/Keypoint_Detection./Beyond_Proximity_Dataset/Videos
The repository includes a placeholder Beyond_Proximity_Dataset/README.md so the sample dataset is clearly referenced inside GitHub without storing the data itself in version control.
If you run the script with no overrides beyond --input-dir, it uses project-relative defaults:
- input videos:
./inputs - output base directory:
./outputs - detector weights:
./models/Object_Detection_Trained_Model.pt - identity weights:
./models/Identification_Model_Trained.pt - pose checkpoint:
./models/Keypoint_Model_Trained.pth - SVM model:
./models/svm_model.joblib - Stage-1 checkpoint:
./models/stage1_tcn_best.pt - Stage-2 checkpoint:
./models/stage2_inception_best.pt - pose config:
./vendor/ZebraPoseViTPose/ZebraPose/configs/animal/2d_kpt_sview_rgb_img/topdown_heatmap/MAE_pret_syn/s_zebras_old_adam.py
Each run creates a new numbered output folder inside outputs/, for example:
outputs/1outputs/2outputs/3
The runner accepts these file extensions:
.mp4.mkv.mov.avi.m4v.wmv.ts.webm
By default, the runner scans the chosen input directory and processes all supported video files found there.
The normal workflow is:
- Download the model files into
models/. - Optionally download the public sample dataset into
Beyond_Proximity_Dataset/. - Set up the Python environment.
- Put videos into
inputs/or pass specific video paths with--video. - Run
run_full_interaction_pipeline.py. - Read the results from a new numbered folder under
outputs/.
For lightweight setup, GitHub dependency submission, and reviewer-safe smoke tests, install the root requirements.txt.
For the full packaged pipeline environment, use the platform bootstrap scripts, which install linux-runtime-pins.txt or windows-runtime-pins.txt plus the matching Torch and mmcv-full wheels for the selected platform.
bootstrap_linux.sh is a general Linux bootstrap script.
It:
- creates a virtual environment in
./venv - installs Python packaging tools
- installs PyTorch
- installs runtime dependencies from
linux-runtime-pins.txt - installs
mmcv-full==1.5.0from an OpenMMLab wheel index chosen to match the selected Torch variant - installs
cython_bbox - verifies that the required project files and imports are present
When apt-get is available, it also installs these system packages:
ffmpeglibgl1libglib2.0-0build-essentialpython3-devpython3-venvpython3-pipgit
If apt-get is not available, the script prints the packages you should install manually before continuing.
The Linux bootstrap uses TORCH_VARIANT.
Supported values:
autocpucu118
Behavior:
autoselectscu118whennvidia-smiis availableautoselectscpuwhen no NVIDIA GPU is detected
GPU-or-auto setup:
bash bootstrap_linux.sh
source venv/bin/activate
python run_full_interaction_pipeline.py --input-dir ./inputsForce CPU setup:
TORCH_VARIANT=cpu bash bootstrap_linux.sh
source venv/bin/activate
python run_full_interaction_pipeline.py --input-dir ./inputsForce CUDA 11.8 setup:
TORCH_VARIANT=cu118 bash bootstrap_linux.sh
source venv/bin/activate
python run_full_interaction_pipeline.py --input-dir ./inputsbootstrap_windows.ps1 is the Windows environment bootstrap.
It:
- creates a virtual environment in
.\venv_windows - installs PyTorch
- installs runtime dependencies from
windows-runtime-pins.txt - installs
mmcv-full==1.5.0from an OpenMMLab wheel index chosen to match the selected Torch variant - installs
cython_bbox - verifies that the required project files and imports are present
The Windows bootstrap uses -TorchVariant.
Supported values:
autocpucu118
Behavior:
autoselectscu118whennvidia-smi.exeis availableautoselectscpuotherwise
From PowerShell in the project root:
Auto-select CPU or GPU:
.\bootstrap_windows.ps1
.\venv_windows\Scripts\python.exe .\run_full_interaction_pipeline.py --input-dir .\inputsForce CPU:
.\bootstrap_windows.ps1 -TorchVariant cpu
.\venv_windows\Scripts\python.exe .\run_full_interaction_pipeline.py --input-dir .\inputsForce CUDA 11.8:
.\bootstrap_windows.ps1 -TorchVariant cu118
.\venv_windows\Scripts\python.exe .\run_full_interaction_pipeline.py --input-dir .\inputsIf PowerShell script execution is blocked, you can run:
powershell -ExecutionPolicy Bypass -File .\bootstrap_windows.ps1Put videos into inputs/ and run:
Linux:
python run_full_interaction_pipeline.py --input-dir ./inputsWindows:
.\venv_windows\Scripts\python.exe .\run_full_interaction_pipeline.py --input-dir .\inputsLinux:
python run_full_interaction_pipeline.py \
--video /abs/path/video1.mp4 \
--video /abs/path/video2.mp4Windows:
.\venv_windows\Scripts\python.exe .\run_full_interaction_pipeline.py `
--video .\inputs\video1.mp4 `
--video .\inputs\video2.mp4Linux:
python run_full_interaction_pipeline.py --input-dir ./inputs --output-dir ./my_outputsWindows:
.\venv_windows\Scripts\python.exe .\run_full_interaction_pipeline.py --input-dir .\inputs --output-dir .\my_outputsThe runner currently exposes these command-line options:
- Type: path
- Default:
./inputs - Meaning: directory scanned for supported video files
- Type: path, repeatable
- Default: none
- Meaning: process one or more explicitly named video files
- Note: you can repeat
--videomultiple times
- Type: path
- Default:
./outputs - Meaning: base directory where numbered run folders are created
- Type: path
- Default:
./models/Object_Detection_Trained_Model.pt - Meaning: detector weights override
- Type: path
- Default:
./models/Identification_Model_Trained.pt - Meaning: identity model weights override
- Type: path
- Default:
./vendor/ZebraPoseViTPose/ZebraPose/configs/animal/2d_kpt_sview_rgb_img/topdown_heatmap/MAE_pret_syn/s_zebras_old_adam.py - Meaning: pose config override
- Type: path
- Default:
./models/Keypoint_Model_Trained.pth - Meaning: pose checkpoint override
- Type: path
- Default:
./models/svm_model.joblib - Meaning: optional SVM model override
- Type: path
- Default:
./models/stage1_tcn_best.pt - Meaning: Stage-1 temporal checkpoint override
- Type: path
- Default:
./models/stage2_inception_best.pt - Meaning: Stage-2 temporal checkpoint override
- Type: string or GPU index
- Default:
auto - Supported examples:
auto,0,cpu - Meaning: detector and identity model device selection
- Type: string
- Default:
auto - Supported examples:
auto,cuda:0,cpu - Meaning: pose model device selection
- Type: float
- Default:
0.5 - Meaning: scaling factor applied to the annotated output video size
- Type: flag
- Default: off
- Meaning: disables annotated video writing to reduce runtime and output size
When auto is used:
- detector device becomes GPU
0when CUDA is available, otherwisecpu - pose device becomes
cuda:0when CUDA is available, otherwisecpu
For each run, the script creates a new numbered folder under outputs/ or the directory provided by --output-dir.
Typical outputs include:
One row per track per frame, including:
- video name
- frame index
- track ID
- bounding box
- tracking score
- resolved identity label
- identity confidence
Detected interaction events, including:
- video
- interaction class
- track IDs for the pair
- start and end frame
- duration
- event weight
- Stage-2 confidence
- Stage-1 probability
- Stage-1 threshold
Candidate overlapping pairs that remained negative for interaction.
Saved when proximity logging is enabled in the script.
Per-class adjacency summaries built from the detected interactions.
Annotated output video, unless --disable-annotated-video is used.
The code supports this file, but it is disabled by default because SAVE_KEYPOINTS_CSV = False in the runner.
The repository intentionally references, rather than stores, two large external asset bundles:
- model weights: https://drive.google.com/drive/folders/1im99sooJqAi70oieGIO0xhpFePfS0qoD?usp=sharing
- sample Beyond Proximity dataset: https://drive.google.com/drive/folders/1sP_RqsjEJuXkHw8UJ1wP3fz_waFQeCyj?usp=sharing
This keeps the GitHub repository lightweight while still documenting how to reconstruct the runnable environment and sample-data layout.
- The runner creates a fresh numbered output folder on each run instead of overwriting the last run automatically.
- Annotated video output is enabled by default.
- Keypoint CSV export is disabled by default.
- The script processes all supported videos in the selected input directory unless you explicitly use
--video. - Stage-2 confidence gating is enabled in the runner.
- The pipeline includes cooldown and temporal stability logic for interaction events, so outputs are not simple frame-by-frame raw detections.
- This packaged project was assembled from the existing working code and assets, but I did not run the full pipeline end-to-end in this packaged form during this session.
- The dependency stacks in
bootstrap_linux.shandbootstrap_windows.ps1are best reconstructed environment definitions, not yet fully validated handoff environments. - GPU support is intentionally left open on both Linux and Windows, but the exact CUDA / driver / wheel compatibility still depends on the target machine.
- The Windows path is more manual and riskier than the Linux path because packages like
mmcv-fullcan be sensitive to Python, Torch, and platform combinations. - The vendored pose stack is older and depends on the matching
mmcv-full/ Torch family specified by the bootstrap scripts.
Full cow video datasets are restricted and are not published online in this repository.
- only project code, configuration, and lightweight examples should be shared here
- model files are hosted externally due to GitHub size limits
- the publicly shared Beyond Proximity sample dataset is referenced externally through Google Drive rather than committed into Git
- any third-party or human data from external sources should remain removed for copyright and privacy compliance
- Lead Developer: Sibi Parivendhan
- Supervisor: Dr. Suresh Raja Neethirajan, Dalhousie University
This project is licensed under the Apache 2.0 License.
See the LICENSE file for details if it is included in the target repository.
- MooAnalytica Research Group: https://mooanalytica.com
- Dalhousie Faculty of Agriculture and Computer Science: https://www.dal.ca/faculty/computerscience/faculty-staff/Suresh-Raja-Neethirajan.html
If you use this work, please cite:
S. Parivendan, K. Sailunaz, S. Neethirajan (2025). DairyCow-SNA: AI-Enabled Social Network Analysis of Dairy Cows.
Before running a large batch, do a quick smoke test:
- Put one short input video into
inputs/. - Run the bootstrap for your platform.
- Run the main script once.
- Check that a numbered run folder appears under
outputs/. - Confirm that
tracking_boxes.csv,interactions.csvorno_interactions.csv, and an annotated video are produced as expected.
If the environment is already set up, the common usage is exactly this:
- Download the required model files into
models/. - Optionally download the public sample dataset into
Beyond_Proximity_Dataset/. - Put videos into
inputs/or select files from the downloaded sample dataset. - Run
run_full_interaction_pipeline.py. - Read the results from the newest numbered folder under
outputs/.