This repository contains the code for a vehicle flow automatic detection system using YOLOv5, incorporating a weather recognition function based on ResNet50.
The project implements basic functionalities including real-time detection of different vehicle types and real-time vehicle counts in both north and south directions. It features a comprehensive GUI that allows manual setting of maximum vehicle flow thresholds for different directions, issuing warnings when thresholds are exceeded. Additionally, the project can automatically adjust thresholds based on recognized weather conditions (maximum vehicle flow: sunny > rainy > snowy). Lastly, there is an incomplete function for detecting illegal lane changes across solid lines.
p22_video.-.Compressed.with.FlexClip.mp4
- Tip: It is highly recommended to use a device with a GPU to run this project.
- Install Python 3.9.19.
- Install necessary dependencies:
pip install -r requirements.txt -f [https://download.pytorch.org/whl/torch_stable.html](https://download.pytorch.org/whl/torch_stable.html)
- Preparing Data:
- Download the dataset: Link
- Place the images from the dataset in the
./VOC/images/folder. - Place the label
.xmlfiles in the./VOC/xml_dataset/folder. - Ensure the filenames match the expected format.
- Preparing Model Weight:
- Download model weight: Link
- Extract both folders from the zip file to the project's root directory.
-
Set the working directory: Navigate to the root directory of the project.
cd Automated-Traffic-Monitoring-System -
Preprocess Training and Validation Data:
- Run
python VOC/maketxt.py- This script splits the dataset into training and validation sets and generates
.txtfiles containing the respective image IDs in theVOC/txt_dataset/folder.
- This script splits the dataset into training and validation sets and generates
- Run
python VOC/voc_label_coco.py- This script converts the
.xmllabel files (from UA-DETRAC) into.txtlabel files suitable for YOLO and saves them in theVOC/labels/folder. - It also generates two
.txtfiles (train.txtandval.txt) in theVOC/list/folder, containing the image paths for the training and validation sets.
- This script converts the
- Run
-
Run the Training Script:
- Execute
python train.py - This will train the YOLOv5 model. Training results, including model weights (
best.pt,last.pt) and performance graphs, will be saved toruns/train/exp. Subsequent training runs will create folders likeexp2,exp3, etc.
- Execute
-
Run the Traffic Flow Detection System:
- Modify the model path variable in line 14 of the
main.pyfile to point to your trained model weights. The default path isruns/train/exp/weights/best.pt. - Execute
python main.py - This will launch the GUI for the vehicle flow detection system, integrating all functionalities.
Important Tip: The illegal lane change detection function is not enabled by default. This feature requires manual adjustment of the Region of Interest (ROI) specific to each video. To enable it:
- Uncomment lines 223 to 234 in
main.py. - Uncomment lines 252 to 257 in
main.py. - Modify the ROI settings defined in lines 33 to 36 of
main.pyaccording to your video's perspective.
- Modify the model path variable in line 14 of the
- Weather Recognition Model: Due to time constraints, an open-source weather recognition model was used directly (The original link of the weather recognition model is here). This model was not trained on our specific traffic dataset. Consequently, if a video frame primarily shows the road without distinct weather features (like clear sky, rain, snow), the weather prediction might be inaccurate. Training the provided weather model architecture (
model.py) using our traffic dataset annotated with weather labels would resolve this. - Kalman Filter Speed Estimation: The current Kalman filter implementation tracks objects but cannot accurately estimate their real-world speed.
- Lane Detection Applicability: The lane detection function is best suited for highway monitoring scenarios where the camera has a fixed viewing angle and the lane lines are relatively straight.

