A comprehensive people detection system using YOLOv8 that tracks individuals with unique IDs and supports configurable polygonal zones for area monitoring.
- Real-time People Detection: Uses YOLOv8 for accurate person detection
- Unique ID Tracking: Assigns and maintains unique IDs for each detected person across frames
- Bounding Box Visualization: Clear visual representation with color-coded boxes
- Tracking Trails: Shows movement history for each person
- Polygonal Zones: Define custom detection zones with any polygon shape
- On-Screen Zone Drawing: Create zones directly on the video feed with mouse clicks
- Zone Statistics: Track current count and total unique visitors per zone
- Zone Monitoring: Tracks which zone(s) each person is in
- Interactive Zone Editor: Visual tool to create and edit zones
- Persistent Configuration: Zones saved in JSON format for reuse
- Video Output: Save processed video with detections and zones
pip install -r requirements.txtThe system uses YOLOv8n (nano) by default. Other models available:
yolov8n.pt- Nano (fastest, recommended for real-time)yolov8s.pt- Smallyolov8m.pt- Mediumyolov8l.pt- Largeyolov8x.pt- Extra Large (most accurate)
python people_detector.pypython people_detector.py --source video.mp4python people_detector.py --source video.mp4 --output output.mp4python people_detector.py --model yolov8s.ptpython people_detector.py --zones custom_zones.jsonpython people_detector.py --source video.mp4 --output output.mp4 --no-displaypython zone_manager.pypython zone_manager.py --source video.mp4python zone_manager.py --mode listpython people_detector.py
# Press 'd' to enable drawing mode, then click to create polygon zonesWhen running people_detector.py:
- 'd' key: Toggle drawing mode (create zones on-screen)
- Left Click (in drawing mode): Add point to current zone
- Right Click (in drawing mode): Finish current zone (minimum 3 points)
- Middle Click (in drawing mode): Remove last point
- 'c' key: Clear current zone being drawn
- 's' key: Save zones to file
- 'z' key: Print zone statistics to console
- 'q' key: Quit
When running zone_manager.py:
- Left Click: Add point to current zone
- Right Click: Finish current zone (minimum 3 points)
- Middle Click: Remove last point
- 's' key: Save zones to file
- 'c' key: Clear current zone being drawn
- 'd' key: Delete a zone (prompts for index)
- 't' key: Toggle zone on/off (prompts for index)
- 'l' key: List all zones
- 'q' key: Quit
{
"zones": [
{
"name": "Entry Zone",
"points": [
[100, 100],
[300, 100],
[300, 300],
[100, 300]
],
"color": [0, 255, 0],
"enabled": true
}
]
}- name: Descriptive name for the zone
- points: Array of [x, y] coordinates defining the polygon
- color: RGB color values [R, G, B] (0-255)
- enabled: Boolean to activate/deactivate zone
- YOLOv8 processes each frame and detects people (class 0)
- Built-in tracking assigns unique IDs that persist across frames
- Each person gets:
- Unique color-coded bounding box
- ID number displayed on box
- Confidence score
- Center point marker
- Movement trail (last 30 positions)
- Zone information (if in any zone)
- Zones are defined as polygons with any number of vertices
- System checks if person's center point is inside zone polygons
- Multiple zones can overlap - persons can be in multiple zones
- Zones are drawn as semi-transparent overlays
- Zone configuration persists in JSON file
- Zone Statistics: Each zone displays:
- Current Count: Number of people currently in the zone
- Total Visitors: Total unique individuals who have entered the zone (tracked by ID)
# Run detector and press 'd' to enable drawing mode
python people_detector.py
# Click on screen to draw polygon zones
# Right-click to complete each zone
# Press 's' to save zones# Create zones for doorways using zone manager
python zone_manager.py --source Camera-Video.mp4
# Run detection and press 'z' to see statistics
python people_detector.py# Process video with existing zones
python people_detector.py --source surveillance.mp4 --output monitored.mp4# Edit zones.json to define restricted areas
# Run detection to see who enters restricted zones
python people_detector.py --zones restricted_zones.jsonThe detector prints detailed information every 30 frames:
Frame 42: 3 people detected
- ID 1: conf=0.89 in ['Entry Zone']
- ID 2: conf=0.94
- ID 5: conf=0.87 in ['Exit Zone', 'Restricted Area']
Press 'z' to see detailed zone statistics:
=== Zone Statistics ===
Entry Zone:
Current Count: 2
Total Visitors: 5
Visitor IDs: [1, 3, 5, 7, 9]
Exit Zone:
Current Count: 1
Total Visitors: 3
Visitor IDs: [5, 7, 9]
=====================
- Use YOLOv8n for real-time performance
- GPU acceleration: Install PyTorch with CUDA support
- Lower resolution: Resize input frames if needed
- Reduce trail length: Modify
track_historymax length in code - Disable zones: Set
"enabled": falsefor unused zones
- Try different camera indices:
--source 1,--source 2, etc. - Check camera permissions
- Use smaller YOLOv8 model (yolov8n.pt)
- Reduce video resolution
- Ensure GPU drivers are installed
- Check lighting conditions
- Verify camera is working
- Try different YOLOv8 model
- Ensure people are clearly visible
- Python 3.8+
- OpenCV
- Ultralytics YOLOv8
- NumPy
- (Optional) CUDA-capable GPU for faster processing
.
├── people_detector.py # Main detection script
├── zone_manager.py # Zone creation and editing tool
├── zones.json # Zone configuration file
├── requirements.txt # Python dependencies
└── README.md # This file
This project uses YOLOv8 from Ultralytics (AGPL-3.0 license).
- YOLOv8 by Ultralytics
- OpenCV for computer vision operations