A computer vision and Django REST API project for counting farm animals from video streams. The system detects and tracks animals crossing a virtual line, stores unique events in a database, and exposes simple statistics through an API.
This project combines two parts:
- A Django backend that stores detected animal records and returns daily statistics.
- A Python computer vision script that uses YOLOv8 + ByteTrack to process a video or webcam feed and send detections to the backend.
The current implementation is best suited for local demo and portfolio use.
- Detects and tracks animals from a video or webcam feed
- Counts animals when they cross a virtual line
- Prevents duplicate records using
animal_type + track_id - Stores events in SQLite
- Exposes simple REST endpoints for logging and statistics
- Includes a sample video and YOLO model for quick testing
- Python 3.12
- Django 5
- Django REST Framework
- OpenCV
- Ultralytics YOLOv8
- ByteTrack
- SQLite
.
├── animal_counter.py # Video processing, tracking, and API integration
├── animals_count/ # Django app
├── config/ # Django project settings and URLs
├── manage.py
├── requirements.txt
├── test_video.mp4 # Sample video for demo
└── yolov8n.pt # Local YOLO model
animal_counter.pyopens a video file or webcam stream.- YOLO detects animals in each frame.
- ByteTrack assigns stable tracking IDs.
- When an animal crosses the counting line, the script sends a
POSTrequest to the Django API. - The backend stores the event and prevents duplicates.
- Statistics can be retrieved from the API.
Creates a new animal log.
Example request:
{
"animal_type": "cow",
"track_id": 101
}Successful response:
{
"id": 1,
"animal_type": "cow",
"track_id": 101,
"timestamp": "2026-01-26T19:30:00Z"
}Returns the newest saved animal records ordered from newest to oldest.
Example response:
[
{
"id": 52,
"animal_type": "cow",
"track_id": 292,
"timestamp": "2026-05-20T13:34:01.506053Z"
},
{
"id": 51,
"animal_type": "cow",
"track_id": 40,
"timestamp": "2026-05-20T13:33:44.977940Z"
}
]Returns daily grouped statistics.
Example response:
[
{
"date": "2026-01-26",
"animal_type": "cow",
"count": 4
},
{
"date": "2026-01-26",
"animal_type": "sheep",
"count": 2
}
]Returns a simple browser dashboard that auto-refreshes every 3 seconds and shows recent logs plus daily stats.
cd /path/to/Animals_count
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt
python manage.py migrate
python manage.py runserverThe backend will start at:
http://127.0.0.1:8000/
Open a second terminal and run:
cd /path/to/Animals_count
source .venv/bin/activate
curl -X POST http://127.0.0.1:8000/ \
-H "Content-Type: application/json" \
-d '{"animal_type":"cow","track_id":101}'
xdg-open http://127.0.0.1:8000/dashboard/
curl http://127.0.0.1:8000/logs/
curl http://127.0.0.1:8000/stats/With the included sample video:
cd /path/to/Animals_count
source .venv/bin/activate
python animal_counter.py --source test_video.mp4With a webcam:
python animal_counter.py --source 0- The default local YOLO model works well for
cowandsheep. goatsupport is intended for a custom model or Roboflow-based workflow.- The backend is ready for API usage, while the OpenCV window-based demo is primarily designed for local execution.
- The dashboard page auto-refreshes, but it uses polling rather than WebSockets.
- Old records are automatically cleaned using retention settings in
config/settings.py.
This project demonstrates:
- backend API development with Django REST Framework
- computer vision pipeline integration
- object tracking and event-based counting
- practical database design with duplicate prevention
- end-to-end local system integration
- add automated tests
- add Docker support
- add video upload processing instead of local-only display
- add deployment-ready API configuration
- add charts or dashboard for statistics
Built as a portfolio project for demonstrating practical computer vision and backend integration skills.