A real-time person detection application using YOLO (You Only Look Once) with OpenCV and Supervision for video processing and annotation.
- Real-time person detection using YOLO models (YOLOv8)
- Multiple input sources: Webcam, video files, or streaming URLs
- Interactive GUI with live video feed and statistics
- Command-line interface for batch processing
- Configurable confidence thresholds
- Frame saving and video output capabilities
- Detection statistics and performance metrics
- Create and activate a virtual environment:
# Using conda (recommended)
conda create -n person-detection python=3.9
conda activate person-detection
# OR using venv
python -m venv person-detection-env
source person-detection-env/bin/activate # Linux/Mac
# person-detection-env\Scripts\activate # Windows
- Install the required dependencies:
pip install -r requirements.txt
Why virtual environments are essential:
- Prevents conflicts with system Python packages
- Ensures reproducible installations across different machines
- Isolates project dependencies from other projects
- Standard practice in professional Python development
Run the graphical interface:
python gui_app.py
GUI Features:
- Load different YOLO models (n, s, m, l, x variants)
- Adjust confidence threshold with slider
- Start webcam for live detection
- Load and process video files
- Save current frames
- View real-time statistics
Run person detection from command line:
# Use webcam (default)
python person_detector.py
# Process a video file
python person_detector.py --source path/to/video.mp4
# Save output video
python person_detector.py --source input.mp4 --output output.mp4
# Use different model and confidence
python person_detector.py --model yolov8s.pt --confidence 0.7
# Process without display (headless)
python person_detector.py --source video.mp4 --no-display
Command Line Options:
--source, -s
: Video source (file path, webcam index, or URL)--output, -o
: Output video path (optional)--model, -m
: YOLO model path (default: yolov8n.pt)--confidence, -c
: Confidence threshold (default: 0.5)--no-display
: Don't display video in real-time
The app supports different YOLOv8 models:
- yolov8n.pt: Nano - Fastest, least accurate
- yolov8s.pt: Small - Good balance
- yolov8m.pt: Medium - Better accuracy
- yolov8l.pt: Large - High accuracy
- yolov8x.pt: Extra Large - Best accuracy, slowest
Models will be automatically downloaded on first use.
- Load Model: Initialize YOLO with selected model
- Start Webcam: Begin live detection from webcam
- Load Video: Select and process a video file
- Save Frame: Export current frame as image
- 'q': Quit application
- 's': Save current frame
- ultralytics: YOLOv8 implementation
- opencv-python: Video processing and display
- supervision: Detection annotation and utilities
- numpy: Numerical operations
- Pillow: Image processing (GUI only)
- Load YOLOv8 model (COCO pre-trained)
- Process video frame by frame
- Filter detections for person class (class_id = 0)
- Apply confidence threshold
- Annotate frames with bounding boxes and labels
- Display results and update statistics
- Use smaller models (yolov8n) for real-time applications
- Increase confidence threshold to reduce false positives
- Process at lower resolution for better performance
- Use GPU acceleration if available (CUDA)
person-detection-app/
├── person_detector.py # Core detection class and CLI
├── gui_app.py # GUI application
├── requirements.txt # Python dependencies
└── README.md # This file
The application will display:
- Bounding boxes around detected persons
- Confidence scores for each detection
- Frame count and detection statistics
- Real-time processing information
Common Issues:
- Webcam not working: Check camera permissions and try different indices (0, 1, 2)
- Model download fails: Ensure internet connection for first-time model download
- Poor performance: Try smaller model (yolov8n) or lower resolution
- No detections: Lower confidence threshold or check lighting conditions
System Requirements:
- Python 3.8+
- Webcam (for live detection)
- 4GB+ RAM recommended
- GPU optional but recommended for better performance
This project uses open-source libraries and pre-trained models. Please check individual library licenses for commercial use.