This project is a traffic detection system built using YOLOv5, Streamlit, FastAPI, and PyTorch. The goal is to detect traffic objects in images using a trained YOLOv5 model, with options to deploy via Streamlit or FastAPI.
streamlit/
: Streamlit app to upload images and run inference.src/
: Core Python scripts for data preparation, model training, and inference.saved_models/
: Folder to store YOLOv5 trained model weights.data/
: Dataset for training and evaluation.uploads/
: Uploaded images for inference.templates/
: HTML templates for the FastAPI frontend.static/
: Static files (e.g., images) for the FastAPI frontend.
conda env create -f environment.yml
conda activate dhaka-traffic-detection
To run the Streamlit app locally:
streamlit run your_app.py --server.runOnSave=false --global.developmentMode=false
Your app will be available locally at http://localhost:8501
.
To run the FastAPI app locally:
uvicorn main:app --reload --port 8080
This will start the FastAPI server locally at http://127.0.0.1:8080
.
Go to the http://localhost:8000/
- to start the frontend
- GET /: Renders the HTML page to upload an image for traffic detection.
- POST /upload/: Uploads an image and returns the processed image with detected objects.
Example to upload an image using curl
:
curl -X 'POST' \
'http://127.0.0.1:8080/upload/' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'file=@your_image.jpg;type=image/jpeg'
Push the project to GitHub and deploy it to Streamlit Cloud using the following steps:
- Push your project to GitHub.
- Go to Streamlit Cloud and create a new app.
- Link it to your GitHub repository and select
streamlit_app.py
as the main file. - Deploy your app to make it accessible online.
- Python 3.9
- Streamlit
- FastAPI
- Uvicorn
- PyTorch
- YOLOv5
- Pillow
- streamlit
- kaggle
- opencv-python
- torch
- python-multipart
- Activate your Conda environment:
conda activate dhaka-traffic-detection
- Download the Dataset from Kaggle:
This project uses a dataset hosted on Kaggle. Follow these steps to download the dataset:
- Install the Kaggle API (if not already installed):
pip install kaggle
- Set up the Kaggle API credentials:
- Download the
kaggle.json
file from your Kaggle account by navigating to the API section and creating a new API token. - Move the
kaggle.json
file to the appropriate location:
- Download the
mkdir ~/.kaggle
mv ~/Downloads/kaggle.json ~/.kaggle/
chmod 600 ~/.kaggle/kaggle.json
- Download the dataset:
Use the following command to download the YOLOv5 weights dataset into the
saved_models/
folder:
kaggle datasets download -d aifahim/dhaka-ai-traffic-challenge-weights-yolov5 -p saved_models --unzip
This will download and unzip the trained YOLOv5 weights into the saved_models/
folder.
- Push your project to a GitHub repository.
- Go to Streamlit Cloud and create a new app.
- Link it to your GitHub repository and select
streamlit/app.py
as the main file. - Deploy your app to make it accessible online!
- For FastAPI, ensure that you have installed
uvicorn
, and run the app using the command provided in the "Running the FastAPI App" section. - This setup will handle everything from model inference to deployment, both using Streamlit and FastAPI.