Vehicles Counting using YOLOv4 + DeepSORT + Flask + Ngrok
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
References
I want to give my big thanks to all of these authors' repo:
Getting Started
This project has 3 main parts:
- Preparing data
- Training model using the power of YOLOv4
- Implementing DeepSORT algorithm for counting vehicles
Preparing data
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):
...
Training model using YOLOv4
Training model on your local computer is really complicated in environment installation and slow 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
Implementing DeepSORT algorithm for counting vehicles
First, setting up environment on your machine:
Conda (Recommended)
# 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
Pip
(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
Convert YOLOv4 model to Tensorflow Keras
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
Counting now!
Import VehiclesCounting
class in object_tracker.py
file and using run()
to start running:
# Import this main file
from object_tracker import VehiclesCounting
# Initialize
vc = VehiclesCounting(cam_name,
framework='tf',
weights='./checkpoints/yolov4-416',
size=416,
tiny=False,
model='yolov4',
video='./data/video/test.mp4',
output=None,
output_format='XVID',
iou=0.45,
score=0.5,
dont_show=False,
info=False,
count=False,
detect_line_position=0.5
detect_line_angle=0)
cam_name
: input your camera 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 objectscount
: count objects being tracked on screendetect_line_position
: (0..1) of height of video frame.detect_line_angle
: (0..180) degrees of detect line.
# Run it
vc.run()