A robust Python-based system for detecting, tracking, and analyzing vehicles in video footage using YOLOv11 and ByteTrack. The system automatically identifies unique vehicles, captures their best frames, and performs license plate recognition with optional AI-powered vehicle analysis.
- Real-time Vehicle Detection: Uses YOLOv11 for accurate vehicle detection
- Robust Tracking: ByteTrack algorithm handles occlusions and ID switching
- Smart Vehicle Filtering: Saves only unique vehicles based on movement patterns and size
- License Plate Recognition: Automatic detection and extraction of license plates
- AI-Powered Analysis: Optional OpenAI integration for vehicle details (make, model, color, etc.)
- Adaptive Processing: Frame skipping for performance optimization
- Comprehensive Output: Generates both individual frames and review clips
- Python 3.8+
- CUDA-compatible GPU (optional, for acceleration)
- OpenAI API key (optional, for vehicle analysis)
-
Clone the repository:
git clone <your-repo-url> cd car_recognition
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Download required models (if not already included):
yolo11m.pt- YOLOv11 medium model for vehicle detectionlicense_plate_detector.pt- License plate detection model
Run the vehicle tracker on a video file:
python object_tracker.py path/to/your/video.mp4python object_tracker.py path/to/your/video.mp4 \
--model-path yolo11m.pt \
--output-dir tracking_output \
--conf 0.3video_path: Path to the input video file (required)--model-path: Path to YOLO model file (default:yolo11m.pt)--output-dir: Directory for output files (default:tracking_output)--conf: Confidence threshold for detection (default: 0.3)
- Processes video frames with adaptive skipping for performance
- Uses YOLOv11 to detect vehicles (cars, trucks, motorcycles, bicycles)
- Applies ByteTrack for robust multi-object tracking
A vehicle is considered "unique" and worth saving if it meets these criteria:
- Movement Pattern: Moving away from camera (getting smaller) and upward
- Size Threshold: Occupies at least 10% of screen area
- Fallback Rule: Appears in the bottom half of the frame
For each unique vehicle:
- License Plate Found: Saves the best frame as JPG in
counted_cars/ - No License Plate: Saves a review clip (MP4) in
review/ - Analysis: Optional OpenAI analysis for vehicle details
- 3-second cooldown prevents saving the same vehicle multiple times
- Grace period handles temporary occlusions
tracking_output/
├── video_name/
│ ├── counted_cars/
│ │ ├── 00-01-09_car_3.jpg
│ │ ├── 00-02-01_car_7.jpg
│ │ └── ...
│ ├── review/
│ │ ├── 00-03-37_car_10_review.mp4
│ │ └── ...
│ └── vehicle_analysis_results.csv
The tracker behavior can be customized by modifying bytetrack.yaml:
- Track buffer length
- High/low threshold ratios
- Match thresholds
- Frame rate settings
- YOLOv11n: Fastest, lower accuracy
- YOLOv11s: Balanced speed/accuracy
- YOLOv11m: Higher accuracy, slower (default)
- Hardware Acceleration: Automatically uses CUDA/MPS if available
- Frame Skipping: Adaptive skipping based on vehicle presence
- Memory Management: Efficient buffer management for long videos
- Model not found: Ensure
yolo11m.ptandlicense_plate_detector.ptare in the project directory - CUDA errors: The system will fall back to CPU if GPU is unavailable
- Memory issues: Reduce frame resolution or use a smaller YOLO model
- Use SSD storage for faster video I/O
- Ensure sufficient RAM (8GB+ recommended)
- Close other applications during processing
This project is licensed under the GNU Affero General Public License v3.0 - see the LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
- Ultralytics for YOLOv11
- ByteTrack for tracking algorithm
- OpenAI for vehicle analysis capabilities