sameelkhan20/vehicle-detection-app
0
๐ Vehicle Detection and Counting System
A real-time vehicle detection and counting system using YOLOv8, OpenCV, and Flask. This system can detect and track vehicles (cars, trucks, buses, motorcycles) in video streams and count them as they cross a defined Region of Interest (ROI).
๐ Features
- Real-time Vehicle Detection: Uses YOLOv8 for accurate vehicle detection
- Vehicle Tracking: Custom IoU-based tracker for consistent vehicle tracking
- ROI-based Counting: Define a region of interest to count vehicles crossing specific lines
- Multiple Vehicle Types: Detects cars, trucks, buses, and motorcycles
- Video Processing: Process uploaded videos or RTSP streams
- CSV Logging: Export vehicle crossing data to CSV files
- Speed Estimation: Calculate vehicle speeds in km/h
- Web Interface: Easy-to-use web interface for configuration and monitoring
- Docker Ready: Fully containerized for easy deployment
๐ Quick Start
Option 1: Hugging Face Spaces (Recommended)
- Fork this repository
- Go to Hugging Face Spaces
- Create a new Space with Docker SDK
- Connect your GitHub repository
- The app will automatically deploy!
Option 2: Local Docker
# Clone the repository
git clone <your-repo-url>
cd vehicle-detection-system
# Build and run with Docker
docker build -t vehicle-detection .
docker run -p 5000:5000 vehicle-detectionOption 3: Local Python
# Install dependencies
pip install -r requirements.txt
# Run the application
python start_robust.py๐ How to Use
1. Upload Video
- Click "Choose File" to upload a video file
- Supported formats: MP4, AVI, MOV, MKV, WMV, FLV, WebM
- Maximum file size: 16MB
2. Set Region of Interest (ROI)
- Click "Set ROI" to define the counting area
- Click on the video to create a polygon around the area you want to monitor
- The system will automatically create counting lines (entry/exit) within the ROI
3. Process Video
- Click "Process Video" to start processing
- The system will:
- Detect vehicles in each frame
- Track vehicles across frames
- Count vehicles crossing the ROI lines
- Generate a processed video with bounding boxes and counts
- Create a CSV log of all vehicle crossings
4. Download Results
- Once processing is complete, download:
- Processed Video: Video with detections, tracks, and counts
- CSV Log: Detailed log of vehicle crossings with timestamps
5. RTSP Stream (Optional)
- For real-time monitoring, use the RTSP stream option
- Enter your RTSP URL (e.g.,
rtsp://your-camera-ip:554/stream) - The system will process the live stream and save the results
๐ฏ Vehicle Types Detected
- Car (Green bounding box)
- Motorcycle (Blue bounding box)
- Bus (Red bounding box)
- Truck (Cyan bounding box)
๐ Output Information
Processed Video
- Bounding boxes around detected vehicles
- Vehicle IDs and types
- Direction arrows showing movement
- Speed estimates (when available)
- Real-time count display (IN/OUT)
- ROI polygon and counting lines
CSV Log
- Timestamp of each vehicle crossing
- Vehicle track ID
- Vehicle type
- Direction (IN/OUT)
- Line type (entry/exit)
โ๏ธ Technical Details
- Detection Model: YOLOv8n (nano version for fast processing)
- Tracking: Custom IoU-based tracker
- Confidence Threshold: 0.5 (adjustable)
- Minimum Detection Area: 500 pixels
- ROI Line Threshold: 100 pixels
- Speed Calculation: Based on pixel movement and frame rate
๐ง API Endpoints
GET /- Main interfacePOST /upload- Upload video filePOST /process- Start video processingGET /status/<process_id>- Check processing statusGET /download/<process_id>/<file_type>- Download resultsPOST /set_roi- Set Region of InterestGET /get_counts- Get current vehicle countsGET /health- Health check endpoint
๐ณ Docker Configuration
The application is fully containerized with:
- Base Image: Python 3.9-slim
- Dependencies: All required system and Python packages
- Health Check: Built-in health monitoring
- Environment: Production-ready configuration
๐ Project Structure
vehicle-detection-system/
โโโ app.py # Main Flask application
โโโ vehicle_detector.py # YOLOv8 vehicle detection
โโโ vehicle_tracker.py # Custom IoU-based tracking
โโโ roi_manager.py # Region of Interest management
โโโ video_processor.py # Video processing pipeline
โโโ start_robust.py # Robust startup script
โโโ requirements.txt # Python dependencies
โโโ Dockerfile # Docker configuration
โโโ templates/
โ โโโ index.html # Web interface
โโโ README.md # This file๐ ๏ธ Troubleshooting
Common Issues
No Vehicles Detected
- Check if the video quality is good
- Ensure vehicles are clearly visible
- Try adjusting the confidence threshold
- Make sure the ROI is set correctly
Counting Issues
- Verify the ROI polygon covers the area where vehicles cross
- Check that counting lines are visible in the processed video
- Ensure vehicles are moving through the ROI area
Performance Issues
- Large video files may take longer to process
- RTSP streams require stable network connection
- Processing time depends on video length and resolution
Permission Errors
- The app automatically handles permission issues
- Logs are stored in
/tmpdirectory - All directories are created with proper permissions
๐ Requirements
- Python 3.9+
- Flask 2.3.3
- OpenCV 4.8.1
- Ultralytics YOLOv8
- PyTorch 2.0.1
- Docker (for containerized deployment)
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
๐ License
This project is open source and available under the MIT License.
๐ Support
For issues or questions:
- Check the troubleshooting section above
- Create an issue in the repository
- Check the logs for detailed error information
๐ Acknowledgments
- YOLOv8 by Ultralytics for object detection
- OpenCV for computer vision operations
- Flask for the web framework
- Hugging Face for hosting platform
Ready to deploy! ๐ Your vehicle detection system is now fully configured for Hugging Face Spaces deployment..
