Skip to content

A project for counting vehicles using YOLOv4 + DeepSORT + Flask + Ngrok + TF2

License

Notifications You must be signed in to change notification settings

duongttr/vehicles-counting-yolov4-deepsort

Repository files navigation

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

Getting Started

This project has 3 main parts:

  1. Preparing data
  2. Training model using the power of YOLOv4
  3. Implementing DeepSORT algorithm for counting vehicles

Shortcuts

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)

Preparing data

Preparing data notebook

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:

If you prepare your own data, remember your annotation files fit this format:

  1. Every image has its own annotation file (.txt)
  2. Each file contains a list of objects' bounding box (read this for more details):
<object-id> <x> <y> <width> <height>
...

Training model using YOLOv4

Training model notebook

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

Implementing DeepSORT algorithm for counting vehicles

Implementing DeepSORT notebook

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
# 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 name
  • framework: choose your model framework (tf, tflite, trt)
  • weights: path to your .weights
  • size: resize images to
  • tiny: (yolo,yolo-tiny)
  • model: (yolov3,yolov4)
  • video: path to your video or set 0 for webcam or youtube url
  • output: path to your results
  • output_format: codec used in VideoWriter when saving video to file
  • iou: iou threshold
  • score: score threshold
  • dont_show: dont show video output
  • info: show detailed info of tracked objects
  • detect_line_position: (0..1) of height of video frame.
  • detect_line_angle: (0..180) degrees of detect line.

Contact me

References

I want to give my big thanks to all of these authors' repo:

About

A project for counting vehicles using YOLOv4 + DeepSORT + Flask + Ngrok + TF2

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published