LdDl / rust-road-traffic

Vehicle counting/tracking and speed estimation
19 stars 3 forks source link
darknet darknet-yolo multiobject-tracking object-detection opencv opencv-rust opencv4 tracking-algorithm vehicle-detection yolo-tiny yolov4 yolov8

Rust toy utility for monitoring road traffic

Table of Contents

Video showcase

About

Vehicle detection/tracking and speed estimation via next instruments:

  1. Rust programming language - https://www.rust-lang.org/
  2. OpenCV bindings - https://github.com/twistedfall/opencv-rust#rust-opencv-bindings. I'm using OpenCV 4.7.0 + v0.66.0 for bindings
  3. Linear algebra - https://github.com/dimforge/nalgebra
  4. Traditional YOLO v3 / v4 / v7 via OpenCV's DNN module - https://arxiv.org/abs/1804.02767 / https://arxiv.org/abs/2004.10934 / https://arxiv.org/abs/2207.02696
  5. YOLO v8 via ONNX + OpenCV's DNN module - https://github.com/ultralytics/ultralytics
  6. actix-web for web part - https://actix.rs/

Notice:

UI is developed in seprate repository: https://github.com/LdDl/rust-road-traffic-ui. Prepared static directory after npm run build is here'

Traffic flow parameters

Both REST API and Redis publisher export following parameters for each user-defined vehicle class:

For the all user-defined vehicles' classes there are:

Screenshots

Installation and usage

  1. You need installed Rust compiler obviously. Follow instruction of official site: https://www.rust-lang.org/tools/install

  2. You need installed OpenCV and its contributors modules. I'm using OpenCV 4.7.0. I'd highly recommend to use OpenCV with CUDA. Here is Makefile adopted from this one if you want build it from sources (it's targeted for Linux user obviously).

    sudo make install_cuda

    Be aware: OpenCV < 4.7.0 probably wont work with YOLOv8 (even with ONNX opset12) if you need those.

  3. OpenCV's bindings have already meant as dependencies in Cargo.toml

  4. Clone the repo

    git clone https://github.com/LdDl/rust-road-traffic.git

    Well, actually I provide yolov4-tiny configuration and weights file from official repository (authors of YOLOv4), but you are free to use yours. I provide video file as sample also.

  5. Сhange parameters for this utility by using template of configuration file. There is detailed explanation of each parameter.

  6. Download weights and configuration files (optional)

  7. Run

    cargo run path-to-toml-file

    If you want to use some Rust's optimizations then call build and run

    cargo build --release && ./target/release/rust-road-traffic path-to-toml-file

    If you want both optimized in term of perfomance and stripped executable binary (thanks to https://github.com/rust-lang/cargo/issues/3483)

    export RUSTFLAGS='-C link-arg=-s' && cargo build --release && ./target/release/rust-road-traffic path-to-toml-file
  8. UI configuration

    If you enabled both REST API and MJPEG streaming and you want to adjust parameters for detection zones you could open http://localhost:42001/ in your browser and adjust polygons as you need (this UI still needs to be debugged and polished):

    Configuration file lines:

    [rest_api]
        enable = true
        host = "0.0.0.0"
        back_end_port = 42001
        api_scope = "/api"
        [rest_api.mjpeg_streaming]
            enable = true
  9. REST API

    If you want to do some REST calls you can do following (based on rest_api field in TOML configuration files)

    # Get polygons (GeoJSON) in which road traffic monitoring is requested
    curl -XGET 'http://localhost:42001/api/polygons/geojson'
    # Get statistics info for each polygon and each vehicle type in that polygon
    curl -XGET 'http://localhost:42001/api/stats/all'
  10. Export data

    If you've enabled Redis output you can connect to Redis server (e.g. via CLI) and monitor incoming messages:

    Configuration file lines:

    [redis_publisher]
        enable = true
        host = "localhost"
        port = 6379
        password = ""
        db_index = 0
        channel_name = "DETECTORS_STATISTICS"

    Both REST API and Redis publisher reset statistics in specific amount of time which could be adjusted via reset_data_milliseconds option:

    [worker]
        reset_data_milliseconds = 30000

Virtual lines

This utility supports vehicle counting via two approaches: Vehicle appeared in the zone Vehicle crossed the line

But what is the point to have optional virtual line for zone? Why just not use either line or zone at all?

-- Well, zone is essential for estimating speed, so it is needed for sure. Why need line then: sometimes it is needed to register vehicles in specific direction only or at specific moment of time (in center of zone, in bottom of zone, after zone and etc.).

You can configure virtual lines via configuration file or via UI (look at showcase):

[[road_lanes]]
    lane_number = 0
    lane_direction = 0
    geometry = [[204, 542], [398, 558], [506, 325], [402, 318]]
    geometry_wgs84 = [[-3.7058048784300297,40.39308821416677],[-3.7058296599552705,40.39306089952626],[-3.7059466895758533,40.393116604041296],[-3.705927467488266,40.39314855180666]]
    color_rgb = [255, 0, 0]
    # Remove lines below if you don't want to include virtual line
    # Note: There is only one possible virtual line for given zone 
    [road_lanes.virtual_line]
        geometry = [[254, 456], [456, 475]]
        color_rgb = [255, 0, 0]
        # lrtb - left->right or top-bottom object registration
        # rtbt - right->left or bottom->top object registration
        direction = "lrtb"

ROADMAP

Please see this file

Support

If you have troubles or questions please open an issue.