Vehicles Counting using YOLOv4 + DeepSORT + Flask + Ngrok

Overview

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):

   
    
    
     
     
      
      
       
       
       
         ... 
       
      
      
     
     
    
    
   
   

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

Comments
  • PermissionError: [Errno 13] Unable to create file (unable to open file: name = './checkpoints/yolov4-416', errno = 13, error message = 'Permission denied', flags = 13, o_flags = 302)

    PermissionError: [Errno 13] Unable to create file (unable to open file: name = './checkpoints/yolov4-416', errno = 13, error message = 'Permission denied', flags = 13, o_flags = 302)

    PermissionError: [Errno 13] Unable to create file (unable to open file: name = './checkpoints/yolov4-416', errno = 13, error message = 'Permission denied', flags = 13, o_flags = 302)

    help wanted 
    opened by byq-luo 4
  • tensorflow installation

    tensorflow installation

    Hello, i have been trying to install the requirements of this project on a new computer, when i try to install the gpu requirements using the following command:

    pip install -r requirements-gpu.txt

    i get this error from tensorflow package: ERROR: Could not find a version that satisfies the requirement tensorflow-gpu==2.3.0rc0 (from versions: 1.13.1, 1.13.2, 1.14.0, 1.15.0, 1.15.2, 1.15.3, 1.15.4, 1.15.5, 2.0.0, 2.0.1, 2.0.2, 2.0.3, 2.0.4, 2.1.0, 2.1.1, 2.1.2, 2.1.3, 2.1.4, 2.2.0, 2.2.1, 2.2.2, 2.2.3, 2.3.0, 2.3.1, 2.3.2, 2.3.3, 2.3.4, 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4, 2.5.0, 2.5.1, 2.5.2, 2.6.0, 2.6.1, 2.6.2, 2.7.0rc0, 2.7.0rc1, 2.7.0) ERROR: No matching distribution found for tensorflow-gpu==2.3.0rc0 any idea on how i can fix/bypass this problem? thanks.

    opened by YeetMasterYeet 2
  • USB CAMERA

    USB CAMERA

    hello, i have been using this repository and i cannot seem to make it work using a usb camera. i have been able to run the code on recorded videos, on my integrated camera but i cannot make my usb camera work. one thing i noticed in my device managed is that my usb camera does not appear under the camera section, it has its own section as a USB device. I have tried everything such but nothing seems to work:

    • changing the 0 for integrated cam to 1, 2,3...
    • giving the camera access path
    • trying to place the usb camera under the cam section in device manager. is there any workaround to this problem or any indication on how to make the camera. thanks.
    opened by YeetMasterYeet 2
  • multiple usb cameras

    multiple usb cameras

    hello @duongttr, i have seen the referenced multi image tracking GitHub project, but i want to use multiple usb cameras and not ip cameras, is there a way to do that with this project (run detection, tracking and counting on multiple usb cameras). thank you.

    opened by YeetMasterYeet 2
  • I Found in this Project /video_feed route will get 500

    I Found in this Project /video_feed route will get 500

    I Found in this Project /video_feed route will get 500

    error msg:

    Error on request:
    Traceback (most recent call last):
      File "/usr/local/lib/python3.7/dist-packages/werkzeug/serving.py", line 323, in run_wsgi
        execute(self.server.app)
      File "/usr/local/lib/python3.7/dist-packages/werkzeug/serving.py", line 314, in execute
        for data in application_iter:
      File "/usr/local/lib/python3.7/dist-packages/werkzeug/wsgi.py", line 506, in __next__
        return self._next()
      File "/usr/local/lib/python3.7/dist-packages/werkzeug/wrappers/base_response.py", line 45, in _iter_encoded
        for item in iterable:
      File "/content/vehicles-counting-yolov4-deepsort/object_tracker_colab.py", line 119, in run
        video_path = pafy.new(self._video).getbest(preftype="mp4").url
      File "/usr/local/lib/python3.7/dist-packages/pafy/pafy.py", line 124, in new
        return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts)
      File "/usr/local/lib/python3.7/dist-packages/pafy/backend_youtube_dl.py", line 31, in __init__
        super(YtdlPafy, self).__init__(*args, **kwargs)
      File "/usr/local/lib/python3.7/dist-packages/pafy/backend_shared.py", line 97, in __init__
        self._fetch_basic()
      File "/usr/local/lib/python3.7/dist-packages/pafy/backend_youtube_dl.py", line 54, in _fetch_basic
        self._dislikes = self._ydl_info['dislike_count']
    KeyError: 'dislike_count'
    
    
    bug good first issue 
    opened by pokobunhsu 2
  • custom counting class.

    custom counting class.

    hello, i am having an issue with the counting mechanism. i have used a custom yolo weights file, when the main.py, my video gets loaded and all of the custom objects get detected but the function that give you the total count, up and down count is not accurate. how can i calibrate it so that the objects that pass the line get counter accurately. thank you.

    opened by YeetMasterYeet 0
  • matrix contains invalid numeric entries

    matrix contains invalid numeric entries

    File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\object_tracker.py", line 302, in run tracker.update(detections) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\deep_sort\tracker.py", line 70, in update self._match(detections) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\deep_sort\tracker.py", line 116, in _match self.tracks, detections, confirmed_tracks) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\deep_sort\linear_assignment.py", line 139, in matching_cascade track_indices_l, unmatched_detections) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\deep_sort\linear_assignment.py", line 58, in min_cost_matching indices = linear_sum_assignment(cost_matrix) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\scipy\optimize_lsap.py", line 93, in linear_sum_assignment raise ValueError("matrix contains invalid numeric entries") ValueError: matrix contains invalid numeric entries

    opened by irfanahme 0
  • ValueError: Duplicate node name in graph: 'packed'

    ValueError: Duplicate node name in graph: 'packed'

    Traceback (most recent call last): File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 11288, in tile name, _ctx._post_execution_callbacks, input, multiples) tensorflow.python.eager.core._FallbackException: This function does not handle the case of the path where all inputs are not already EagerTensors.

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1610, in _create_c_op c_op = c_api.TF_FinishOperation(op_desc) tensorflow.python.framework.errors_impl.InvalidArgumentError: Duplicate node name in graph: 'packed'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "save_model.py", line 58, in app.run(main) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\absl\app.py", line 312, in run _run_main(main, args) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\absl\app.py", line 258, in _run_main sys.exit(main(argv)) File "save_model.py", line 54, in main save_tf() File "save_model.py", line 36, in save_tf output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\core\yolov4.py", line 169, in decode return decode_tf(conv_output, output_size, NUM_CLASS, STRIDES, ANCHORS, i=i, XYSCALE=XYSCALE) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\core\yolov4.py", line 204, in decode_tf xy_grid = tf.tile(tf.expand_dims(xy_grid, axis=0), [batch_size, 1, 1, 3, 1]) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 11293, in tile input, multiples, name=name, ctx=_ctx) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 11340, in tile_eager_fallback _attr_Tmultiples, (multiples,) = _execute.args_to_matching_eager([multiples], _ctx, _dtypes.int32) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\eager\execute.py", line 257, in args_to_matching_eager t, dtype, preferred_dtype=default_dtype, ctx=ctx)) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1296, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 1278, in _autopacking_conversion_function return _autopacking_helper(v, dtype, name or "packed") File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 1214, in _autopacking_helper return gen_array_ops.pack(elems_as_tensors, name=scope) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 6304, in pack "Pack", values=values, axis=axis, name=name) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 793, in _apply_op_helper op_def=op_def) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 548, in create_op compute_device) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3429, in _create_op_internal op_def=op_def) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1773, in init control_input_ops) File "D:\Vehicle-Counting-master\vehicles-counting-yolov4-deepsort\testw\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1613, in _create_c_op raise ValueError(str(e)) ValueError: Duplicate node name in graph: 'packed'

    opened by irfanahme 1
Owner
Duong Tran Thanh
I love learning AI and mobile development
Duong Tran Thanh
WHENet - ONNX, OpenVINO, TFLite, TensorRT, EdgeTPU, CoreML, TFJS, YOLOv4/YOLOv4-tiny-3L

HeadPoseEstimation-WHENet-yolov4-onnx-openvino ONNX, OpenVINO, TFLite, TensorRT, EdgeTPU, CoreML, TFJS, YOLOv4/YOLOv4-tiny-3L 1. Usage $ git clone htt

Katsuya Hyodo 49 Sep 21, 2022
YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )

Yolo v4, v3 and v2 for Windows and Linux (neural networks for object detection) Paper YOLO v4: https://arxiv.org/abs/2004.10934 Paper Scaled YOLO v4:

Alexey 20.2k Jan 9, 2023
using yolox+deepsort for object-tracker

YOLOX_deepsort_tracker yolox+deepsort实现目标跟踪 最新的yolox尝尝鲜~~(yolox正处在频繁更新阶段,因此直接链接yolox仓库作为子模块) Install Clone the repository recursively: git clone --rec

null 245 Dec 26, 2022
Yolov5 deepsort inference,使用YOLOv5+Deepsort实现车辆行人追踪和计数,代码封装成一个Detector类,更容易嵌入到自己的项目中

使用YOLOv5+Deepsort实现车辆行人追踪和计数,代码封装成一个Detector类,更容易嵌入到自己的项目中。

null 813 Dec 31, 2022
yolov5 deepsort 行人 车辆 跟踪 检测 计数

yolov5 deepsort 行人 车辆 跟踪 检测 计数 实现了 出/入 分别计数。 默认是 南/北 方向检测,若要检测不同位置和方向,可在 main.py 文件第13行和21行,修改2个polygon的点。 默认检测类别:行人、自行车、小汽车、摩托车、公交车、卡车。 检测类别可在 detect

null 554 Dec 30, 2022
StrongSORT: Make DeepSORT Great Again

StrongSORT StrongSORT: Make DeepSORT Great Again StrongSORT: Make DeepSORT Great Again Yunhao Du, Yang Song, Bo Yang, Yanyun Zhao arxiv 2202.13514 Abs

null 369 Jan 4, 2023
Implementation of "Scaled-YOLOv4: Scaling Cross Stage Partial Network" using PyTorch framwork.

YOLOv4-large This is the implementation of "Scaled-YOLOv4: Scaling Cross Stage Partial Network" using PyTorch framwork. YOLOv4-CSP YOLOv4-tiny YOLOv4-

Kin-Yiu, Wong 2k Jan 2, 2023
Train Yolov4 using NBX-Jobs

yolov4-trainer-nbox Train Yolov4 using NBX-Jobs. Use the powerfull functionality available in nbox-SDK repo to train a tiny-Yolo v4 model on Pascal VO

Yash Bonde 1 Jan 12, 2022
Various operations like path tracking, counting, etc by using yolov5

Object-tracing-with-YOLOv5 Various operations like path tracking, counting, etc by using yolov5

Pawan Valluri 5 Nov 28, 2022
Range Image-based LiDAR Localization for Autonomous Vehicles Using Mesh Maps

Range Image-based 3D LiDAR Localization This repo contains the code for our ICRA2021 paper: Range Image-based LiDAR Localization for Autonomous Vehicl

Photogrammetry & Robotics Bonn 208 Dec 15, 2022
GndNet: Fast ground plane estimation and point cloud segmentation for autonomous vehicles using deep neural networks.

GndNet: Fast Ground plane Estimation and Point Cloud Segmentation for Autonomous Vehicles. Authors: Anshul Paigwar, Ozgur Erkent, David Sierra Gonzale

Anshul Paigwar 114 Dec 29, 2022
这是一个mobilenet-yolov4-lite的库,把yolov4主干网络修改成了mobilenet,修改了Panet的卷积组成,使参数量大幅度缩小。

YOLOV4:You Only Look Once目标检测模型-修改mobilenet系列主干网络-在Keras当中的实现 2021年2月8日更新: 加入letterbox_image的选项,关闭letterbox_image后网络的map一般可以得到提升。

Bubbliiiing 65 Dec 1, 2022
YOLTv4 builds upon YOLT and SIMRDWN, and updates these frameworks to use the most performant version of YOLO, YOLOv4

YOLTv4 builds upon YOLT and SIMRDWN, and updates these frameworks to use the most performant version of YOLO, YOLOv4. YOLTv4 is designed to detect objects in aerial or satellite imagery in arbitrarily large images that far exceed the ~600×600 pixel size typically ingested by deep learning object detection frameworks.

Adam Van Etten 161 Jan 6, 2023
PyTorch ,ONNX and TensorRT implementation of YOLOv4

PyTorch ,ONNX and TensorRT implementation of YOLOv4

null 4.2k Jan 1, 2023
I tried to apply the CAM algorithm to YOLOv4 and it worked.

YOLOV4:You Only Look Once目标检测模型在pytorch当中的实现 2021年2月7日更新: 加入letterbox_image的选项,关闭letterbox_image后网络的map得到大幅度提升。 目录 性能情况 Performance 实现的内容 Achievement

null 55 Dec 5, 2022
People movement type classifier with YOLOv4 detection and SORT tracking.

Movement classification The goal of this project would be movement classification of people, in other words, walking (normal and fast) and running. Yo

null 4 Sep 21, 2021
Implementing yolov4 target detection and tracking based on nao robot

Implementing yolov4 target detection and tracking based on nao robot

null 6 Apr 19, 2022
A tutorial on training a DarkNet YOLOv4 model for the CrowdHuman dataset

YOLOv4 CrowdHuman Tutorial This is a tutorial demonstrating how to train a YOLOv4 people detector using Darknet and the CrowdHuman dataset. Table of c

JK Jung 118 Nov 10, 2022
YOLOv4-v3 Training Automation API for Linux

This repository allows you to get started with training a state-of-the-art Deep Learning model with little to no configuration needed! You provide your labeled dataset or label your dataset using our BMW-LabelTool-Lite and you can start the training right away and monitor it in many different ways like TensorBoard or a custom REST API and GUI. NoCode training with YOLOv4 and YOLOV3 has never been so easy.

BMW TechOffice MUNICH 626 Dec 31, 2022