A project for counting vehicles using YOLOv4
for training, DeepSORT
for tracking, Flask
for deploying to web (watch result purpose only) and Ngrok
for public IP address
This project has 3 main parts:
- Preparing data
- Training model using the power of YOLOv4
- Implementing DeepSORT algorithm for counting vehicles
Note: For private reason, please ask for permission before using datasets and pre-trained model!
Shortcuts | Links |
---|---|
📕 Colab notebooks | Part 1, Part 2, Part 3 |
📀 Datasets | Daytime, Nighttime |
🚂 My pre-trained model | GGDrive Mirror (Works well in well-lit conditions) |
I splitted my data into 2 scenes: daytime
and nighttime
, and training 8 classes (4 classes each scene, which are motorbike, car, bus, truck
).
Prepare your own data or you can download my cleaned data with annotations:
- Daytime dataset: GGDrive mirror
- Nighttime dataset: GGDrive mirror
If you prepare your own data, remember your annotation files fit this format:
- Every image has its own annotation file (
.txt
) - Each file contains a list of objects' bounding box (read this for more details):
<object-id> <x> <y> <width> <height>
...
Training model on your local computer is really complicated in environment installation and slow-like-a-snail if you don't have a powerful GPU. In this case, I used Google Colab.
Read more: Testing your trained model on local machine with OpenCV
First, setting up environment on your machine:
# Tensorflow CPU
conda env create -f conda-cpu.yml
conda activate yolov4-cpu
# Tensorflow GPU
conda env create -f conda-gpu.yml
conda activate yolov4-gpu
(TensorFlow 2 packages require a pip version > 19.0.)
# TensorFlow CPU
pip install -r requirements.txt
# TensorFlow GPU
pip install -r requirements-gpu.txt
# Google Colab
!pip install -r requirements-colab.txt
Copy your trained model in previous part to this project and run save_model.py
in cmd:
--weights
: Path to.weights
file (your trained model)--output
: Path to converted model.--model
: Model version (yolov4
in this case)
python save_model.py --weights ./yolov4_final.weights --output ./checkpoints/yolov4-416 --model yolov4
Download my
.weights
model if you want: GGDrive mirror
Import VehiclesCounting
class in object_tracker.py
file and using run()
to start running:
# Import this main file
from object_tracker import VehiclesCounting
# Initialize
# check the list of parameters below to modify values as you want
# check object_tracker.py file to check the default values
vc = VehiclesCounting()
# Run it
vc.run()
VehicleCounting
's parameters:
file_counter_log_name
: input your file counter log nameframework
: choose your model framework (tf, tflite, trt)weights
: path to your .weightssize
: resize images totiny
: (yolo,yolo-tiny)model
: (yolov3,yolov4)video
: path to your video or set 0 for webcam or youtube urloutput
: path to your resultsoutput_format
: codec used in VideoWriter when saving video to fileiou
: iou thresholdscore
: score thresholddont_show
: dont show video outputinfo
: show detailed info of tracked objectsdetect_line_position
: (0..1) of height of video frame.detect_line_angle
: (0..180) degrees of detect line.
I want to give my big thanks to all of these authors' repo: