# makejian/pet-behavior-recognition architecture

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

- Interactive diagram: https://gitdiagram.com/makejian/pet-behavior-recognition
- Repository: https://github.com/makejian/pet-behavior-recognition

## Overview

This repository is a cat-video analysis application: a CLI or Streamlit user supplies video or a camera feed, and the pipeline produces tracked cat poses, behavior labels, visualized video, and structured JSON. Its principal runtime stages are detection, tracking, pose estimation, temporal stabilization, and behavior recognition. It also includes a separate dataset-preparation and model-training workflow for custom 27-keypoint pose models and optional YOLO detectors. Runtime connections are supported by the pipeline excerpt and source index; training-stage links are documented in the README.

Main entry points: [`src/cat_pose_pipeline.py`](https://github.com/makejian/pet-behavior-recognition/blob/main/src/cat_pose_pipeline.py) (Frame pipeline), [`scripts`](https://github.com/makejian/pet-behavior-recognition/tree/main/scripts) (Dataset preparation), [`app.py`](https://github.com/makejian/pet-behavior-recognition/blob/main/app.py) (Interactive app), [`run.py`](https://github.com/makejian/pet-behavior-recognition/blob/main/run.py) (Batch CLI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["User Entry"]
  node_cli["Batch CLI<br/>[run.py]"]
  node_web["Interactive app<br/>[app.py]"]
end

subgraph group_runtime["Video Analysis"]
  node_pipeline["Frame pipeline"]
  node_detection["Cat detection<br/>[detector.py]"]
  node_tracking["Multi-object tracking<br/>[tracker.py]"]
  node_pose["Pose estimation<br/>[pose_estimator.py]"]
  node_stabilize["Temporal stabilization<br/>[smoothing.py]"]
  node_behavior["Behavior recognition<br/>[behavior.py]"]
  node_utilities["JSON and geometry utilities<br/>[utils.py]"]
end

subgraph group_outputs["Results"]
  node_render["Pose rendering<br/>[render.py]"]
  node_videoout["Annotated video"]
  node_jsonout["Frame JSON"]
end

subgraph group_training["Data and Training"]
  node_annotations["Labeled pose data"]
  node_conversion["Annotation conversion"]
  node_dataset["Dataset preparation"]
  node_pose_training["Pose model training<br/>[train_pose.py]"]
  node_det_training["Detector training<br/>[train_detector.py]"]
  node_models["Model configurations"]
end

node_user(("Video analyst"))
node_video(("Video or camera"))

node_user -->|"runs batch"| node_cli
node_user -->|"starts analysis"| node_web
node_video -->|"provides frames"| node_pipeline
node_video -->|"camera or upload"| node_web
node_cli -->|"configures and runs"| node_pipeline
node_web -->|"processes frames"| node_pipeline
node_pipeline -->|"detects cats"| node_detection
node_pipeline -->|"assigns identities"| node_tracking
node_pipeline -->|"estimates keypoints"| node_pose
node_pipeline -->|"stabilizes poses"| node_stabilize
node_pipeline -->|"classifies behavior"| node_behavior
node_pipeline -->|"draws results"| node_render
node_pipeline -->|"uses helpers"| node_utilities
node_pipeline -->|"writes video"| node_videoout
node_pipeline -->|"writes records"| node_jsonout
node_user -.->|"labels examples"| node_annotations
node_annotations -->|"converts labels"| node_conversion
node_conversion -->|"prepares data"| node_dataset
node_dataset -->|"supplies dataset"| node_pose_training
node_dataset -->|"supplies dataset"| node_det_training
node_pose_training -->|"trains pose model"| node_models
node_det_training -->|"trains detector"| node_models

click node_cli "https://github.com/makejian/pet-behavior-recognition/blob/main/run.py"
click node_web "https://github.com/makejian/pet-behavior-recognition/blob/main/app.py"
click node_pipeline "https://github.com/makejian/pet-behavior-recognition/blob/main/src/cat_pose_pipeline.py"
click node_detection "https://github.com/makejian/pet-behavior-recognition/blob/main/src/detector.py"
click node_tracking "https://github.com/makejian/pet-behavior-recognition/blob/main/src/tracker.py"
click node_pose "https://github.com/makejian/pet-behavior-recognition/blob/main/src/pose_estimator.py"
click node_stabilize "https://github.com/makejian/pet-behavior-recognition/blob/main/src/smoothing.py"
click node_behavior "https://github.com/makejian/pet-behavior-recognition/blob/main/src/behavior.py"
click node_render "https://github.com/makejian/pet-behavior-recognition/blob/main/src/render.py"
click node_utilities "https://github.com/makejian/pet-behavior-recognition/blob/main/src/utils.py"
click node_videoout "https://github.com/makejian/pet-behavior-recognition/blob/main/src/cat_pose_pipeline.py"
click node_jsonout "https://github.com/makejian/pet-behavior-recognition/blob/main/src/cat_pose_pipeline.py"
click node_annotations "https://github.com/makejian/pet-behavior-recognition/tree/main/data/annotations"
click node_conversion "https://github.com/makejian/pet-behavior-recognition/blob/main/scripts/convert_labelstudio_to_coco.py"
click node_dataset "https://github.com/makejian/pet-behavior-recognition/tree/main/scripts"
click node_pose_training "https://github.com/makejian/pet-behavior-recognition/blob/main/scripts/train_pose.py"
click node_det_training "https://github.com/makejian/pet-behavior-recognition/blob/main/scripts/train_detector.py"
click node_models "https://github.com/makejian/pet-behavior-recognition/tree/main/pretrained/rtmpose"

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_cli,node_web toneBlue
class node_pipeline,node_detection,node_tracking,node_pose,node_stabilize,node_behavior,node_utilities toneAmber
class node_render,node_videoout,node_jsonout toneMint
class node_annotations,node_conversion,node_dataset,node_pose_training,node_det_training,node_models toneRose
class node_user,node_video toneIndigo
```

## Components

### User Entry

- [Batch CLI](https://github.com/makejian/pet-behavior-recognition/blob/main/run.py): `run.py`
- [Interactive app](https://github.com/makejian/pet-behavior-recognition/blob/main/app.py): `app.py`

### Video Analysis

- [Frame pipeline](https://github.com/makejian/pet-behavior-recognition/blob/main/src/cat_pose_pipeline.py): `src/cat_pose_pipeline.py`
- [Cat detection](https://github.com/makejian/pet-behavior-recognition/blob/main/src/detector.py): `src/detector.py`
- [Multi-object tracking](https://github.com/makejian/pet-behavior-recognition/blob/main/src/tracker.py): `src/tracker.py`
- [Pose estimation](https://github.com/makejian/pet-behavior-recognition/blob/main/src/pose_estimator.py): `src/pose_estimator.py`
- [Temporal stabilization](https://github.com/makejian/pet-behavior-recognition/blob/main/src/smoothing.py): `src/smoothing.py`
- [Behavior recognition](https://github.com/makejian/pet-behavior-recognition/blob/main/src/behavior.py): `src/behavior.py`
- [JSON and geometry utilities](https://github.com/makejian/pet-behavior-recognition/blob/main/src/utils.py): `src/utils.py`

### Results

- [Pose rendering](https://github.com/makejian/pet-behavior-recognition/blob/main/src/render.py): `src/render.py`
- [Annotated video](https://github.com/makejian/pet-behavior-recognition/blob/main/src/cat_pose_pipeline.py): `src/cat_pose_pipeline.py`
- [Frame JSON](https://github.com/makejian/pet-behavior-recognition/blob/main/src/cat_pose_pipeline.py): `src/cat_pose_pipeline.py`

### Data and Training

- [Labeled pose data](https://github.com/makejian/pet-behavior-recognition/tree/main/data/annotations): `data/annotations`
- [Annotation conversion](https://github.com/makejian/pet-behavior-recognition/blob/main/scripts/convert_labelstudio_to_coco.py): `scripts/convert_labelstudio_to_coco.py`
- [Dataset preparation](https://github.com/makejian/pet-behavior-recognition/tree/main/scripts): `scripts`
- [Pose model training](https://github.com/makejian/pet-behavior-recognition/blob/main/scripts/train_pose.py): `scripts/train_pose.py`
- [Detector training](https://github.com/makejian/pet-behavior-recognition/blob/main/scripts/train_detector.py): `scripts/train_detector.py`
- [Model configurations](https://github.com/makejian/pet-behavior-recognition/tree/main/pretrained/rtmpose): `pretrained/rtmpose`

### Other components

- **Video analyst**
- **Video or camera**

## Connections

- Video analyst → Batch CLI: runs batch (evidence: `README.md`)
- Video analyst → Interactive app: starts analysis (evidence: `app.py`)
- Video or camera → Frame pipeline: provides frames (evidence: `README.md`)
- Video or camera → Interactive app: camera or upload (evidence: `app.py`)
- Batch CLI → Frame pipeline: configures and runs (evidence: `run.py`)
- Interactive app → Frame pipeline: processes frames (evidence: `app.py`)
- Frame pipeline → Cat detection: detects cats (evidence: `src/cat_pose_pipeline.py`)
- Frame pipeline → Multi-object tracking: assigns identities (evidence: `src/cat_pose_pipeline.py`)
- Frame pipeline → Pose estimation: estimates keypoints (evidence: `src/cat_pose_pipeline.py`)
- Frame pipeline → Temporal stabilization: stabilizes poses (evidence: `src/cat_pose_pipeline.py`)
- Frame pipeline → Behavior recognition: classifies behavior (evidence: `src/cat_pose_pipeline.py`)
- Frame pipeline → Pose rendering: draws results (evidence: `src/cat_pose_pipeline.py`)
- Frame pipeline → JSON and geometry utilities: uses helpers (evidence: `src/cat_pose_pipeline.py`)
- Frame pipeline → Annotated video: writes video (evidence: `src/cat_pose_pipeline.py`)
- Frame pipeline → Frame JSON: writes records (evidence: `src/cat_pose_pipeline.py`)
- Video analyst → Labeled pose data: labels examples (evidence: `README.md`)
- Labeled pose data → Annotation conversion: converts labels (evidence: `README.md`)
- Annotation conversion → Dataset preparation: prepares data (evidence: `README.md`)
- Dataset preparation → Pose model training: supplies dataset (evidence: `README.md`)
- Dataset preparation → Detector training: supplies dataset (evidence: `README.md`)
- Pose model training → Model configurations: trains pose model (evidence: `README.md`)
- Detector training → Model configurations: trains detector (evidence: `README.md`)
