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:

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

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 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
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 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
  • count: count objects being tracked on screen
  • detect_line_position: (0..1) of height of video frame.
  • detect_line_angle: (0..180) degrees of detect line.

# Run it
vc.run()

Contact me

GitHub

GitHub - duongttr/vehicles-counting-yolov4-deepsort at pythonawesome.com
A project for counting vehicles using YOLOv4 + DeepSORT + Flask + Ngrok - GitHub - duongttr/vehicles-counting-yolov4-deepsort at pythonawesome.com