CoolFace
Apppublic

sameelkhan20/vehicle-detection-app

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
App README

๐Ÿš— 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)

  1. 1.Fork this repository
  2. 2.Go to Hugging Face Spaces
  3. 3.Create a new Space with Docker SDK
  4. 4.Connect your GitHub repository
  5. 5.The app will automatically deploy!

Option 2: Local Docker

bash
# 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-detection

Option 3: Local Python

bash
# 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 interface
  • โ€”POST /upload - Upload video file
  • โ€”POST /process - Start video processing
  • โ€”GET /status/<process_id> - Check processing status
  • โ€”GET /download/<process_id>/<file_type> - Download results
  • โ€”POST /set_roi - Set Region of Interest
  • โ€”GET /get_counts - Get current vehicle counts
  • โ€”GET /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 /tmp directory
  • โ€”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

  1. 1.Fork the repository
  2. 2.Create a feature branch
  3. 3.Make your changes
  4. 4.Test thoroughly
  5. 5.Submit a pull request

๐Ÿ“„ License

This project is open source and available under the MIT License.

๐Ÿ†˜ Support

For issues or questions:

  1. 1.Check the troubleshooting section above
  2. 2.Create an issue in the repository
  3. 3.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..