# Bus Crowd Detection System

Managing crowd levels in public buses is a challenge for many cities around the world. Overcrowding can lead to safety concerns, discomfort, and delays. To help address this issue, I've developed a Bus Crowd Detection System using computer vision and email notifications for real-time crowd monitoring. This system not only detects people in a bus but also sends out notifications when it detects a crowd, allowing for proactive management and improved passenger safety. Here's how it all works.

[Github](https://github.com/Neelpatel1604/Person-Detection)

### Key Features of the Bus Crowd Detection System

The Bus Crowd Detection System is packed with features designed to monitor and respond to real-time crowding:

1. Real-Time Person Detection: By leveraging the efficiency of YOLOv4-tiny, the system can detect people in a video feed from either a webcam or an IP camera.
    
2. Email Notifications: If the system detects an excessive crowd, it automatically sends an email notification to a predefined recipient.
    
3. Data Storage for Analysis: Detections are logged in an SQLite database so that you can track crowding over time and analyze patterns.
    
4. Configurable Notification Cooldown: Avoids spamming by setting a custom cooldown period between email alerts.
    
5. Secure Credential Management: Uses environment variables to store email credentials securely.
    

### Prerequisites for Setting Up the System

To build and run this project, you’ll need:

* Python 3.7 or higher
    
* A Webcam or IP camera
    
* Gmail account (with App Password enabled for secure access)
    
* Git to clone the project repositor
    

### Step-by-Step Installation Guide

Here’s how to set up and run the Bus Crowd Detection System:

Clone the Repository Start by cloning the GitHub repository to your local system:

`git clone` [`https://github.com/Neelpatel1604/Person-Detection`](https://github.com/Neelpatel1604/Person-Detection)

2\. Install Required Packages

Next, navigate to the project directory and install the necessary Python packages:

`cd bus-crowd-detection`

`pip install -r requirements.txt`

3. Set Up Email Credentials For the system to send email notifications, create a .env file in the root directory and add your email details as environment variables:
    

`SENDER_EMAIL=your_email`

`SENDER_PASSWORD=your_app_password`

`RECIPIENT_EMAIL=recipient_email`

Make sure your Gmail account has an App Password enabled, as this provides a more secure way to access your account.

### Download YOLOv4-Tiny Weights

YOLOv4-tiny requires specific weights to perform accurate detection. Follow these steps:

Create a models folder in the project root.

Download the [yolov4-tiny.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights), [Coco name](https://github.com/Neelpatel1604/Person-Detection/blob/main/models/coco.names) , [yolov4-tiny.cgf](https://github.com/Neelpatel1604/Person-Detection/blob/main/models/yolov4-tiny.cfg) file and place it into models folder

Or Coco name and yolov4-tiny.cgf will be in your system when you clone the repo.

### Running the System

Once everything is set up, you can start the detection system:

`python main.py`

While the program is running:

* Press 'q' to quit the program.
    
* The detection runs automatically, with bounding boxes displayed around detected people.
    
* Email notifications are triggered based on the set crowd threshold.
    

### How the System Works:Features in Detail

**Person Detection with YOLOv4-Tiny** YOLOv4-tiny is a streamlined version of the YOLOv4 model, designed for faster detection with minimal loss of accuracy. When running, the system detects people in real-time, drawing bounding boxes around them and showing a confidence score to indicate detection reliability.

**Email Notifications for Real-Time Alerts** One of the most valuable aspects of this system is its ability to send email alerts when the crowd level reaches a certain threshold. This feature is configurable and prevents multiple emails from being sent in a short period through a cooldown period, giving administrators actionable insights without overload.

**Data Logging with SQLite** For historical data analysis, each detection is stored in an SQLite database with a timestamp, count, and occupancy rate. This data enables you to track crowding patterns and make decisions based on data trends.

### Troubleshooting Common Issues

While setting up, you might encounter some common issues. Here are some tips for resolving them:

* Email Not Sending:
    
    * Confirm your Gmail App Password is correct.
        
    * Check your internet connection.
        
    * Ensure your .env file is correctly set up with all the required variables.
        
* Detection Not Working:
    
    * Make sure your camera is properly connected and functioning.
        
    * Confirm that yolov4-tiny.weights is in the models folder.
        
    * Improve lighting for better detection accuracy.
        
* Database Errors:
    
    * Check write permissions on the database file.
        
    * Ensure SQLite is installed and working on your system.
        

### Understanding the Project Structure

Here’s a quick look at the key files in the project:

* `main.py`: The entry point to run the system.
    
* `crowd_detection.py`: Contains the logic for detecting people.
    
* `db_handler.py`: Manages database operations.
    
* `requirements.txt`: Lists the Python packages needed to run the system.
    
* `.env`: Stores sensitive environment variables (like email credentials).
    

### Security Recommendations

This project uses email credentials to send notifications, so it's essential to follow these security tips:

* Never share your .env file with others.
    
* Use Gmail App Passwords for enhanced security.
    
* Keep your model files secure to prevent unauthorized modifications.
    

### Contributing to the Project

If you’d like to add features or improvements, contributions are always welcome! Here’s how you can contribute:

1. Fork the repository.
    
2. Create a new branch for your feature.
    
3. Submit a pull request with a brief description of your changes.
    

### Real life implementation

1. **Install Cameras on Buses**: Set up a webcam or IP camera inside each bus to capture real-time video footage of passengers.
    
2. **Deploy YOLOv4-tiny Model**: Use the YOLOv4-tiny model for person detection. This lightweight model is ideal for real-time processing and works efficiently on edge devices like Raspberry Pi or embedded systems.
    
3. **Set Up a Detection Server**: Run the detection software on a server or embedded device onboard the bus. This will handle video processing, person detection, and occupancy calculations.
    
4. **Database Integration**: Store detection data, including bus ID, passenger count, occupancy rate, and timestamp, in a local SQLite database or cloud storage for analysis.
    
5. **Email Alerts**: Configure email notifications to alert the fleet manager when the bus exceeds a certain occupancy rate or crowd threshold.
    
6. **Monitor and Optimize**: Use the stored data to optimize bus routes, schedules, and manage crowding, improving passenger safety and comfort.
    

### Conclusion

With this Bus Crowd Detection System, you can monitor bus occupancy in real time, allowing for faster responses to overcrowding and improved safety for all passengers. Whether you’re managing a public transportation fleet or just interested in computer vision projects, this system offers a practical and easy-to-implement solution for real-world challenges.
